• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterating Hashmap  RSS feed

 
Ranch Hand
Posts: 62
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a HashMap like

and inner map object inside the oldMap is like

i added some values to both maps.


what are next statements should i use inorder to print the values of inner map?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to iterate over the entries (key-value pairs in the map), call entrySet() on the map and iterate:
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you do it in Java8 with a Stream object? Let's look through the Java™ Tutorials for Map. It mentions access to the entry set there, but not a lot else, so let's try the Map interface. That has no methods returning a Stream, and two methods which return some sort of set: Map#keySet() and Map#entrySet(). Since a Map contains 1‑to‑1 mappings, I would expect the two sets to have the same size, but don't know whether they have the same iteration order.
Let's imagine a Map<Something, String>; since the “V”s implement Comparable and provides a ready‑made Comparator, one can sort a Stream<String>You start off by getting the entry set with the same method that Jesper showed you. In that instance, the “V” was a Map of some sort, and you could create a Stream to iterate that Map's entry set, too. In my example you have not got a Stream<Map.Entry<Foo, String>> (line 4), which would be easier to handle because there is no need to “nest” Streams.
You can create a second Stream of a second type with the map() method. That takes a reference to a Function instance. If you do through Function, you find it has only one method, namely apply(), which actually needs to be implemented (the other three methods already have implementations), and Function is tagged with @FunctionalInterface. That means you can replace the reference to a Function instance with a λ; that means pass the e which we already know is a Map.Entry<Foo String> instance to the apply method as the method argument, and in the method body write the equivalent of
[return] e.getValue();
There is probably another way to do that, which I shall mention in a subsequent post.
The compiler “knows” the type of your Map.Entry, so it can infer that in line 5 you have a Stream<String>.
The sorted() method returns a Stream (line 6) which in this instance will be the same type as the preceding Stream. You pass the same ready‑made Comparator which I mentioned earlier as an argument, and that creates a Stream which sorts the preceding Stream's contents. That would require about 8MB extra memory to sort a 1000000‑element Stream, but the memory can be released by garbage collection after the method completes. If you have 1000000 elements in a Stream, you should insert a line 4½ reading “   .parallel()” because execution will be faster in parallel with so many elements.
The Comparator used there uses alphabetical order rather than “ASCIIbetical”; aardvark will come before Abraham. Be sure to read the link if you are using languages other than English.
The collect() method requires a Collector reference as its parameter. There is a ready‑made Collector which the Collectors#toList() method supplies, and when you pass that to the collect method, you don't get a Stream returned, but it tells the method to return a List; since you are in a Stream<String>, that will be a List<String>. And Bob's your uncle
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably replace the λ
e -> e.getValue()
which seems only to call the getValue method with a method reference
Map.Entry::getValue
Note no () required. You might need to replace String::CASE_INSENSITIVE_ORDER with String.CASE_INSENSITIVE_ORDER.
I haven't tested the code in the previous post. You can find out more about λs and method references in the Java™ Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!