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
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.