Course Hero Logo

Java - The Set Interface.pdf - 12/14/21 6:07 PM Java - The...

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 out of 3 pages.

Unformatted text preview: 12/14/21, 6:07 PM Java - The Set Interface Java - The Set Interface A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. The methods declared by Set are summarized in the following table − Sr.No. 1 Method & Description add( ) Adds an object to the collection. 2 clear( ) Removes all objects from the collection. 3 contains( ) Returns true if a specified object is an element within the collection. 4 isEmpty( ) Returns true if the collection has no elements. 5 iterator( ) Returns an Iterator object for the collection, which may be used to retrieve an object. 6 remove( ) Removes a specified object from the collection. 7 size( ) Returns the number of elements in the collection. Example Set has its implementation in various classes like HashSet, TreeSet, LinkedHashSet. Following is an example to explain Set functionality − import java.util.*; public class SetDemo { Live Demo public static void main(String args) { int count = {34, 22,10,60,30,22}; Set<Integer> set = new HashSet<Integer>(); try { for(int i = 0; i < 5; i++) { set.add(count[i]); } System.out.println(set); TreeSet sortedSet = new TreeSet<Integer>(set); System.out.println("The sorted list is:"); System.out.println(sortedSet); System.out.println("The First element of the set is: "+ (Integer)sortedSet.first()); System.out.println("The last element of the set is: "+ (Integer)sortedSet.last()); } catch(Exception e) {} } } 1/3 12/14/21, 6:07 PM Java - The Set Interface This will produce the following result − Output [34, 22, 10, 60, 30] The sorted list is: [10, 22, 30, 34, 60] The First element of the set is: 10 The last element of the set is: 60 Useful Video Courses Video Java Date And Time Online Training 16 Lectures 2 hours Malhar Lathkar More Detail Video Java Servlet Online Training 19 Lectures 5 hours Malhar Lathkar More Detail Video JavaScript Online Training 25 Lectures 2.5 hours Anadi Sharma More Detail Video Java Online Training 126 Lectures 7 hours Tushar Kale More Detail 2/3 12/14/21, 6:07 PM Java - The Set Interface Video Java Essential Training 119 Lectures 17.5 hours Monica Mittal More Detail Video Java Essentials Online Training 76 Lectures 7 hours Arnab Chakraborty More Detail 3/3 ...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture