HashMap
//in hashmap duplicate value can we insert but , duplicate key not , all are unique.
import java.util.HashMap;
public class Basic{
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>();
map.put(78,"ved");
map.put(99, "sneha");
map.put(89, "tarun");
map.put(90, "rajan");
System.out.println(map);
// size of map
System.out.println(map.size());
// get(key)- gives the value of key
System.out.println(map.get(90));
// remove(key) - it delete key and pair both
map.remove(89);
System.out.println(map);
// check value is exist or not
System.out.println(map.containsValue("sneha"));
// check key is exist or not
System.out.println(map.containsKey(99));
// we can update value- key remains same
map.put(78, "dev");
System.out.println(map);
// value same bit chnage key
map.put(9,"rajan");
System.out.println(map);
//keyset()
for(int key:map.keySet()){
String value=map.get(key);
System.out.println(key+" "+value);
}
//values() - if values are same then also print
for(String val:map.values()){
System.out.println(val);
}
//entrySet()- gives pairs
for(Object pair : map.entrySet()){
System.out.println(pair);
}
}
}
Comments
Post a Comment