Check if a particular element exists in Java LinkedHashSet Example

1.Basic java example program to check particular element is exists in linkedhashset
  • boolean contains(Object o)   This method Returns true if this set contains the specified element

  1. package com.checkelementhashset;
  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.  To check whether a particular value exists in LinkedHashSet we need to use
  20.   boolean contains(Object value) method of HashSet class.
  21.  this method returns true if the LinkedHashSet contains the value, otherwise returns false.
  22.  */
  23.        
  24.         boolean isExists = linkedhashset.contains("Java Quiz");
  25.         System.out.println("Java Quiz exists in LinkedHashSet? : " + isExists);    
  26.  

  27.  
  28. Iterator it=linkedhashset.iterator();
  29.              
  30. while(it.hasNext()){
  31. System.out.println(it.next());
  32.                      
  33. }   
  34.  
  35. }
  36.  
  37. }
     



Output:

  1. Java Quiz exists in LinkedHashSet ? : true
  2. Java Interview Questions
  3. Java interview program
  4. Concept and example program
  5. Concept and interview questions
  6. Java Quiz

Return statement in finally block in java

Can we write return statement in finally block

  • Finally block will executes always excepts system.exit().
  • So if we are returning some value in finally means it will be returned always
  • Finally will executed so method always returns finally return value and no need of keeping return value at end of the method.
  • And in finally after return if we keep some statement those statement will be treated as dead code.



i)Return statement in finally block
  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ 
  6.         
  7. try {
  8.  

  9.  
  10. } catch (Exception e) {
  11.  
  12. }
  13.  finally(){
  14.  return 1;
  15. }   
  16.  System.out.println("End of the method"); // Error : Unreachable code
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22.         TryCatchReturn obj = new TryCatchReturn();
  23.        
  24.  
  25. }

  26. }
     
ii) return statement in finally



  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ 
  6.         
  7. try {
  8.  

  9.  
  10. } catch (Exception e) {
  11.  
  12. }
  13.  finally(){
  14.  return 1;
  15. System.out.println("End of the method"); // Error : Unreachable code
  16. }   
  17.  
  18. }
  19.     
  20.     
  21. public static void main(String[] args) {
  22.         
  23.         TryCatchReturn obj = new TryCatchReturn();
  24.        
  25.  
  26. }

  27. }
     
iii) return statement in try catch and finally blocks



  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ 
  6.         
  7. try {
  8.  
  9. return 10;
  10.  
  11. } catch (Exception e) {
  12.  return 20;
  13. }
  14.  finally(){
  15.  return 30;
  16. }   
  17.  
  18. }
  19.     
  20.     
  21. public static void main(String[] args) {
  22.         
  23.  TryCatchReturn obj = new TryCatchReturn();
  24.        
  25.  System.out.println(obj.calc())
  26. }

  27. }



Output:


  1. 30

finally with return statement in java

Remove specified element from Java HashSet example

1.Basic java example program to remove element hashset
  • boolean remove(Object o) .   This method is used to remove element from hashset if it is present it returns true.

  1. package com.getSizehashset;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5.  
  6. public class HashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create object of HashSet
  11.  HashSet<Integer> hashSet = new HashSet();
  12.        
  13.  //add elements to HashSet object
  14.  hashSet.add(1);
  15.  hashSet.add(2);
  16.  hashSet.add(3);
  17.  hashSet.add(4);
  18.  hashSet.add(5);
  19.  hashSet.add(6);
  20.  hashSet.add(7);
  21.  hashSet.add(8);
  22.  
  23. System.out.println("Size of HashSet after addition : " + hashSet.size());
  24.  
  25. System.out.println("Hashset contains");
  26.  
  27. Iterator it=hashSet.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  
  34. boolean isRemoved = hashSet.remove(6);
  35. System.out.println("Was 6 removed from HashSet ? " + isRemoved );
  36.  
  37. System.out.println("HashSet after removal : " + hashSet);
  38. }
  39.  
  40. }
     


Output:

  1. Size of HashSet after addition
  2. 8
  3. hashset contains
  4. 1
  5. 2
  6. 3
  7. 4
  8. 6
  9. 7
  10. 8
  11. Was 6 removed from HashSet ?true
  12. HashSet after removal : [1, 2, 3, 4,5,7,8]

