Iterate through keys of hashtable java

1.Basic java example program to iterate keys of hashtable
  • Enumeration keys()   This method used to get keys of hashmap

  1. package com.iteartekeysHashtable;
  2.  
  3. import java.util.Hashtable;

  4. import java.util.Enumeration;
  5.  
  6. public class HashtableExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10.  //create Hashtable object
  11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
  12.        
  13. //add key value pairs to Hashtable
  14. hashtable.put("1","Java Interview Questions");
  15. hashtable.put("2","Java Interview Programs");
  16. hashtable.put("3","Concept and exampe program");
  17. hashtable.put("4","Concept and interview Questions");
  18. hashtable.put("5","Java Quiz");
  19. hashtable.put("6","Real time examples");
  20.  
  21.  
  22.  
  23. Enumeration e=hashtable.keys();
  24.           
  25.  
  26.            
  27. // display search result
  28.  while (e.hasMoreElements()) {
  29.         System.out.println(e.nextElement());
  30. }    

  31. }
  32.  
  33. }
     



Output:

  1. 6
  2. 5
  3. 4
  4. 3
  5. 2
  6. 1

Check particular value exist in hashtable

1.Basic java example program to check particular value exist in hashtable
  • boolean contains(Object value)   This method used to check  particular value exist in hashtable

  1. package com.CheckElelemtHashtable;
  2.  
  3. import java.util.Hashtable;

  4. import java.util.Enumeration;
  5.  
  6. public class HashtableExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10.  //create Hashtable object
  11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
  12.        
  13. //add key value pairs to Hashtable
  14. hashtable.put("1","Java Interview Questions");
  15. hashtable.put("2","Java Interview Programs");
  16. hashtable.put("3","Concept and exampe program");
  17. hashtable.put("4","Concept and interview Questions");
  18. hashtable.put("5","Java Quiz");
  19. hashtable.put("6","Real time examples");
  20.  
  21.  /*
  22.  To check whether a particular value exists in Hashtable we need to use
  23.  boolean containsKey(Object value) method of Hashtable class.
  24. if the Hashtable contains mapping for specified value It returns true
  25. otherwise returns false.
  26. */
  27.        
  28.  boolean isExists = hashtable.contains("Java Quiz");
  29.  System.out.println("Java Quiz exists in Hashtable ? : " + isExists);

  30. System.out.println("Hashtable values : ");
  31.  
  32. Enumeration e=hashtable.elements();
  33.           
  34.  
  35.            
  36. // display search result
  37.  while (e.hasMoreElements()) {
  38.         System.out.println(e.nextElement());
  39. }    

  40. }
  41.  
  42. }
     



Output:

  1. Java Quiz exists in Hashtable ? : true
  2. Hashtable values : 
  3. Real time examples
  4. Java Quiz
  5. Concept and interview Questions
  6. Concept and exampe program
  7. Java Interview Programs
  8. Java Interview Questions

Check if a particular key exists in Java Hashtable example

1.Basic java example program to check particular element exist in hashtable.
  • boolean containsKey(Object key)   This method used to check  particular key exist in hashtable

  1. package com.CheckElelemtHashtable;
  2.  
  3. import java.util.Hashtable;
  4. import java.util.Iterator;
  5.  
  6. public class HashtableExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10.  //create Hashtable object
  11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
  12.        
  13. //add key value pairs to Hashtable
  14. hashtable.put("1","Java Interview Questions");
  15. hashtable.put("2","Java Interview Programs");
  16. hashtable.put("3","Concept and exampe program");
  17. hashtable.put("4","Concept and interview Questions");
  18. hashtable.put("5","Java Quiz");
  19. hashtable.put("6","Real time examples");
  20.  
  21.  /*
  22.  To check whether a particular key exists in Hashtable we need to use
  23.  boolean containsKey(Object key) method of Hashtable class.
  24. if the Hashtable contains mapping for specified key It returns true
  25. otherwise returns false.
  26. */
  27.        
  28.  boolean isExists = hashtable.containsKey("5");
  29.  System.out.println("5 exists in Hashtable ? : " + isExists);

  30.  
  31.  
  32. Enumeration e=hashtable.elements();
  33.           
  34.  System.out.println("Display result:"); 
  35.            
  36. // display search result
  37.  while (e.hasMoreElements()) {
  38.         System.out.println(e.nextElement());
  39. }    

  40. }
  41.  
  42. }
     



