Automation Using Selenium Webdriver
Showing posts with label Collections Set in Java. Show all posts
Showing posts with label Collections Set in Java. Show all posts

Tuesday, 27 December 2016

Collections Set in Java

Collections:
  • Collections in java is a framework that provides an architecture to store and display the data.
  • Collections API provides interfaces and classes.
  • Using Collection we can perform operations like searching,sorting,insertion,deletion,manipulation.
  • Collections is nothing but group of different type of objects together as a single entity is also known as Collections.
  • Collections are growable Nature.
  • Collection Framework having Interfaces like Set,List,Map.Every Interface having classes.
Why Collection Came?
  • Arrays are fixed in size.we can't increase size of Arrays.that's y only collections came.collections having growable nature.we can increase size.
Set Interface:
  • A set is a collection that having only unique elements.Duplicate won't be there.
  • Set having no index.
  • Set allows only one null value.
  • Set having classes like :
  • HashSet
  • LinkedHashMap
  • TreeSet

HashSet:

HashSet having only unique elements.
HashSet having no Order.
HashSet allows only one null value.

Program for HashSet:
package.com.instanceofjava;
import.java.util.HashSet;

public class A{
public static void main(String args[]){

HashSet hashset=new HashSet();

hashset.add("Indhu");
hashset.add("Indhu");
hashset.add("Sindhu");
hashset.add("swathi");
hashset.add(null);
hashset.add(null);
hashset.add("Lavs");
Iterator it=hashset.iterator();
while(it.hasNext()){
System.out.println(it.next);
}
}
}
Output:
Indhu
null
Lavs
Sindhu
swathi

LinkedHashMap:
  • LinkedHashMap allows only unique elements.
  • LinkedHasMap allows Insertion order.
  • LinkedHashMap allows only one null value.

Program for LinkedHashSet;

package.com.instanceofjava;
import.java.util.LinkedHashSet;

public class testSet{
public static void main(String args[]){

LinkedHasMap linkedHashMap=new LinkedHasMap();

linkedHashMap.add("Indhu');
linkedHashMap.add("Indhu");
linkedHashMap.add("Sindhu");
linkedHashMap.add("Bindhu");
linkedHashMap.add(null);
linkedHashMap.add(null);

Iterator it=linkedHashMap.iterator();
while(it.hasNext()){
System.out.println(it.next);
}
}
}

Output:
Indhu
Sindhu
Bindhu
null


TreeSet:
  • TreeSet Allows only Unique elements.
  • Treeset having sorted order
  • TreeSet doesn't allow  any null value.
Program for TreeSet:

package.com.instanceofjava;
import.java.util.TreeSet;

Public class testSet2{

public static void main(String args[]){
TreeSet treeSet=new treeSet();
treeSet.add("Sindhu");
treeSet.add("Sindhu");
treeSet.add("Indhu");
treeSet.add("Bindhu');
Iterator it=treeSet.iterator();
while(it.hasNext()){
System.out.println(it.next);
}
}
}

Output:
Bindhu
Indhu
Sindhu
  • in Set interface if you want to add elements you can use add() method.
  • If you want to remove you can use remove() method.