Java program to remove key value from hashmap

1.Basic java example program to remove value from hashmap.

  • using  Object remove(Object key) method we can remove key value pair of hashmap.


  1. package com.javaremovevaluehashmap;
  2.  
  3. import java.util.Hashmap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class HashmapExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12. //create an Hashmap object
  13. HashMap<String, String> hashmap = new HashMap();
  14.        
  15. //Add key values pairs to hashmap
  16.         
  17. hashmap.put("1","One");
  18. hashmap.put("2","Two");
  19. hashmap.put("3","Three");
  20. hashmap.put("4","Four");
  21. hashmap.put("5","Five");
  22. hashmap.put("6","Six");       
  23.       String key=null;
  24.       String value="java programming basics Interview questions";   
  25. hashmap.put(key,value);
  26.  
  27. /*
  28. To remove a key value pair from HashMap use
  29. Object remove(Object key) method of HashMap class.
  30. It returns either the value mapped with the key or null if no value
  31. was mapped.    
  32.  */      
  33.  
  34.  Object object = hashmap.remove("4");
  35.         System.out.println(object + " Removed from HashMap");
  36.     
  37.  
  38. if(!hashmap.isEmpty()){
  39.  
  40. Iterator it=hashmap.entrySet().iterator();
  41.              
  42. while(it.hasNext()){

  43. Map.Entry obj=(Entry) it.next();
  44. System.out.print(obj.getKey()+" ");
  45. System.out.println(obj.getValue());
  46.              
  47. }           
  48. }   
  49.  
  50. }
  51.  
  52. }
     



Output:

  1. Four Removed from HashMap
  2. null java programming basics Interview questions
  3. 1 One
  4. 2 Two
  5. 3 Three
  6. 5 Five
  7. 6 Six

Java Example program to restrict a class from creating not more than three objects

Question: How to restrict a class from creating multiple objects?

  • Restring a class from creating multiple objects or restricting class from creating not more than three objects this type of interview questions will come for experience interview or written tests.

  • Yes we can restrict a class from creating multiple objects.
  • As we know using singleton we can restrict a class from creating multiple objects it will create single object and share it.
  • Same design pattern we can apply here with counter 
  • In this we will take a static variable counter to check how many objects created
  • As we can access static variables in constructor and static variables are class level we can stake help of static variable to count number object created.
  • First three time we will create new objects and forth time we need to return 3rd object reference. if we don't want same object 4th time we can return null
  • By printing the hashcode() of the object we can check how many objects created. 
  •  

Basic java example program to restrict a class from not to create more than three instances




  1. package com.instanceofjava;
  2.  
  3. public class RestrictObjectCreation{

  4. private static RestrictObjectCreationobject;
  5. public static int objCount = 0;
  6.  
  7. private RestrictObjectCreation()
  8. {
  9.      System.out.println("Singleton(): Private constructor invoked");
  10.  
  11. objCount  ++;
  12. }
  13.  
  14. public static RestrictObjectCreation getInstance()
  15. {
  16.  
  17. if (objCount < 3)
  18. {
  19.  
  20. object = new RestrictObjectCreation();
  21.  
  22.  }
  23.  
  24. return object;
  25.  
  26. }
  27.  
  28. }
     

  1. package instanceofjava;
  2.  
  3. public class Test{

  4. public static void main(String args[]) {
  5.  
  6. RestrictObjectCreation obj1= RestrictObjectCreation.getInstance();
  7. RestrictObjectCreation obj2= RestrictObjectCreation.getInstance();
  8. RestrictObjectCreation obj3= RestrictObjectCreation.getInstance();
  9. RestrictObjectCreation obj4= RestrictObjectCreation.getInstance();
  10. RestrictObjectCreation obj5= RestrictObjectCreation.getInstance();
  11.  
  12. System.out.println(obj1.hashCode());
  13. System.out.println(obj2.hashCode());
  14. System.out.println(obj3.hashCode());
  15. System.out.println(obj4.hashCode());
  16. System.out.println(obj5.hashCode());
  17.  
  18. }
  19. }



