0

I am looking to take a Hashmap that has many key/value pairs where each value has a list. I want to take an index, and make a list based on that index.

Essentially I am looking to take this code and turn it into a stream.

HashMap < Integer , List< Object >> map = new HashMap();
                     //name //age 
map.put("1", new List("Bob",20));
map.put("2", new List("Jim",37));
map.put("3", new List("Dan",30));
map.put("3", new List("Rick",40));

  List < Integer > s = new ArrayList();

  map.values().forEach(e - >
  {
      s.add(( Integer ) e.get(1)); //looking to extract all of the ages into
  });                            // a new list.

In my use case, each index of the list is a different type of object, so in this case I tried to use a String and an Integer. I mention this in case there is a way to select an item based on an object's type to put into the new list. I had found this example that mentioned "groupingBy" as a Collector's option, but It doesn't seem to work for my use-case

Shortcut for adding to List in a HashMap

Thank you for any help

MXBuster
  • 43
  • 7
  • @HovercraftFullOfEels I was in the process of editing the question myself which is why your edit didn't go through, is that why I was downvoted? – MXBuster Oct 30 '20 at 01:33
  • I don't know why your question was down-voted, but it may be closed for being unclear. Consider posting more explanation and better, real-world code that demonstrates the problem better. – Hovercraft Full Of Eels Oct 30 '20 at 01:35
  • @HovercraftFullOfEels I rolled back your edit and applied my edits to them, I hope that works for you. I will try to add more to this example to make it more clear. – MXBuster Oct 30 '20 at 01:37
  • 1
    You know that this: `map.add("3", new List("Dan","30"); map.add("3", new List("Rick","40");` will only hold the last addition in the map, and then the preceding line of data, Dan/30, will be discarded. Also your code has a bad anti-pattern, using an array or List as a substitute for a class. Much better to have the value data be a custom class that holds a name String and an age int (or date of birth). This will make creating your stream *much* easier and much more intuitive. – Hovercraft Full Of Eels Oct 30 '20 at 01:48
  • @HovercraftFullOfEels Thank you. I realized I had a few mistakes I had to edit, and that add should be "put." It does make sense that a class would be better, I had figured with only a few pieces of data in that array it wouldn't warrant a class, but I can see why it would be better. I think that collectors "GroupingBy" also might work out better with that. I'll try it out adn report back... Thank you very much for all of your help. – MXBuster Oct 30 '20 at 01:56

1 Answers1

1

Make a stream of the map's values collection, use Stream.map to extract the values you care about, and then Stream.collect to make a new collection.

List<Integer> ages = map.values().stream()
    .map(list -> (Integer) list.get(1))
    .collect(Collectors.toList());

I agree with the commenter on the question who said you should really make these into actual POJO objects - the code will be a lot clearer and less error prone if you do. If you go that route then you can use a method reference to get the ages:

.map(Person::getAge)
Player One
  • 607
  • 6
  • 12