![]() LinkedHashSet lhs = new LinkedHashSet(Collection obj) It is used to initialize the capacity of the Linked hash set to the given integer value capacity and the specified load factor. LinkedHashSet lhs = new LinkedHashSet(int capacity, float loadFactor) It is used to initialize the capacity of the Linked hash set to the given integer value capacity. LinkedHashSet lhs = new LinkedHashSet(int capacity) It is used to construct a default LinkedHashSet. Here, the first parameter is capacity and the second parameter is loadFactor. Notice, the part new LinkedHashSet(8,0.75). Syntax : LinkedHashSet numbers = new LinkedHashSet(8, 0.75) LinkedHashSet is called an ordered Collection because it is a guarantee for the insertion order of elements. LinkedHashSet supports only one null value. LinkedHashSet is not a synchronized class. The LinkedHashSet internally follows hashtable + double linked list structures. LinkedHashSet is the implementation class of the Set interface which is also used to store a group of individual objects but duplicate values are not allowed. ![]() Return "item: " + item + " price: " + price Return (pp.item.equals (em) & pp.price = this.price) ("Does set contains key? " + ntains (key)) HashSet Example with Complex Data type in Java: import Example to demonstrate HashSet Collection in Java import java.util.* Object clone(): This method returns a shallow copy of the HashSet.boolean isEmpty(): Returns true if there is no element present in the Set.If the object has been found it returns true or else false. boolean contains(E obj): It checks whether the specified Object o is present in the list or not.void clear(): It removes all the elements from the list.int size(): It gives the number of elements of a Set.boolean remove(E obj): It removes the specified Object o from the Set.boolean add(E obj): It adds the element e to the list.It is used to initialize the hash set by using the elements of the collection obj. HashSet hs = new HashSet(Collection obj) It is used to initialize the capacity of the hash set to the given integer value capacity and the specified load factor. HashSet hs = new HashSet(int capacity, float loadFactor) It is used to initialize the capacity of the hash set to the given integer value capacity. It is used to construct a default HashSet. This means, that whenever our hash set is filled by 60%, the elements are moved to a new hash table of double the size of the original hash table. loadFactor – The load factor of this hash set is 0.6.capacity – The capacity of this hash set is 8.Here, the first parameter is capacity, and the second parameter is the load factor. Here, we have created a hash set named numbers. Syntax : HashSet numbers = new HashSet(8, 0.75) ![]() HashSet is called an unordered collection because it is not guaranteed for the insertion order of elements. HashSet internally follows a hashtable structure where all the elements are stored using a hashing technique which will improve the performance by reducing the waiting time. The HashSet is the implementation class of the Set interface which is also used to store a group of individual objects but duplicate values are not allowed. These classes are defined in the Collections framework and implement the Set interface. In order to use functionalities of the Set interface, we can use these classes: Since Set is an interface, we cannot create objects from it. Unlike the List interface, sets cannot contain duplicate elements. The Set interface of the Java Collections framework provides the features of the mathematical set in Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |