Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A clarification on Java's Map system and ":" operator

I am trying to learn Java’s Map<> system.
I cannot find a lot of information about it online so I am referring myself to a website called javatpoint.com.

At one point in the explanation the code used looks like this:

public static void main(String args[]){  
  Map<Integer,String> map=new HashMap<Integer,String>();  
  map.put(100,"Amit");  
  map.put(101,"Vijay");  
  map.put(102,"Rahul");  
  //Elements can traverse in any order  
  for(Map.Entry m:map.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  

I can understand most of it up until

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for(Map.Entry m:map.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  

I have absolutely no clue what is happening here, firstly, what is the ":" operator, and secondly what exactly does ".Entry" and ".entrySet" do?

I would really appreciate an explanation as I have a heavy background in python but have never seen this.
Thank you!

>Solution :

: is part of java foreach syntax:

for (type var : array) 
{ 
    statements using var;
}

Using foreach you are getting collection items one by one. map.entrySet() is your collection of items and Map.Entry m is a single item of collection that you get in each iteration of foreach.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading