codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
[25622348989, 25622348990, 25622348991, 25622348992]
[James Moore, Donald Taylor, Edward Parkar, Ryan Bakshi]
codeaft@codeaft:~$
Java LinkedHashMap: replace() method
Codeaft.java
importjava.util.LinkedHashMap;classCodeaft{publicstaticvoidmain(String[]args){//LinkedHashMap class implements the Map interfaceLinkedHashMap<Long,String>lhm=newLinkedHashMap<>();lhm.put(25622348989L,"James Moore");lhm.put(25622348990L,"Donald Taylor");lhm.put(25622348991L,"Edward Parkar");lhm.put(25622348992L,"Ryan Bakshi");System.out.println(lhm);lhm.replace(25622348991L,"Edward D Parkar");lhm.replace(25622348992L,"Ryan Bakshi","Ryan S Bakshi");System.out.println(lhm);}}
importjava.util.LinkedHashMap;classCodeaft{publicstaticvoidmain(String[]args){//LinkedHashMap class implements the Map interfaceLinkedHashMap<Long,String>lhm=newLinkedHashMap<>();lhm.put(25622348989L,"James Moore");lhm.put(25622348990L,"Donald Taylor");lhm.put(25622348991L,"Edward Parkar");lhm.put(25622348992L,"Ryan Bakshi");System.out.println(lhm.keySet());lhm.remove(25622348991L);System.out.println(lhm.keySet());lhm.clear();if(lhm.isEmpty()){System.out.println("LinkedHashMap is empty now");}}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
[25622348989, 25622348990, 25622348991, 25622348992]
[25622348989, 25622348990, 25622348992]
LinkedHashMap is empty now
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.