-4

Is possible to change Map< String, List< Integer >> to indvidual lists?

    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(1);
    list1.add(2);
    list1.add(3);
    list1.add(4);
    List<Integer> list2 = new ArrayList<Integer>();
    list2.add(100);
    list2.add(400);
    list2.add(500);
    list2.add(700);

    Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
    map.put ("account_number", list1);
    map.put ("Amount", list2);

    System.out.println(map);
    map: {Amount=[100, 400, 500, 700], account_number=[1, 2, 3, 4]}

    List<String> list3 = new ArrayList<String>();
    List<int> list4 = new ArrayList<int>();

Now I want to change the map to lists (list3, list4).

   Expected: map -> list3, list 4

   list3               list4
   Amount              100
   Amount              400
   Amount              500
   Amount              700
   account_number      1
   account_number      2
   account_number      3
   account_number      4

I tried, map.keySet(), map.values() to put them into separate list(list3, list4) but just couldn't achieve it

how can I do it?

Knight
  • 363
  • 2
  • 7
  • 24
  • 3
    This smells a lot like an [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), what is the original requirement? –  Sep 22 '17 at 13:38
  • The question is badly specified; judging by the accepted answer, what you actually wanted was a list containing all the keys and a (flat, non hierarchical) list containing all the values, correct? – Haroldo_OK Sep 22 '17 at 14:25
  • 1
    Toward the end, there is a listing of exactly what is desired. It's possible the question could have been phrased a little clearer but if one actually read the entire thing, it was obvious what the OP was trying to accomplish. – Jamie Bisotti Sep 22 '17 at 14:55
  • @JamieBisotti is it possible to get unique sequence numbers based on list3, list4 into list5? – Knight Sep 26 '17 at 13:09

3 Answers3

2

Something like this (I wrote it free-hand; might not compile):

for (final Entry<String, List<Integer> entry : map.entrySet()) {
    final String key = entry.getKey();
    final List<Integer> value = entry.getValue();
    for (final Integer val : value) {
        list3.add(key);
        list4.add(val);
    }
}
Jamie Bisotti
  • 2,605
  • 19
  • 23
  • 2
    Yup, this seems to work : https://repl.it/L3cv/0 I think you are the only person to understand what OP was actually asking for as well. – Karl Reid Sep 22 '17 at 13:42
0

Map.keySet() returns a Set of the keys and you can create it with

new ArrayList<>(map.keySet())

Map.values() returns a Collection of the values. So create it using

new ArrayList<>(map.values())
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
0

If you mean putting them into separate variables, then Java syntax won't allow you to do that.

On the other hand, if you want to turn your map into a list of lists, you could do something like this:

List<List<Integer>> allLists = new ArrayList<>(map.values());

Also, if you are using Java 8, and you want to flatten the list of lists into a simple list, you could also take a look at this: Turn a List of Lists into a List Using Lambdas

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80