This example shows how to create new objects of LinkedHashSet in Java using the constructor. This example also shows how to use various LinkedHashSet constructors.
How to create new objects of LinkedHashSet in Java?
The LinkedHashSet class in Java provides several constructors using which we can create new objects of it as given below.
How to create a new and empty LinkedHashSet object?
The default LinkedHashSet constructor creates a new and empty set object.
1 |
public LinkedHashSet() |
The set created using this constructor will have a default initial capacity of 16 and a load factor of 0.75.
1 |
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<Integer>(); |
The above statement will create a new LinkedHashSet object of type Integer.
How to create a new LinkedHashSet object with custom initial capacity and load factor?
The default initial capacity of the LinkedHashSet is 16. If you want to change that, you can use the below given overloaded constructor provided by the LinkedHashSet class.
1 |
public LinkedHashSet(int initialCapacity) |
It creates a new set object having the specified initial capacity and a default load factor of 0.75.
1 |
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<Integer>( 25 ); |
If you want to specify both, the initial capacity and a load factor, you can use below given constructor.
1 |
public LinkedHashSet(int initialCapacity, float loadFactor) |
It creates a new LinkedHashSet object with the specified initial capacity and load factor.
1 |
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<Integer>( 25, 0.8f ); |
The above statement will create a new LinkedHashSet object with 25 initial capacity and load factor 0f 0.8.
How to create a new LinkedHashSet object from another collection object?
The LinkedHashSet class offers a constructor that accepts a collection object.
1 |
public LinkedHashSet(Collection<? extends E> collection) |
It creates a new linked hash set object having the same elements as the specified collection object. The initial capacity, in this case, will be large enough to hold all the elements of the specified collection and the load factor will be the default i.e. 0.75.
We can convert any collection to the linked hash set object using this constructor. The below given example shows how to convert ArrayList to LinkedHashSet object using this constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
List<String> listWeekDays = new ArrayList<String>(); listWeekDays.add("Monday"); listWeekDays.add("Tuesday"); listWeekDays.add("Wednesday"); listWeekDays.add("Thursday"); listWeekDays.add("Friday"); listWeekDays.add("Monday"); /* * To convert a collection object to the LinkedHashSet, * use the constructor. */ LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>( listWeekDays ); System.out.println( "LinkedHashSet contains: " + linkedHashSet); |
Output
1 |
LinkedHashSet contains: [Monday, Tuesday, Wednesday, Thursday, Friday] |
As we can see from the output above, the element “Monday” was only added once from the ArrayList object. Since the LinkedHashSet class is an implementation of the Set interface, it does not allow duplicate elements.
This example is a part of the Java LinkedHashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet