Retrieving Values ​​from a HashMap

I tried to learn and understand from the work of HashMap. So I created this hash map to store certain values ​​which when displayed using Iterator give me outputs like

1=2 2=3 3=4 

etc. I get this output using the Iterator.next() function. Now, what is my actual doubt that since the type of this value is returned to the Iterator Object, if I need to extract only the values ​​of the right side of the above equalities, is there any function for this? Something like a substring. Is there any way to get results like

  2 3 4 

Any help would be appreciated. thanks in advance.

+6
source share
5 answers

I would use something like

 Map<Integer, Integer> map = new HashMap<>(); for(int value: map.values()) System.out.println(value); 
+11
source

You are looking for map.values() .

+3
source

The map has a method called values ​​() to get the Collection of all values. (Right side)

Similarly, there is a method to call keySet () to get the set of all keys. (left-hand side)

+2
source
 import java.util.HashMap; public class Test { public static void main( String args[] ) { HashMap < Integer , Integer > map = new HashMap < Integer , Integer >(); map.put( 1 , 2 ); map.put( 2 , 3 ); map.put( 3 , 4 ); for ( Integer key : map.keySet() ) { System.out.println( map.get( key ) ); } } } 
+2
source

You need the Map # values ​​() method, which returns Collection .

You can get Iterator from this collection in the usual way.

0
source

Source: https://habr.com/ru/post/913358/


All Articles