Output:

  1. 5 exists in Hashtable ? : true
  2. Hashtable values : 
  3. Display result:
  4. Real time examples
  5. Java Quiz
  6. Concept and interview Questions
  7. Concept and exampe program
  8. Java Interview Programs
  9. Java Interview Questions

Non static blocks in java example

Non Static Blocks in java

  • When ever object created non static blocks will be executed before the execution of constructor
  • Non static blocks are class level block which does not have prototype


  1. package nonstaticblocks;
  2. public class A {
  3.  
  4.    {
  5.         
  6.        System.out.println("non static block executed");
  7.  
  8.     }
  9.  
  10. }


What is the need of Non static blocks in java?

  • To execute any logic whenever object is created irrespective of constructor used in object creation.

Who will execute Non static blocks?

  • Non static blocks are automatically called by JVM for every object creation in java stack area

How many Non static blocks we can create?
  • We can create any number of Non static blocks

Order of execution of non static blocks

  • Order of execution of non static blocks will be order as they are defined.
  1. package nonstaticblocks;
  2. public class A {
  3.  
  4. {

  5.   System.out.println("first block");
  6.  
  7. {
  8.  System.out.println("second block");
  9. }
  10.  
  11. {
  12.  System.out.println("third block");
  13. }
  14. public static void main(String[] args) {
  15.   A obj= new A();
  16. }
  17. }

 Output:

  1. first block
  2. second block
  3. third block


 Order of execution of non static blocks with respect to constructor?



non static blocks in java example program


Remove all elements LinkedHashSet example

1.Basic java example program to remove all elements in linkedhashset
  • clear()   This method used to remove all elements from Linkedhashset.

  1. package com.removeelementLinkedhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.       
  19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
  20.  
  21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
  22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
  23.   

  24.  
  25. System.out.println("LinkedHashSet after removal : ");
  26.  
  27. Iterator it=linkedhashset.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  linkedhashset.clear();
  34.  System.out.println("LinkedHashSet after removal  all elements"); 
  35.  
  36. }
  37.  
  38. }
     



Output:

  1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
  2. and example program, Concept and interview questions, Java Quiz]
  3. Was Java Quiz removed from LinkedHashSet ? true
  4. LinkedHashSet after removal :
  5. Java Interview Questions
  6. Java interview program
  7. Concept and example program
  8. Concept and interview questions
  9. LinkedHashSet after removal all elements  :[]

Differences between Default constructor and no argument constructor in java

Differences between default constructor and no argument constructor


Default Constructor in java:

  • When we write a class without any constructor then at compilation time java compiler creates a default constructor in our class.
  • The accessibility modifier of the default constructor is same as accessibility modifier of class.
  • The allowed accessibility modifier are public and default.
  • Default constructor added by java compiler this constructor does not have anything except super(); call.





  1. package constructor;
  2. public class A {
  3.   
  4.  
  5. }


  1. package constructor;
  2. public class A {
  3.  
  4.     A(){
  5.         
  6.         super();
  7.  
  8.     }
  9.  
  10. }



  • If our class have any constructor then java compiler does not create default constructor

No-argument Constructor in java:

  • As a developer we can create our own constructor with no arguments is known as no-argument constructor.
  • It can have all four accessibility modifiers as it is defined by developer.
  • So allowed accessibility modifiers are public, private, protected and default
  • It can have logic including super call.


  1. package constructor;
  2. public class A {
  3.  
  4.     A(){
  5.         
  6.   super();
  7.   System.out.println("no -argument constructor");

  8.     }
  9.  
  10. }

  • The common point between default and no-argument constructor 
  • Both does not have any arguments.
  • And one more point we need to remember that in no-argument constructor also by default first statement will be super() call which is added by java compiler if it does not have.

Remove specified element from Java LinkedHashSet example

1.Basic java example program to remove particular element in linkedhashset
  • boolean remove(Object o)    This method used to remove specified element from Linkedhashset.

  1. package com.removeelementLinkedhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.       
  19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
  20.  
  21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
  22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
  23.   

  24.  
  25. System.out.println("LinkedHashSet after removal : ");
  26.  
  27. Iterator it=linkedhashset.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  
  34. }
  35.  
  36. }
     



Output:

  1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
  2. and example program, Concept and interview questions, Java Quiz]
  3. Was Java Quiz removed from LinkedHashSet ? true
  4. Java Interview Questions
  5. Java interview program
  6. Concept and example program
  7. Concept and interview questions


Select Menu