Get Size of Java HashSet Example

1.Basic java example program to get size of  hashset
  • int size()   This method is used get get size of  hashset

  1. package com.getSizehashset;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5.  
  6. public class HashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create object of HashSet
  11.  HashSet<Integer> hashSet = new HashSet();
  12.        
  13.  //add elements to HashSet object
  14.  hashSet.add(1);
  15.  hashSet.add(2);
  16.  hashSet.add(3);
  17.  hashSet.add(4);
  18.  hashSet.add(5);
  19.  hashSet.add(6);
  20.  hashSet.add(7);
  21.  hashSet.add(8);
  22.  
  23. System.out.println("Size of HashSet after addition : " + hashSet.size());
  24.  
  25. System.out.println("Hashset contains");
  26.  
  27. Iterator it=hashSet.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  
  34. }
  35.  
  36. }
     


Output:

  1. Size of HashSet after addition
  2. 8
  3. hashset contains
  4. 1
  5. 2
  6. 3
  7. 4
  8. 6
  9. 7
  10. 8

Return statement in try catch block java

1.Can we write return statement in try or catch blocks in java

  • Inside method if we have some statements which may proven to raise exceptions we need to keep those statements in side try catch blocks in order to handle the exceptions.

  • There is a situation where a method will have a return type and we can return some value at any part of the method based on the conditions.
  • So mixing these two scenarios we may have a situation to return some value from try block or catch block in such cases we need to follow some basic rules.
  • This is also one of the famous interview question  "can we write return statement in try block ", "can we write return statement in catch block ", "what will happen if we return some value in try block or catch block ", "will catch block execute after return statement " and "what are the basic rules we need to follow when we are returning or our method returning some value in try or catch blocks"
  • We will see the all the scenarios and conclusion.
Return statement in try block:

i) return statement in try block only
  • If we return a value in try block and if we do not return a value at the end of the method it leads to compile time exception 
  • Error: This method must return a result of type int

  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ // Error:This method must return a result of type int
  6.         
  7. try {
  8.  
  9. return 1;
  10.  
  11. } catch (Exception e) {
  12.  
  13. }
  14.         
  15.  System.out.println("End of the method");
  16. }
  17.     
  18.     
  19. public static void main(String[] args) {
  20.         
  21.         TryCatchReturn obj = new TryCatchReturn();
  22.        
  23.  
  24. }

  25. }
     

ii) return statement in try block and end of the method but after return one statement

  • Even though it in this condition it wont reach end of the method still it asks us for return at end of the method because in some cases there may be a chance of getting exception in try block so it will not completely execute try in this case we don not have return in catch so at end of the method it expects some value to be return because the  method have some return type.
  • If we are keeping return statement in try block only there may be a situation of chance of raising exception and try will not execute completely and it goes to catch block that is the reason it expecting a return at catch or end of the method 
  • So in this scenario we will try to keep return but after return we have written one statement

  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){
  6.         
  7. try {
  8.  
  9. return 1;
  10.  
  11. } catch (Exception e) {
  12.  
  13. }
  14.  
  15. return 10; 
  16.  System.out.println("End of the method"); // Error : Unreachable code
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22.         TryCatchReturn obj = new TryCatchReturn();
  23.        
  24.  
  25. }

  26. }
     

  • in the above program last statement will not execute at any condition so it became unreachable code as we know java does not supports unreachable codes so it will raise compile time error.
iii) return statement in try block and end of the method

  • This is the correct and successful scenario with respect to try with return statement in java
  • Lest see a java program on this




  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){
  6.         
  7. try {
  8.  
  9. return 1;
  10.  
  11. } catch (Exception e) {
  12.  
  13. }
  14.  
  15. System.out.println("End of the method");
  16. return 10; 
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22.   
  23. TryCatchReturn obj = new TryCatchReturn();
  24.        
  25. System.out.println(obj.calc());
  26.  
  27. }

  28. }
     

Output:

  1. 1

Return statement in catch block:

