Constructor in HashMap
HashMap() : It is the default constructor which creates an instance of HashMap with initial capacity 16 and load factor 0.75.
HashMap(int initial capacity) : It creates a HashMap instance with specified initial capacity and load factor 0.75.
HashMap(int initial capacity, float loadFactor) : It creates a HashMap instance with specified initial capacity and specified load factor.
HashMap(Map map) : It creates instance of HashMapwith same mappings as specified map.
CODING:HashMap() : It is the default constructor which creates an instance of HashMap with initial capacity 16 and load factor 0.75.
HashMap(int initial capacity) : It creates a HashMap instance with specified initial capacity and load factor 0.75.
HashMap(int initial capacity, float loadFactor) : It creates a HashMap instance with specified initial capacity and specified load factor.
HashMap(Map map) : It creates instance of HashMapwith same mappings as specified map.
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
public class hasmap {
public static void main(String[] args){
HashMap<Integer,String> o=new HashMap<>();
o.put(1, "one");
o.put(2, "two");
o.put(3,"three");
System.out.println("Get "+ o.get(2));
o.remove(2);
Collection<String> val=o.values();
Iterator<String> ans=val.iterator();
while(ans.hasNext()){
System.out.println(ans.next());
}
}
}
import java.util.HashMap;
import java.util.Iterator;
public class hasmap {
public static void main(String[] args){
HashMap<Integer,String> o=new HashMap<>();
o.put(1, "one");
o.put(2, "two");
o.put(3,"three");
System.out.println("Get "+ o.get(2));
o.remove(2);
Collection<String> val=o.values();
Iterator<String> ans=val.iterator();
while(ans.hasNext()){
System.out.println(ans.next());
}
}
}
Comments
Post a Comment