Output:

  1. RestrictObjectCreation(): Private constructor invoked
  2. RestrictObjectCreation(): Private constructor invoked
  3. RestrictObjectCreation(): Private constructor invoked
  4. 705927765
  5. 366712642
  6. 1829164700
  7. 1829164700
  8. 1829164700


restrict  from creating multiple objects


You Might Like:

1.Five Different places to create object of a class




3.Six different ways to iterate list in java

Simple Way to Import all Missing Packages at Once : Eclipse shortcut

1.How to import all missing packages at once in java program eclipse:

  • While writing a java program or copied some line to eclipse then we need to import corresponding java packages.
  • Instead of checking one by one there is a short cut in eclipse to import corresponding packages at once
  • There is a short cut key to import all missing packages at once
  • "CTL+SHIFT+O" then one popup will come with all corresponding packages so we need to select corresponding missing package name then it automatically come. one by one it will automatically ask and we need to select.
  • Lets see an example program.

1.Copy This program and paste in eclipse and remove all packages.
2.Then it will show some errors on classes 
3. select CTL+SHIFT+O
4.Select missing packages


  1. package com.javacheckvaluehashmap;
  2.  
  3.  
  4. public class HashmapExample{
  5.  
  6. public static void main(String[] args) {
  7.   
  8. //create an Hashmap object
  9. HashMap<String, String> hashmap = new HashMap();
  10.        
  11. //Add key values pairs to hashmap
  12.         
  13. hashmap.put("1","One");
  14. hashmap.put("2","Two");
  15. hashmap.put("3","Three");
  16. hashmap.put("4","Four");
  17. hashmap.put("5","Five");
  18. hashmap.put("6","Six");       
  19.         
  20.        
  21. boolean isExists = hashmap.containsValue("Six");
  22. System.out.println("The value Six exists in HashMap ? : " + isExists);
  23.     
  24.  
  25. if(!hashmap.isEmpty()){
  26.  
  27. Iterator it=hashmap.entrySet().iterator();
  28.              
  29. while(it.hasNext()){

  30. Map.Entry obj=(Entry) it.next();
  31. System.out.print(obj.getKey()+" ");
  32. System.out.println(obj.getValue());
  33.              
  34. }           
  35. }   
  36.  
  37. }
  38.  
  39. }
     


eclipse short cut import all missing packages at once





import all missing packages once java program



Java Basic example program to check particular value exists in hashmap

1.Basic Java example program to check particular value exists in hashmap


  1. package com.javacheckvaluehashmap;
  2.  
  3. import java.util.Hashmap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class HashmapExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12. //create an Hashmap object
  13. HashMap<String, String> hashmap = new HashMap();
  14.        
  15. //Add key values pairs to hashmap
  16.         
  17. hashmap.put("1","One");
  18. hashmap.put("2","Two");
  19. hashmap.put("3","Three");
  20. hashmap.put("4","Four");
  21. hashmap.put("5","Five");
  22. hashmap.put("6","Six");       
  23.         
  24.  
  25.  /*
  26.  To check whether a particular value exists in HashMap use
  27.  boolean containsValue(Object value) method of HashMap class.
  28. containsValue(Object value) returns true if the HashMap contains mapping for specified
  29. value otherwise false.*/
  30.        
  31. boolean isExists = hashmap.containsValue("Six");
  32. System.out.println("The value Six exists in HashMap ? : " + isExists);
  33.     
  34.  
  35. if(!hashmap.isEmpty()){
  36.  
  37. Iterator it=hashmap.entrySet().iterator();
  38.              
  39. while(it.hasNext()){

  40. Map.Entry obj=(Entry) it.next();
  41. System.out.print(obj.getKey()+" ");
  42. System.out.println(obj.getValue());
  43.              
  44. }           
  45. }   
  46.  
  47. }
  48.  
  49. }
     



Output:

  1. The value Six exists in HashMap ? : true
  2. 1 One
  3. 2 Two
  4. 3 Three
  5. 4 Four
  6. 5 Five
  7. 6 Six