i) return statement in catch block only
  • If we return a value in catch block and if we do not return a value at the end of the method it leads to compile time exception 
  • Error: This method must return a result of type int

  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ // Error:This method must return a result of type int
  6.         
  7. try {
  8.  

  9.  
  10. } catch (Exception e) {
  11.  return 1;
  12. }
  13.         
  14.  System.out.println("End of the method");
  15. }
  16.     
  17.     
  18. public static void main(String[] args) {
  19.         
  20.         TryCatchReturn obj = new TryCatchReturn();
  21.        
  22.  
  23. }

  24. }
     

ii) return statement in catch block and end of the method but after return one statement

  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){
  6.         
  7. try {
  8.  
  9.  
  10. } catch (Exception e) {
  11.  
  12. return 1;
  13. }
  14.  
  15. return 10; 
  16.  System.out.println("End of the method"); // Error : Unreachable code
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22.         TryCatchReturn obj = new TryCatchReturn();
  23.        
  24.  
  25. }

  26. }
     

  • in the above program last statement will not execute at any condition so it became unreachable code as we know java does not supports unreachable codes so it will raise compile time error.
ii) return statement in catch block and end of the method





  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){
  6.         
  7. try {
  8.  
  9.  int x=12/0;
  10. } catch (Exception e) {
  11.  
  12. return 1;
  13. }
  14.  
  15. return 10; 
  16. }
  17.     
  18.     
  19. public static void main(String[] args) {
  20.         
  21. TryCatchReturn obj = new TryCatchReturn();
  22.        
  23.  System.out.println(obj.calc());
  24.  
  25. }

  26. }
     
Output:

  1. 1


Return statement with try catch block

try catch with return statement in java


try catch block with return statement
 

Java program to Copy all elements of Java HashSet to an Object Array

1.Basic java example program to Copy all elements of Java HashSet to an Object Array
  • Object[] toArray()   This method is used To copy all elements of java HashSet object into array use

  1. package com.copyelementhashset;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5.  
  6. public class HashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create object of HashSet
  11.  HashSet<Integer> hashSet = new HashSet();
  12.        
  13.  //add elements to HashSet object
  14.  hashSet.add(1);
  15.  hashSet.add(2);
  16.  hashSet.add(3);
  17.  hashSet.add(4);
  18.  hashSet.add(5);
  19.      
  20. Object[] objArray = hashSet.toArray();
  21.  
  22. //display contents of Object array
  23. System.out.println("HashSet elements are copied into an Array. Now Array Contains..");
  24.  
  25.  for(int index=0; index < objArray.length ; index++)
  26.   System.out.println(objArray[index]);

  27.  
  28. System.out.println("Hashset contains");
  29.  
  30. Iterator it=hashSet.iterator();
  31.              
  32. while(it.hasNext()){
  33. System.out.println(it.next());
  34.                      
  35. }   
  36.  
  37. }
  38.  
  39. }
     



Output:

  1. HashSet elements are copied into an Array. Now Array Contains..
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. Hashset contains
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5

Check if a particular element exists in Java HashSet Example

1.Basic java example program to check particular element is exists in hashset
  • boolean contains(Object o)   This method Returns true if this set contains the specified element

  1. package com.checkelementhashset;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5.  
  6. public class HashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create object of HashSet
  11.  HashSet<Integer> hashSet = new HashSet();
  12.        
  13.  //add elements to HashSet object
  14.  hashSet.add(1);
  15.  hashSet.add(2);
  16.  hashSet.add(3);
  17.  hashSet.add(4);
  18.  hashSet.add(5);
  19.      
  20.  /*
  21.  To check whether a particular value exists in HashSetwe need to use
  22.   boolean contains(Object value) method of HashSet class.
  23.  this method returns true if the HashSet contains the value, otherwise returns false.
  24.  */
  25.        
  26.         boolean isExists = hashSet.contains(5);
  27.         System.out.println("5 exists in HashSet ? : " + isExists);    
  28.  

  29.  
  30. Iterator it=hashSet.iterator();
  31.              
  32. while(it.hasNext()){
  33. System.out.println(it.next());
  34.                      
  35. }   
  36.  
  37. }
  38.  
  39. }
     



Output:

  1. 3 exists in HashSet ? : true
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
Select Menu