Java HashMap: put() and size() methods
KW.java
import java.util.HashMap;

class KW 
{
    public static void main(String[] args) 
    {
        //HashMap class implements the Map interface
        HashMap<Long,String> hm = new HashMap<>();
        hm.put(25622348989L,"James Moore");
        hm.put(25622348990L,"Donald Taylor");
        hm.put(25622348991L,"Edward Parkar");
        hm.put(25622348992L,"Ryan Bakshi");
        System.out.println(hm);
        System.out.println("Size: "+hm.size());
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW {25622348992=Ryan Bakshi, 25622348991=Edward Parkar, 25622348990=Donald Taylor, 25622348989=James Moore} Size: 4 kodingwindow@kw:~$
Java HashMap: getKey(), getValue(), and get() methods
KW.java
import java.util.HashMap;

class KW 
{
    public static void main(String[] args) 
    {
        //HashMap class implements the Map interface
        HashMap<Long,String> hm = new HashMap<>();
        hm.put(25622348989L,"James Moore");
        hm.put(25622348990L,"Donald Taylor");
        hm.put(25622348991L,"Edward Parkar");
        hm.put(25622348992L,"Ryan Bakshi");
        
        for(HashMap.Entry m:hm.entrySet())
        {
            System.out.println("Account No: "+m.getKey()+" Name: "+m.getValue());
        }
        System.out.println(hm.get(25622348992L));
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Account No: 25622348992 Name: Ryan Bakshi Account No: 25622348991 Name: Edward Parkar Account No: 25622348990 Name: Donald Taylor Account No: 25622348989 Name: James Moore Ryan Bakshi kodingwindow@kw:~$
Java HashMap: keySet() and values() methods
KW.java
import java.util.HashMap;

class KW 
{
    public static void main(String[] args) 
    {
        //HashMap class implements the Map interface
        HashMap<Long,String> hm = new HashMap<>();
        hm.put(25622348989L,"James Moore");
        hm.put(25622348990L,"Donald Taylor");
        hm.put(25622348991L,"Edward Parkar");
        hm.put(25622348992L,"Ryan Bakshi");
        
        System.out.println(hm.keySet());
        System.out.println(hm.values());
        
        /*
         * for(Long l: hm.keySet()) { System.out.println(l); }
         * 
         * for(String s: hm.values()) { System.out.println(s); }
         */
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [25622348992, 25622348991, 25622348990, 25622348989] [Ryan Bakshi, Edward Parkar, Donald Taylor, James Moore] kodingwindow@kw:~$
Java HashMap: replace() method
KW.java
import java.util.HashMap;

class KW 
{
    public static void main(String[] args) 
    {
        //HashMap class implements the Map interface
        HashMap<Long,String> hm = new HashMap<>();
        hm.put(25622348989L,"James Moore");
        hm.put(25622348990L,"Donald Taylor");
        hm.put(25622348991L,"Edward Parkar");
        hm.put(25622348992L,"Ryan Bakshi");
        System.out.println(hm);
        
        hm.replace(25622348991L, "Edward D Parkar");
        hm.replace(25622348992L, "Ryan Bakshi", "Ryan S Bakshi");
        System.out.println(hm);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW {25622348992=Ryan Bakshi, 25622348991=Edward Parkar, 25622348990=Donald Taylor, 25622348989=James Moore} {25622348992=Ryan S Bakshi, 25622348991=Edward D Parkar, 25622348990=Donald Taylor, 25622348989=James Moore} kodingwindow@kw:~$
Java HashMap: putIfAbsent() and putAll() methods
KW.java
import java.util.HashMap;

class KW 
{
    public static void main(String[] args) 
    {
        //HashMap class implements the Map interface
        HashMap<Long,String> hm1 = new HashMap<>();
        hm1.put(25622348989L,"James Moore");
        hm1.put(25622348990L,"Donald Taylor");
        
        HashMap<Long,String> hm2 = new HashMap<>();
        hm2.put(25622348991L,"Edward Parkar");
        hm2.put(25622348992L,"Ryan Bakshi");
        hm2.putIfAbsent(25622348993L, "Marie Peters");
        
        hm2.putAll(hm1);
        for(HashMap.Entry m:hm2.entrySet())
        {
            System.out.println("Account No: "+m.getKey()+" Name: "+m.getValue());
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Account No: 25622348993 Name: Marie Peters Account No: 25622348992 Name: Ryan Bakshi Account No: 25622348991 Name: Edward Parkar Account No: 25622348990 Name: Donald Taylor Account No: 25622348989 Name: James Moore kodingwindow@kw:~$
Java HashMap: remove() and clear() methods
KW.java
import java.util.HashMap;

class KW 
{
    public static void main(String[] args) 
    {
        //HashMap class implements the Map interface
        HashMap<Long,String> hm = new HashMap<>();
        hm.put(25622348989L,"James Moore");
        hm.put(25622348990L,"Donald Taylor");
        hm.put(25622348991L,"Edward Parkar");
        hm.put(25622348992L,"Ryan Bakshi");
        
        hm.remove(25622348992L);
        hm.remove(25622348991L, "Edward Parkar");
        System.out.println(hm);
        
        hm.clear();
        if(hm.isEmpty())
        {
            System.out.println("Map is empty now");
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW {25622348990=Donald Taylor, 25622348989=James Moore} Map is empty now kodingwindow@kw:~$
Advertisement