Constructors and methods of LinkedHashSet in Java

There are four constructors defined for LinkedHashSet.

  • LinkedHashSet()
  • LinkedHashSet(Collection c)
  • LinkedHashSet(int initialCapacity)
  • LinkedHashSet(int initialCapacity, float loadFactor)

 

LinkedHashSet() – This is default constructor which is used to create new and empty LinkedHashSet with the default initial capacity (16) and load factor (0.75).

Internal implementation –

 public LinkedHashSet() {
        super(16, .75f, true);
    }

Example –

import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetExample {
	public static void main(String[] args) {

		// creating new LinkedHasSetHashSet using default constructor
		Set<String> linkedHashSetObj = new LinkedHashSet();
		linkedHashSetObj.add("ram");
		linkedHashSetObj.add("mohan");
		linkedHashSetObj.add("sohan");
		linkedHashSetObj.add("ram");
		linkedHashSetObj.add("ram");
		System.out.println(linkedHashSetObj);
	}
}

Output is – [ram, mohan, sohan]

LinkedHashSet(Collection c) – We can pass any type of collection classes which implements Collections interface and can convert it into LinkedHashSet. For example, we are going to convert ArrayList into LinkedHashSet in below example before that let’s see how this constructor has been defined in LinkedHashSet class.

public LinkedHashSet(Collection c) {
        super(Math.max(2*c.size(), 11), .75f, true);
        addAll(c);
    }
package linkedlistconstructor;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class LinkedHashSetConstructor4 {
	public static void main(String[] args) {
		// Converting ArrayList into LinkedHashSet using LinkedHashSet
		// constructor

		List<String> listObject = new ArrayList<>();

		listObject.add("ram");
		listObject.add("mohan");
		listObject.add("shyam");
		listObject.add("mohan");
		listObject.add("ram");

		Set<String> linkedHashSetObj = new LinkedHashSet<>(listObject);

		for (String str : linkedHashSetObj) {
			System.out.println(str);
		}
	}
}

Output is –


ram
mohan
shyam

LinkedHashSet(int initialCapacity) – We can create a new LinkedHashSet defining some different capacity than 16 using this constructor.

Internal implementation –


public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }

Example –

package linkedlistconstructor;

import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetConstructor2 {
	public static void main(String[] args) {

		// create LinkedHashSet with initial capacity 25
		Set<String> linkedHashSetObj = new LinkedHashSet<>(25);
		linkedHashSetObj.add("ram");
		linkedHashSetObj.add("mohan");
		linkedHashSetObj.add("sohan");
		linkedHashSetObj.add("ram");
		linkedHashSetObj.add("ram");
		for (String str : linkedHashSetObj) {
			System.out.println(str);
		}
	}
}

Output is –

ram
mohan
sohan

LinkedHashSet(int initialCapacity, float loadFactor) – We can also create a new LinkedHashSet with new capacity as well as new load factor, rather than 16 and 0.75.

Internal implementation –

public LinkedHashSet(int initialCapacity, float loadFactor) {
    super(initialCapacity, loadFactor, true);
}

Example –

package linkedlistconstructor;

import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetConstructor3 {
	public static void main(String[] args) {

		// create LinkedHashSet with initial capacity 25 and load factor 0.45
		Set<String> linkedHashSetObj = new LinkedHashSet<>(25, 0.45f);
		linkedHashSetObj.add("ram");
		linkedHashSetObj.add("mohan");
		linkedHashSetObj.add("sohan");
		linkedHashSetObj.add("ram");
		linkedHashSetObj.add("ram");
		for (String str : linkedHashSetObj) {
			System.out.println(str);
		}
	}
}

Output is –

ram
mohan
sohan

There are no methods defined in LinkedHashSet, all methods inherited from the super class.