0

Here's some code:

ArrayList<String> List = new ArrayList<>();
Map<String, List<String> > map  = new HashMap<String, List<String>>();

List.add("stringA");
List.add("stringB");
List.add("stringC");

for(int i = 0; i<List.size();i++){
            String key = List.get(i);
            List<String> value = new ArrayList<String>();
            map.put(key, value);
        }

This code takes whatever is in the ArrayList, loops through it, adds it to the Map, and then creates an empty ArrayList with each string name as the variable name. Now, this works, but there's one problem, unless I'm overlooking something. At some point, I will need to access the new empty ArrayLists that are in the map. However, I won't know what the titles of these ArrayLists are, without printing them out, which I don't want to do. Basically, I'm thinking I need a map method or class and then an additional map key method or class. I'm not sure how to implement it but maybe something like this:

public class MapKey {
public MapKey(int count, String header){

    }
}

Map<MapKey, List<String> > map  = new HashMap<MapKey, List<String>>();

Another option I've considered is to somehow loop through the map array and add Strings to each ArrayList, but I'm very new to maps and looping through them. Especially ones that contain ArrayLists as their values.

REAL O G
  • 693
  • 7
  • 23
  • 2
    "At some point I will need to access [these] ArrayLists ... in the map. ... I won't know what the titles of these ArrayLists are..." It's not clear to me whether you want to access a specific ArrayList (which cannot be done without knowing the map key) or whether you want to iterate through the entire map and perform some uniform action, in which case I'd like to introduce you to [`Map.keySet()`](https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#keySet()) and [`Map.entrySet()`](https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#entrySet()). – dcsohl May 27 '15 at 20:49
  • you can retrieve based on key names. `Set keys = map.keySet()` , to get the value, `ArrayList value = map.get(key);` – Haifeng Zhang May 27 '15 at 20:51
  • @dcsohl I have separate ArrayLists outside of the map that are separated into categories. I would then need to iterate through these outside ArrayLists and merge them into the ArrayLists inside the map. So for instance, lets say I have `ArrayList vars1` that is separate from whatever the name of the first ArrayList is inside the map. I would then need to to loop through `vars1` and add it to the first ArrayList in the map, and then do it again for another ArrayList, but this time put it in the second one in the map and so on and so on. – REAL O G May 27 '15 at 20:56
  • @GD check my answer, I added your new request about loop through outside ArrayList objects.. – Haifeng Zhang May 27 '15 at 21:01

2 Answers2

1

There're multiple ways to access keys and values of your HashMap:

for (Map.Entry<String,ArrayList<String>> entry : map.entrySet()) {
  String key = entry.getKey();
  ArrayList<String> value = entry.getValue();
  // do your work
}

or

Set<String> keys = map.keySet();
for(String key : keys){
    ArrayList<String> value = map.get(key);
}

Read the java HashMap api Java HashMap Link

Edit: you dont need to loop through your outside ArrayList objects when you add all of its elements to another, just simply invoke addAll(), it will append all elements of an arraylist to another.

ArrayList<String> aList = map.get("stringA");

assume your first outside ArrayList is called outListOne;

aList.addAll(outListOne);

Appends to corresponding lists:

   //assume number of outside lists are equal to number of map elements
    String[] keysArr = {"stringA", "stringB", "stringC"};
    ArrayList[] outLists = {outListOne, outListTwo, outListThree};

    // adds outside lists to corresponding map ArrayList lists
    for(int i = 0; i < keysArr.length; i++){
        list = map.get(keysArr[i]);  // you ArrayList in a map, get it by key name
        list.addAll(outLists[i]);  // append elements from out list to corresponding list
    }
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
  • I would need `outListOne` to only append all of it's elements to the very first Array in the hashmap set. Then I would need `outListTwo` to append all of it's elements to only the second array in the hashmap and so on and so forth. I'm used to doing this with Arrays using the indexes, but since there are no indexes it is confusing to me. – REAL O G May 27 '15 at 21:07
  • You are awesome. Thanks for the help. Sorry for the dumb questions. – REAL O G May 27 '15 at 21:27
1

Not exactly sure what you mean by "titles of these ArrayLists." But here are a few code snippets that might give you a better idea of how to work with your map:

// add string x to the list for "stringA"
map.get("stringA").add(x);

// print all the values in the list for "stringC"
for (String s: map.get("stringC")) {
  System.out.println(s);
}

// print the names of the lists that contain "xyzzy"
for (String key: map.keySet()) {
  if (map.get(key).contains("xyzzy")) {
    System.out.println(key);
  }
}

// remove "foo" wherever it appears in any of the lists
for (List<String> list: map.values()) {
  while (list.remove("foo")){}
}
Andy Lowry
  • 785
  • 7
  • 12