Thursday, February 25, 2016

Java Gear Upward Interface As Well As Methods

| Thursday, February 25, 2016
  • Set is 1 of the collection framework interface which extends origin Collections interface.
  • Set collection tin non comprise duplicate elements.
  • Set interface has all methods which are inherited from Collection interface amongst special restriction of non allowing duplicate elements.
  • If 2 Set interface contains same elements together with then both are equal.
  • Set interface is implemented past times LinkedHashSet, HashSet classes together with extended past times SortedSet interface which is implemented past times TreeSet.
Set is 1 of the collection framework interface which extends origin Collections interface Java Set Interface And Methods
Important Methods of Set Interface
  • boolean add(E e) : It volition insert specified chemical cistron inwards laid if it is non available inwards set.
  • boolean addAll(Collection<? extends E> c) : It volition add together all elements of specified collection inwards laid if non already acquaint inwards set.
  • void clear() : It volition take away all elements from set.
  • boolean contains(Object o) : It volition provide truthful if  given chemical cistron is available inwards set.
  • boolean containsAll(Collection<?> c) : It volition provide truthful if all the elements of given collection are available inwards set.
  • boolean equals(Object o) :It volition compare specified object amongst laid to banking concern jibe equality.
  • boolean isEmpty() : It volition provide truthful if laid is empty.
  • int hashCode() :It volition returns hash code value for this set.
  • Iterator<E> iterator() : It volition provide an iterator over the elements inwards set.
  • boolean remove(Object o) : It volition take away specified chemical cistron from the laid if it is present.
  • boolean removeAll(Collection<?> c) : It volition take away all those elements from laid which are specified inwards collection.
  • int size() : It volition provide size of Set.
Below given Set interface event volition demo y'all demo of Set interface's few of import methods.

Java Set Interface Example
package JAVAExamples;  import java.util.HashSet; import java.util.Iterator; import java.util.Set;  world cast SetInterfaceExample {  world static void main(String args[]) {   Set s1 = novel HashSet();   // Check initial size of set.   System.out.println("Initial size of laid : " + s1.size());   // Add items inwards set. Try to add together duplicate exceptional inwards laid every bit laid tin non convey duplicate items.   s1.add("Item1");   s1.add("Item2");   s1.add("Item1");   s1.add("Item3");   s1.add("Item4");   // Print set. It volition demo solely iv items every bit laid tin non concur duplicate items.   System.out.println("Set items are : " + s1);   // Check if laid is empty. It volition provide false.   System.out.println("Set is empty? : " + s1.isEmpty());    // access laid items through Iterator.   Iterator iterator = s1.iterator();                 System.out.println("Set items are : ");   piece (iterator.hasNext()) {    String chemical cistron = (String) iterator.next();    System.out.println(element);   }    // Remove exceptional from set.   s1.remove("Item3");   // Set items afterwards removing Item3 from set.   System.out.println("Now Set items are : " + s1);   // Get laid size.   System.out.println("Set size is : " + s1.size());  } }

Output :
Initial size of laid : 0 Set items are : [Item1, Item2, Item3, Item4] Set is empty? : imitation Set items are :  Item1 Item2 Item3 Item4 Now Set items are : [Item1, Item2, Item4] Set size is : 3

This way, You tin role coffee laid to shop non duplicate items.

Related Posts