dsafas fsafasfsa
dsafas fsafasfsa

Reputation: 101

Iterating through multiple predefined ArrayLists using a method

I have defined a few ArrayLists that are already populated. I have the names of all of the ones I want to iterate through in an array 'tagArrays'. Is it possible to iterate through each of them in a similar logic to mine. I know this code is not going to work however I'm not sure how the code is supposed to look. This is my attempt:

These are already populated and are defined in main method.

ArrayList<String> KEYWORDS = new ArrayList<String>();
ArrayList<String> CUSTOMERS = new ArrayList<String>();
ArrayList<String> SYSTEM_DEPS = new ArrayList<String>();
ArrayList<String> MODULES = new ArrayList<String>();
ArrayList<String> DRIVE_DEFS = new ArrayList<String>();
ArrayList<String> PROCESS_IDS = new ArrayList<String>();

This is the logic I'm using

public void genericForEachLoop(POITextExtractor te) {

        final String[] tagArrays = {"KEYWORDS", "CUSTOMERS", "SYSTEM_DEPS", "MODULES", "DRIVE_DEFS", "PROCESS_IDS"};
        ArrayList<String> al = new ArrayList<String>();

        for(int i=0; i<tagArrays.length; i++) {
            System.out.println(tagArrays[i]);
            al = tagArrays[i];


            for (String item : al) {
                if (te.getText().contains(item)) {
                    System.out.println(item);
                }
            }
        }
    }

I want the for each loop to be different every time e.g. once go through KEYWORDS, then go through CUSTOMERS etc.

Upvotes: 2

Views: 81

Answers (3)

davidxxx
davidxxx

Reputation: 131496

You cannot reference variables with string values in Java.
What you try to do could be performed with reflection.
But I don't encourage it : it is less readable, more brittle/error prone and slower as the "classical" way.

As alternative you can provide a varargs of List<String> as last parameter of the method:

public void genericForEachLoop(POITextExtractor te, String[] tagArrays, List<String>... lists ) {

    int i = 0;
    for(List<String> list : lists) {    

        System.out.println(tagArrays[i]);                   
        for (String item : list) {

            if (te.getText().contains(item)) {
                System.out.println(item);
            }

        }

     i++;
    }
}

And invoke it in this way :

genericForEachLoop(te, 
   new String[]{"KEYWORDS", "CUSTOMERS", "SYSTEM_DEPS", "MODULES", "DRIVE_DEFS", "PROCESS_IDS"},
   KEYWORDS, CUSTOMERS,SYSTEM_DEPS,MODULES,DRIVE_DEFS,PROCESS_IDS);

Upvotes: 2

Amir
Amir

Reputation: 80

Rather creating String array you can create Array of ArrayList, which will help you to iterate dynamically like below.

   public static void main(String[] args) 
   {
    ArrayList<String> KEYWORDS = new ArrayList<String>();
    ArrayList<String> CUSTOMERS = new ArrayList<String>();
    ArrayList<String> SYSTEM_DEPS = new ArrayList<String>();
    ArrayList<String> MODULES = new ArrayList<String>();
    ArrayList<String> DRIVE_DEFS = new ArrayList<String>();
    ArrayList<String> PROCESS_IDS = new ArrayList<String>();

    final String[] tagNames = {"KEYWORDS", "CUSTOMERS", "SYSTEM_DEPS", "MODULES", "DRIVE_DEFS", "PROCESS_IDS"};
    final List<String> tagNameList=Arrays.asList(tagNames);
    final ArrayList[] tagList = { KEYWORDS, CUSTOMERS, SYSTEM_DEPS, MODULES, DRIVE_DEFS, PROCESS_IDS };

    for (ArrayList<String> list : tagList) {
        for(String str :list){
            if(str.contains(""))
            {

            }

    }
  }

Upvotes: 0

Bilbo Baggins
Bilbo Baggins

Reputation: 3019

I have tried following things with respect to Java 8. Below is the working example of your code, I have modified some of the code. Please check.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainClass {
    public static void main(String... args) {
        String[] strings = {"KEYWORDS", "CUSTOMERS", "SYSTEM_DEPS", "MODULES", "DRIVE_DEFS", "PROCESS_IDS"};
        ArrayList<String> KEYWORDS = new ArrayList<String>();
        KEYWORDS.add("goto");
        ArrayList<String> CUSTOMERS = new ArrayList<String>();
        CUSTOMERS.add("Royal Lotus");
        ArrayList<String> SYSTEM_DEPS = new ArrayList<String>();
        SYSTEM_DEPS.add("MAC BOOK");
        ArrayList<String> MODULES = new ArrayList<String>();
        MODULES.add("TETS MODULE");
        ArrayList<String> DRIVE_DEFS = new ArrayList<String>();
        DRIVE_DEFS.add("TEST DRIVE");
        ArrayList<String> PROCESS_IDS = new ArrayList<String>();
        PROCESS_IDS.add("-15153213");


        Map<String, List<String>> mapOfLists = new HashMap<>();
        mapOfLists.put("KEYWORDS", KEYWORDS);
        mapOfLists.put("CUSTOMERS", CUSTOMERS);
        mapOfLists.put("SYSTEM_DEPS", SYSTEM_DEPS);
        mapOfLists.put("DRIVE_DEFS", DRIVE_DEFS);
        mapOfLists.put("PROCESS_IDS", PROCESS_IDS);
        mapOfLists.put("MODULES", MODULES);
        genericForEachLoop(mapOfLists, strings);
    }


    public static void genericForEachLoop(Map<String, List<String>> mapOfLists, String[] listsToIterate) {
        Arrays.stream(listsToIterate).forEach((listName -> mapOfLists.get(listName).stream().forEach(str -> System.out.println(str))));
    }
}

I have taken out the String[] and providing it as an input to method so I can change it. Only those arrays where I want to iterate I can pass them. Further more building on top of @Eran's answer I am using the Map<String, List<String> for storing all the available ArrayLists.

Please modify the code as per your need. I have tried to use the streams and foreach methods from Java8.

Upvotes: 0

Related Questions