HashSet class


Hierarchy of HashSet class:


 

 public class HashSet<E>
        extends AbstractSet<E>
           implements Set<E>, Cloneable, java.io.Serializable

Key points:

  • HashSet is sub class of AbstractSet class.
  • Underlying data structure of HashSet is HashTable.
  • HashSet implements Serializable and Cloneable interfaces
  • HashSet does not allow duplicate elements.
  • Insertion order is not preserved. No order(Based on hashcode of objects)



Constructors of HashSet:

1.HashSet( )
  • Creates an empty HashSet object with default initial capacity 16.  Fill ratio or load factor 0.75
2.HashSet(Collection obj)
  • This constructor initializes the hash set by using the elements of the collection obj.
3.HashSet(int capacity)
    • Creates an empty HashSet object with given capacity.
      4.HashSet(int capacity, float fillRatio)
      • This constructor initializes both the capacity and the fill ratio (also called load capacity) of the hash set from its arguments (fill ratio 0.1 to 1.0)

      Reverse words in a String

      1. Java Interview Program to Reverse words in a string


      1. package com.instaceofjava;
      2.  
      3. public class ReverseString {
      4.  
      5. public static void main(String[] args) {
      6.  
      7. String strng= "Instance of Java ";
      8.  
      9. String str[] =strng.split(" ");
      10.  
      11. String result="";
      12.  
      13. for(int i=str.length()-1;i>=0;i--){
      14.  
      15. result += str[i]+" ";
      16.  
      17. }
      18.  
      19. System.out.println(result );
      20. }
      21. }
      Output:

      1. Java of Instance



      2. Java Interview Program to Reverse words in a string


      1. package com.instaceofjava;
      2.  
      3. public class ReverseString {
      4.  
      5. public static void main(String[] args) {
      6.  
      7. String strng= "Instance of Java ";
      8.  
      9. StringBuilder sb = new StringBuilder(strng.length() + 1);
      10.  
      11.   String[] words = strng.split(" ");
      12.  
      13.   for (int i = words.length - 1; i >= 0; i--) {
      14.          sb.append(words[i]).append(' ');
      15.    }
      16.     
      17.     sb.setLength(sb.length() - 1); 
      18.  
      19.    String result= sb.toString();  
      20.  
      21.     System.out.println(result);

      22. }
      23. }
      Output:

      1. Java of Instance

      Introducton to Spring



      Spring Framework history:

      • Spring was developed in June,2003
      • Initially developed by Rod Johnson.
      • Latest version of  Spring Framework version 4.1.6 released in Mar 2015.

      What is Spring:
      • Spring is  Open Source Framework.
      • Spring is Lightweight Application Framework. 
      • Spring is  Simple framework.
      • Spring is loosely coupled. 
      • Spring is a complete and a modular framework,because spring can be used in all layers of applications means front end(Spring MVC),Database(Spring JDBC,Spring ORM).

      Why Spring:
      • Why Spring come in to picture is because of EJB fails,EJB having multiple configuration files,it is affected on performance.so spring came up.
      • Struts designed for Web Layer,like Other Frameworks also addressed specific layer,But Spring Framework provides solution to support all layers of application.
      • Spring is non invasive framework,means Spring doesn't force to implement or extend any class from predefined class from Spring API.

      Advantages of Spring:
      • Spring is open source and lightweight framework.
      • Spring is supports all layers including web layer.
      • Easy for testing.
      • Spring Supports POJO Model.
      • POJO-Plain Old Java Object. 
      • Spring works in simple Java environment,because it is non,invasive.
      • Spring can be integrated with any Application Server
      • Spring simplifies J2EE development. 



      Spring Modules:
       spring 1.x have seven modules,but in 2.x on wards we have 6 modules.
      • Spring Core
      • Spring DAO(Spring JDBC)
      • Spring AOP(Ascept Orient Programming)
      • Spring ORM(Spring Hibernate)
      • Spring MVC
      • Spring WEB
      • Spring Context(J2EE)  
      Spring 2.x Modules:
      from Spring 2.x ,Spring -WEB and Spring-MVC is combined.
      • Spring Core
      • Spring DAO(Spring JDBC)
      • Spring AOP(Ascept Orient Programming)
      • Spring ORM(Spring Hibernate)
      • Spring WEB-MVC
      • Spring Context(J2EE


      spring tutorial



      • Spring is combination of various modules Seven well defined modules,Most of them are reasonably independent.
      • Spring modules built using modular approach,you can use only required modules Each module is set of one or more JAR files
      • Spring’s core module is “Inversion of Control”(IoC) also known as “Dependency Injection”.
      • All other Spring modules are built on top of IoC.IoC is a Foundation or Container of Spring Framework.
      • Spring core module is the base module for all modules.

      Collection interface in java

      The Collection Interface in Java is the root interface(a kind of abstract class) in java.util package. It represents a group of objects, known as elements, and provides a unified way to manipulate and work with collections of data.

      Key Points About the Collection Interface

      Root Interface: The Collection interface is the root interface of all Collection objects in Java, except Map.
      Generic Interface: A generic interface, meaning it can work with any type of object (_e.g.,_ Collection<String>, Collection etc.).
      Common Methods: For example, we can use the following methods for both adding and deleting elements to an array: add(), remove (), size().
      Extends Iterable: The Collectioneuml; interface extends the Iterableinterface, and this means that all collections can be iterated over via either an iterator or the enhanced for-loop construct.



      1. public interface Collection<E>
      2. extends Iterable<E>


      Methods in Collection Interface:

       1.public Boolean add(Object obj)
      •  Used to add element in to the collection

       2.public Boolean addAll(Collection c)

      • Adds all the elements of c to the invoking collection. Returns true if the operation succeeded else returns false.

       3.public boolean isEmpty()
      • Returns true if collection is empty otherwise returns false.

       4.public boolean remove(Object obj)
      • Remove element from collection. Returns true if successful deletion.

      5.public boolean  removeAll(Collection c)
      • Removes all elements of a collection.

       6.public void clear()
      • Deletes total elements from collection

       7.public boolean contains(Object obj)
      • This method used to search an element

       8.public boolean containsAll(Collection c)
      • This method used to search an element in a collection

       9.public boolean retianAll(Collection c)
      • Used to delete all elements from a collection except specified one.

      10.public int size()
      • Returns total numbers of elements in a collection

      11.public Iterator iterator()
      • Returns iterator for the collection

      12.public boolean equals(Object obj)
      • Compares two collections

      13.public int hashCode()
      • Returns hashcode

      14.public Object[] toArray()
      • This method used to convert collection into array
      15.public Object[] toArray(Object[] obj)
      • Returns an array containing only those collection elements whose type matches that of array.

      Difference Between Collection and Collections:

      • Collection is the base interface for most of the classes.
      • Collections is the utility class.


      Java experience interview programming questions on Strings


      1.Java Basic interview Programs on Strings


       2.Reverse a String Without using String API?





       3.Sorting the String without using String API?


       4.Check String is palindrome or not? 

       5.How to split a String in java

      • #1: Java Program to split a String using String Split() method:
      • #2: Java Program to split a String using String Split() method and use for each loop to display  
      • #4: Java Program to split a String using String Split() method: Before splitting check whether deliminator is there or not
      • Check here for all questions on Splitting a string using string  method

      6.Removing Non Ascii character from a String?


       7.Java program to replace or remove a character in a string java



      8.Java Program to check two strings are anagrams or not

       9.Java program to return maximum occurring character in the input string

      10.Java program to count the number of vowels in a string.




      11.Java Program to find numbers of occurrences of given char in a string


      12.Java Program to count number of words in a String.
      13.How to reverse vowels in a string java
      14.Java program to remove vowels from string

       15.How to find count of uppercase letters in a string 

      Remove non ascii character from string

      #1: Java Program to Remove non ASCII chars from String

      1. package com.instanceofjava;
      2.  
      3. class RemoveNonASCIIString{
      4.  
      5. public static void main(String [] args){ 

      6.   String str = "Instance��of��java";
      7.   System.out.println(str);
      8.  
      9.   str = str.replaceAll("[^\\p{ASCII}]", "");
      10.  
      11.   System.out.println("After removing non ASCII chars:");
      12.  
      13.   System.out.println(str);
      14. }
      15. }
      Output:
      1. Instance��of��java
      2. After removing non ASCII chars:
      3. Instanceofjava


      #2: Java Program to Remove multiple spaces in a string

      1. package com.instanceofjava;
      2.  
      3. class RemoveNonASCIIString{
      4.  
      5. public static void main(String [] args){ 

      6.   String str = "Instance  of    java";
      7.   StringTokenizer st = new StringTokenizer(str, " ");
      8.  
      9.   StringBuffer sb = new StringBuffer();
      10.  
      11.   while(st.hasMoreElements()){
      12.          sb.append(st.nextElement()).append(" ");
      13.   }
      14.  
      15.      System.out.println(sb.toString().trim());
      16.     
      17.     }
      18. }
      19. }


      Output:
      1. Instance of java
      Select Menu