This example shows how to add elements to LinkedHashSet in Java. This example also shows how to add elements to LinkedHashSet object using the add and addAll methods.
How to add elements to LinkedHashSet in Java?
There are a couple of ways using which we can add elements to LinkedHashSet in Java.
1. Add individual elements using the add method
The add
method of the LinkedHashSet class adds the specified element to the linked hash set object.
1 |
public boolean add(E e) |
The add
method returns true if the element was not present in the set object and it was added. It returns false if the element exists in the LinkedHashSet object and hence not added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.LinkedHashSet; public class LinkedHashSetAddElementsExample { public static void main(String[] args) { LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); /* * To add elements to the LinkedHashSet object, use * the add method */ //this will add element "red" to the set and return true System.out.println( lhSetColors.add("red") ); //this will return false as element "red" already exists in the set System.out.println( lhSetColors.add("red") ); System.out.println("LinkedHashSet contains: " + lhSetColors); } } |
Output
1 2 3 |
true false LinkedHashSet contains: [red] |
As the LinkedHashSet is an implementation of the Set interface, it does not allow duplicate elements.
2. Add all elements of another collection using the addAll method
The addAll
method of the LinkedHashSet class adds all the elements of the specified collection object to this set object.
1 |
boolean addAll(Collection<? extends E> collection) |
The addAll
method returns true if this set was changed due to this method call, false otherwise.
The below given example shows how to add all elements of ArrayList object to the LinkedHashSet object using the addAll
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
List<String> listColors = new ArrayList<String>(); listColors.add("red"); listColors.add("green"); LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("green"); lhSetColors.add("blue"); lhSetColors.add("white"); /* * To add all elements of another collection to this * set object, use the addAll method */ lhSetColors.addAll(listColors); System.out.println("LinkedHashSet contains: " + lhSetColors); |
Output
1 |
LinkedHashSet contains: [green, blue, white, red] |
As we can see from the output, the only elements which were not present in the LinkedHashSet were added to it. The element “green” was already present in the LinkedHashSet, so it was not added again from the ArrayList object.
Instead of an ArrayList, you can also add all elements of a LinkedList or TreeSet to the LinkedHashSet using the addAll
method.
How to add objects of custom class to LinkedHashSet?
Let’s first see an example of adding a custom class objects to the LinkedHashSet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Student{ private Integer id; private String name; public Student(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + "=>" + this.name + "]"; } } public class LinkedHashSetAddElementsExample { public static void main(String[] args) { LinkedHashSet<Student> lhSetStudents = new LinkedHashSet<Student>(); lhSetStudents.add(new Student(1, "John")); lhSetStudents.add(new Student(1, "John")); System.out.println(lhSetStudents); } } |
Output
1 |
[[1=>John], [1=>John]] |
As we can see from the output, the duplicate objects were added to the LinkedHashSet object. The reason is, the add
method (and the addAll
method) of the LinkedHashSet class relies on the equals
and hashCode
methods of the elements to check if the element already exists in the set or not.
Since the Student class has not overridden these methods, the methods inherited from the Object class were used that compares object references, not the actual object content. That is the reason the add
method could not find the duplicate Student object the second time we called it.
To solve the problem of duplicate elements being added to the LinkedHashSet, we need to override the equals
and hashCode
methods in the Student class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
class Student{ private Integer id; private String name; public Student(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + "=>" + this.name + "]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } public class LinkedHashSetAddElementsExample { public static void main(String[] args) { LinkedHashSet<Student> lhSetStudents = new LinkedHashSet<Student>(); lhSetStudents.add(new Student(1, "John")); lhSetStudents.add(new Student(1, "John")); System.out.println(lhSetStudents); } } |
1 |
[[1=>John]] |
Tip: While working with any type of Set of custom class objects, always override the equals
and hashCode
methods in your custom class to avoid duplicate elements being added problem.
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