Java Set Interface And Methods

  • Set is 1 of the collection framework interface which extends source Collections interface.
  • Set collection tin strength out non incorporate duplicate elements.
  • Set interface has all methods which are inherited from Collection interface amongst particular restriction of non allowing duplicate elements.
  • If 2 Set interface contains same elements thus 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.
Java Set Interface hierarchy
Important Methods of Set Interface
  • boolean add(E e) : It volition insert specified chemical gene 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 introduce inwards set.
  • void clear() : It volition take away all elements from set.
  • boolean contains(Object o) : It volition provide truthful if  given chemical gene 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 cheque 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 gene 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 example volition demonstrate yous 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;  populace course of report SetInterfaceExample {  populace 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 detail inwards laid equally laid tin strength out non convey duplicate items.   s1.add("Item1");   s1.add("Item2");   s1.add("Item1");   s1.add("Item3");   s1.add("Item4");   // Print set. It volition demonstrate entirely four items equally laid tin strength out 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 : ");   spell (iterator.hasNext()) {    String chemical gene = (String) iterator.next();    System.out.println(element);   }    // Remove detail from set.   s1.remove("Item3");   // Set items later 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 strength out role coffee laid to shop non duplicate items.


More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post