Prev Next

Java / Set and its implementations

1. Java Collection:How to convert an Array to a Set?

One of the HashSet overloaded constructors accept ArrayList as an argument to create Set out of it. Passing the AsList method view of the array to the HashSet constructor, the Set can be created. Set mySet = new HashSet(Arrays.asList(1,4,3,4,4));

Read full answer

2. How HashSet checks for duplicates in Java?

When you put an object into a HashSet, it uses hashcode value to determine where to put the object in the set. It also compares the objects hashcode value to other object's hashcode in the hashset. But two objects having same hashcode might not be equal. If the hashcode of two objects are equal t...

Read full answer

3. What are the Set implementations in the Java Collections API?

EnumSet. HashSet. LinkedHashSet. TreeSet. The above classes are defined in java.util package.

Read full answer

4. How do you calculate the intersection of two HashSet in Java?

Use retainAll method that retains only the elements in the set that are contained in the specified collection (optional operation). In other words, removes from this set all of its elements that are not contained in the specified collection. If the specified collection is also a set, this operati...

Read full answer

5. How do you find the difference of of two sets.

Using removeAll method we can find the difference of two sets.

Read full answer

6. Difference between HashSet and TreeSet in Java.

HashSet stores the object in random order. There is no guarantee that the element we inserted first in the HashSet will be printed first in the output . TreeSet maintains the elements in sorted order. HashSet can store null object while TreeSet does not allow null object. If one try to store null...

Read full answer

7. Difference between HashSet and LinkedHashSet in Java collection.

A LinkedHashSet is ordered as it maintains a doubly-linked List across all elements. When you iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted.

Read full answer

8. What is LinkedHashSet in Java?

LinkedHashSet is the Hashtable and linked list implementation of the Set interface with predictable iteration order. The linked list defines the iteration ordering, which is the order in which elements were inserted into the set. Insertion order is not affected if an element is re-inserted into t...

Read full answer

9. Why do we need LinkedHashSet while we already have HashSet and TreeSet?

HashSet does not maintain order and returns random order every time we retrieve elements.In TreeSet elements are naturally sorted but there is increased cost associated to it. LinkedHashSet maintains the order of elements without incurring increased cost. LinkedHashSet is introduced in JDK 1.4 wh...

Read full answer

10. What is Initial capacity and load factor in Set implementations?

The capacity is the number of buckets in the set, and the initial capacity is the capacity at the time HashSet is created. Each bucket represent a key value pair. The load factor is a measure of how full the Hash table is allowed to get before its capacity is automatically increased.

Read full answer

11. What is the default value of initial capacity and load factor in Set?

The default value of initial capacity and load factor is 16 and 0.75f respectively.

Read full answer

12. How LinkedHashSet internally works in Java?

LinkedHashSet is an extended version of HashSet. HashSet does not maintain order while LinkedHashSet maintains insertion order. HashSet uses HashMap object internally to store its elements while LinkedHashSet uses LinkedHashMap object internally. There are 4 constructors in LinkedHashSet class. A...

Read full answer

13. How does LinkedHashSet maintain unique Elements in Java?

LinkedHashSet inherits the uniqueness property from LinkedHashMap as internally LinkedHashMap gets created indirectly when LinkedHashSet is created.

Read full answer

14. Which Java Collection maintained insertion order and allow no duplicates?

LinkedHashSet.

Read full answer

15. Compare LinkedHashSet, TreeSet and HashSet in Java.

Implementation: LinkedHashSet is backed by linked list and LinkedHashMap, TreeSet is implemented as TreeMap till Java 5 and now using NavigableMap from Java 6 onward, and HashSet is also backed by HashMap in Java. Synchronization: HashSet, TreeSet, and LinkedHashSet are not synchronized. Ordering...

Read full answer

16. How HashSet works internally in Java?

When HashSet object is created, it creates a HashMap object internally. When an element is passed to Set, it is added as a key in the HashMap by add(Element e) method. As HashMap maintains key value pair, a value needs to be associated to the key. Java uses a Dummy value (new Object) which is cal...

Read full answer

17. How do I update an existing object at HashSet in Java?

Perform remove before adding the element into the HashSet. if(!myHashSet.add(myObject)) { myHashSet.remove(myObject); myHashSet.add(myObject); }

Read full answer

18. How does HashSet handles collision in Java?

Java HashSet handle Hash collisions automatically, however it is important to override both the equals and the hashcode methods, as both methods are utilized by Set to differentiate duplicate or unique entries.

Read full answer

19. Explain CopyOnWriteArraySet in Java collection.

CopyOnWriteArraySet is a Set implementation backed up by a copy-on-write array. All mutative operations, such as add, set, and remove, are implemented by making a new copy of the array; no locking is ever required. CopyOnWriteArraySet is best suited as read-only collection whose size is small eno...

Read full answer

20. What copy technique internally used by HashSet clone() method?

To create a clone or copy of the Set object, HashSet internally uses shallow copy in clone() method, the elements themselves are not cloned.

Read full answer

21. How do you retrieve a element from HashSet?

There is no get method in HashSet as Set is not index based. Use the contains method to determine if the object exists or not, if exists use the Iterator to retrieve element.

Read full answer

22. What is Collections.emptySet() in Java?

Collections.emptySet() returns the empty immutable Set, not containing null.

Read full answer

23. Does TreeSet use hashcode method?

TreeSet does NOT use hashCode. It uses either compareTo or the Comparator passed to the constructor. This is used by methods like contains to find objects in the set.

Read full answer

«
»

Comments & Discussions