check particular value exits in hashmap

Basic Java Example program Check if a particular key exists HashMap

1.Basic Java example program to check particular key exists in hashmap

  1. package com.javacheckkeyhashmap;
  2.  
  3. import java.util.Hashmap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class HashmapExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12. //create an Hashmap object
  13. HashMap<String, String> hashmap = new HashMap();
  14.        
  15. //Add key values pairs to hashmap
  16.         
  17. hashmap.put("1","One");
  18. hashmap.put("2","Two");
  19. hashmap.put("3","Three");
  20. hashmap.put("4","Four");
  21. hashmap.put("5","Five");
  22. hashmap.put("6","Six");       
  23.         
  24.  
  25.  /*
  26.   To check whether a particular key exists in HashMap use
  27.    boolean containsKey(Object key) method of HashMap class.
  28.   containsKey(Object key) returns true if the HashMap contains mapping for specified key
  29.   otherwise false.
  30.  */
  31.  
  32. boolean isExists = hashmap.containsKey("4");
  33. System.out.println("4 exists in HashMap ? : " + isExists);
  34.     
  35.  
  36. if(!hashmap.isEmpty()){
  37.  
  38. Iterator it=hashmap.entrySet().iterator();
  39.              
  40. while(it.hasNext()){

  41. Map.Entry obj=(Entry) it.next();
  42. System.out.print(obj.getKey()+" ");
  43. System.out.println(obj.getValue());
  44.              
  45. }           
  46. }   
  47.  
  48. }
  49.  
  50. }
     



Output:

  1. 4 exists in HashMap ? : true
  2. 1 One
  3. 2 Two
  4. 3 Three
  5. 4 Four
  6. 5 Five
  7. 6 Six

Six different ways to iterate list in java

How many ways we can iterate list in java?

  • We can iterate list in 6  different ways in java.
  1. For Loop
  2. Enhanced For Loop
  3. While Loop
  4. Iterator
  5. Collections stream() util (Java8 feature)
  6. ListIterator

different ways to iterate list in java


1. Iterate list using For loop


