Add contents of a List into a Set in Java
This post will discuss how to add the contents of a List into a Set in Java.
1. Using Set.addAll() method
The standard solution to add all elements of the specified collection to the set is using the addAll() method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { List<Integer> list = List.of(2, 4, 6, 8); Set<Integer> set = new HashSet<>(Set.of(1, 3, 5, 7)); set.addAll(list); System.out.println(set); } } |
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
Since a Set doesn’t permit duplicates, any common elements will be silently discarded.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { List<Integer> list = List.of(1, 2, 3, 4); Set<Integer> set = new HashSet<>(Set.of(1, 3, 5, 7)); set.addAll(list); System.out.println(set); } } |
Output:
[1, 2, 3, 4, 5, 7]
Note that the behavior of the addAll() method is undefined if the list is modified while the operation is in progress. Also, any mutable object present in the list is modified after calling the addAll() method, the changes are reflected in the set as well. This is because both list and set hold the same references of the objects inside them.
This behavior is demonstrated below. To handle this, pass a copy of each object to the Set.add() method, as discussed below.[3]
|
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 52 |
import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; class Point { int x, y; private Point(int x, int y) { this.x = x; this.y = y; } public static Point of(int x, int y) { return new Point(x, y); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } @Override public String toString() { return "{" + + x + ", " + y + '}'; } } public class Main { public static void main(String[] args) { List<Point> list = List.of(Point.of(1, 2), Point.of(3, 4)); Set<Point> set = new HashSet<>(Set.of(Point.of(1, 3), Point.of(5, 7))); set.addAll(list); System.out.println(set); Point pt = list.get(0); pt.x = 2; System.out.println(set); } } |
Output:
[{1, 2}, {3, 4}, {5, 7}, {1, 3}]
[{2, 2}, {3, 4}, {5, 7}, {1, 3}]
2. Using Apache Commons Collections
You can also use CollectionUtils.addAll() from by Apache Commons Collections that works similar to the Collections.addAll().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.apache.commons.collections4.CollectionUtils; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { List<Integer> list = List.of(2, 4, 6, 8); Set<Integer> set = new HashSet<>(Set.of(1, 3, 5, 7)); CollectionUtils.addAll(set, list); System.out.println(set); } } |
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
3. Using Set.add() method
With Java 8, you can use Stream API to call the add() method on the set for each list item.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { List<Integer> list = List.of(2, 4, 6, 8); Set<Integer> set = new HashSet<>(Set.of(1, 3, 5, 7)); list.stream().forEach(set::add); System.out.println(set); } } |
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
This approach faces the same issue as the addAll() method, i.e., both list and set hold the same references of the objects inside them. This can be easily handled by inserting a copy of each object into the set, instead of inserting the actual object itself.
|
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 52 53 54 55 |
import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; class Point { int x, y; // constructor public Point(int x, int y) { this.x = x; this.y = y; } // copy constructor public Point(Point pt) { this.x = pt.x; this.y = pt.y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } @Override public String toString() { return "{" + + x + ", " + y + '}'; } } public class Main { public static void main(String[] args) { List<Point> list = List.of(new Point(1, 2), new Point(3, 4)); Set<Point> set = new HashSet<>(Set.of(new Point(1, 3), new Point(5, 7))); list.stream().forEach(e -> set.add(new Point(e))); System.out.println(set); Point pt = list.get(0); pt.x = 2; System.out.println(set); } } |
Output:
[{1, 2}, {3, 4}, {5, 7}, {1, 3}]
[{1, 2}, {3, 4}, {5, 7}, {1, 3}]
That’s all about adding the contents of a List into a Set in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)