Many time some little things goes our of our mind. Same thing happen in developer life also.So, here i am going to write a small piece of java code which is used daily routine.
Here i am talking about Collection framework Iteration of MAP.
When ever developer need Map almost all time they go to google for how to iterate MAP? so, ans for this question i am going to write small blog about MAP iteration. Bellow is the example for the same.
About Map : Map is part of collection framework. Mainly use to store data in key,value manner. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. now we go through step by step how to use MAP.
1) Declaration of MAP
Map<String,String> stringMap = new HashMap<String, String>();
2) Store data into MAP
stringMap.put("1","Apple" );
stringMap.put("2","Ball");
stringMap.put("3","Cat");
3) Iteration/Loop/Use of MAP
There are so many ways you can iterate MAP in java. but here i am going to simple way to iterate MAP in java as per my knowledge.
- by Iterator
- by EnterySet
- by keySet
Here we use keySet to iterator MAP. this is very simple way as per my development experiance. like...
for(String key : stringMap.keySet()){
System.out.println("Key : " + key + ", Value : " + stringMap.get(key) );
}
4) Exmaple
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class test {
public static void main(String[] s){
Map<String,String> stringMap = new HashMap<String, String>();
stringMap.put("3","Cat");
stringMap.put("1","Apple" );
stringMap.put("2","Ball");
for(String key : stringMap.keySet()){
System.out.println("Key : " + key + ", Value : " + stringMap.get(key) );
}
}
}
5) Output
Key : 3, Value : Cat
Key : 2, Value : Ball
Key : 1, Value : Apple
It's simple right? still we have to go googling for every time to iterate map. i ma sure this blog visit help you remember iteration of map.
That's it!!
Enjoy the code!!
Thank you,
Ketan Savaliya
Here i am talking about Collection framework Iteration of MAP.
When ever developer need Map almost all time they go to google for how to iterate MAP? so, ans for this question i am going to write small blog about MAP iteration. Bellow is the example for the same.
About Map : Map is part of collection framework. Mainly use to store data in key,value manner. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. now we go through step by step how to use MAP.
1) Declaration of MAP
Map<String,String> stringMap = new HashMap<String, String>();
2) Store data into MAP
stringMap.put("1","Apple" );
stringMap.put("2","Ball");
stringMap.put("3","Cat");
3) Iteration/Loop/Use of MAP
There are so many ways you can iterate MAP in java. but here i am going to simple way to iterate MAP in java as per my knowledge.
- by Iterator
- by EnterySet
- by keySet
Here we use keySet to iterator MAP. this is very simple way as per my development experiance. like...
for(String key : stringMap.keySet()){
System.out.println("Key : " + key + ", Value : " + stringMap.get(key) );
}
4) Exmaple
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class test {
public static void main(String[] s){
Map<String,String> stringMap = new HashMap<String, String>();
stringMap.put("3","Cat");
stringMap.put("1","Apple" );
stringMap.put("2","Ball");
for(String key : stringMap.keySet()){
System.out.println("Key : " + key + ", Value : " + stringMap.get(key) );
}
}
}
5) Output
Key : 3, Value : Cat
Key : 2, Value : Ball
Key : 1, Value : Apple
It's simple right? still we have to go googling for every time to iterate map. i ma sure this blog visit help you remember iteration of map.
That's it!!
Enjoy the code!!
Thank you,
Ketan Savaliya
No comments:
Post a Comment