1.Basic Java example program to iterate arraylist elements using list iterator
  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class IterateList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12.  List<String> instanceofjavaList = new ArrayList<String>();
  13.        
  14. //Add elements to Arraylist
  15.  
  16. // add 5 different values to arraylist 
  17. instanceofjavaList.add("Interview Questions");
  18. instanceofjavaList.add("Interview Programs");
  19. instanceofjavaList.add("Concept and example program");
  20. instanceofjavaList.add("Concept and interview questions");
  21. instanceofjavaList.add("Java Quiz");
  22.     
  23. for (int i = 0; i < instanceofjavaList.size(); i++) {
  24.  
  25.     System.out.println(instanceofjavaList.get(i));
  26.  
  27. }

  28. }
  29.  
  30. }
     



Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz


 2. Iterate list using Enhanced For loop

  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. public class IterateList{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create an ArrayList object
  11. List<String> instanceofjavaList = new ArrayList<String>();
  12.        
  13. //Add elements to Arraylist
  14.  
  15. // add 5 different values to arraylist 
  16. instanceofjavaList.add("Interview Questions");
  17. instanceofjavaList.add("Interview Programs");
  18. instanceofjavaList.add("Concept and example program");
  19. instanceofjavaList.add("Concept and interview questions");
  20. instanceofjavaList.add("Java Quiz");
  21.  
  22. for (String str: instanceofjavaList) {
  23.             System.out.println(str);
  24. }

  25. }
  26.  
  27. }
     


Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz


 3. Iterate list using while loop

  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. public class IterateList{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create an ArrayList object
  11. List<String> instanceofjavaList = new ArrayList<String>();
  12.        
  13. //Add elements to Arraylist
  14.  
  15. // add 5 different values to arraylist 
  16. instanceofjavaList.add("Interview Questions");
  17. instanceofjavaList.add("Interview Programs");
  18. instanceofjavaList.add("Concept and example program");
  19. instanceofjavaList.add("Concept and interview questions");
  20. instanceofjavaList.add("Java Quiz");
  21.  
  22.  
  23. int i = 0;
  24.  
  25. while (i < instanceofjavaList.size()) {
  26.  
  27.   System.out.println(instanceofjavaList.get(i));
  28.   i++;
  29. }

  30. }
  31.  
  32. }
     


Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz

 4. Iterate list using iterator

  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. public class IterateList{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create an ArrayList object
  11. List<String> instanceofjavaList = new ArrayList<String>();
  12.        
  13. //Add elements to Arraylist
  14.  
  15. // add 5 different values to arraylist 
  16. instanceofjavaList.add("Interview Questions");
  17. instanceofjavaList.add("Interview Programs");
  18. instanceofjavaList.add("Concept and example program");
  19. instanceofjavaList.add("Concept and interview questions");
  20. instanceofjavaList.add("Java Quiz");
  21.  
  22.  
  23. Iterator<String> itr = instanceofjavaList.iterator();
  24.  
  25. while (itr.hasNext()) {
  26.      System.out.println(itr.next());
  27. }
  28.  
  29. }
  30.  
  31. }
     


Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz


 5. Iterate list using Stream API.

  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. public class IterateList{
  5.  
  6. public static void main(String[] args) {
  7.   
  8. //create an ArrayList object
  9. List<String> instanceofjavaList = new ArrayList<String>();
  10.        
  11. //Add elements to Arraylist
  12.  
  13. // add 5 different values to arraylist 
  14. instanceofjavaList.add("Interview Questions");
  15. instanceofjavaList.add("Interview Programs");
  16. instanceofjavaList.add("Concept and example program");
  17. instanceofjavaList.add("Concept and interview questions");
  18. instanceofjavaList.add("Java Quiz");
  19.  
  20.  
  21. instanceofjavaList.forEach((name) -> {
  22.             System.out.println(name);
  23.         }); 

  24. }
  25.  
  26. }
     


Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz

6. Using List erator:

You Might Like:

1.What are all the different places to create object in java

Java program to insert an element to ArrayList using ListIterator Example

1.Basic Java example program to insert element using list iterator to arraylist
  1. package com.javaIteratearraylistiterator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class IterateListIteratorArrayList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12. ArrayList<String> arrayList = new ArrayList();
  13.        
  14. //Add elements to Arraylist
  15.  
  16. arrayList.add("A");
  17. arrayList.add("B");
  18. arrayList.add("C");
  19. arrayList.add("D");
  20. arrayList.add("F");
  21. arrayList.add("F");
  22. arrayList.add("G");
  23. arrayList.add("H");
  24. arrayList.add("I");
  25.     
  26.         
  27.  /*
  28. Get a ListIterator object for ArrayList using
  29. istIterator() method.
  30. */
  31.   
  32. System.out.println("Before inserting element");
  33.  
  34. for(int intIndex = 0; intIndex < arrayList.size(); intIndex++)
  35.               System.out.println(arrayList.get(intIndex)); 
  36. ListIterator itr = arrayList.listIterator();
  37.        
  38. /*
  39.       Use void add(Object o) method of ListIterator to add or insert an element
  40.       to List. It adds an element just before the element that would have
  41.       been returned by next method call and after the element that would have
  42.       returned by previous call.
  43.     */
  44.    
  45.     itr .next();
  46.        
  47.     //Add an element
  48.     itr .add("Added Element");
  49.     /*
  50.  
  51. System.out.println("After inserting element .");
  52.  
  53. for(int intIndex = 0; intIndex < arrayList.size(); intIndex++)
  54.               System.out.println(arrayList.get(intIndex));   
  55.  
  56. }
  57. }
     



Output:

  1. Before inserting element
  2. A
  3. B
  4. C
  5. D
  6. F
  7. F
  8. G
  9. H
  10. I
  11. After inserting element .
  12. A
  13. Added Element
  14. B
  15. C
  16. D
  17. F
  18. F
  19. G
  20. H
  21. I
Select Menu