Build a Map from a List of keys and values in Java
This post will discuss how to build a Map from a List of keys and values in Java.
1. Java 8
Since Java 8, you can do this in a single line using Stream API. Since a stream is a sequence of elements and not a sequence of key-value pairs, you can’t directly construct a map out of it. You need to specify how to extract keys and values from elements of the stream using the Collectors.toMap() method.
|
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 |
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; 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); } public int getX() { return x; } public int getY() { return y; } } public class Main { public static void main(String[] args) { List<Point> points = Arrays.asList(Point.of(0, 1), Point.of(1, 2), Point.of(2, 3), Point.of(3, 4)); Map<Integer, Integer> map = points.stream() .collect(Collectors.toMap(Point::getX, Point::getY)); System.out.println(map); } } |
Output:
{0=1, 1=2, 2=3, 3=4}
The code will throw IllegalStateException on encountering a duplicate key. We can provide a merge function to resolve collisions between values associated with the same key, as shown 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 |
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; 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); } public int getX() { return x; } public int getY() { return y; } } public class Main { public static void main(String[] args) { List<Point> points = Arrays.asList(Point.of(0, 1), Point.of(1, 2), Point.of(1, 1), Point.of(2, 3)); Map<Integer, Integer> map = points.stream() .collect(Collectors.toMap(Point::getX, Point::getY, (oldValue, newValue) -> oldValue)); System.out.println(map); } } |
Output:
{0=1, 1=2, 2=3}
2. Using for loop
Here’s a version without streams (works with Java 8 and above). You can easily modify the code to handle duplicate keys.
|
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 |
import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; 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); } public int getX() { return x; } public int getY() { return y; } } public class Main { public static void main(String[] args) { List<Point> points = Arrays.asList(Point.of(0, 1), Point.of(1, 2), Point.of(2, 3), Point.of(3, 4)); Map<Integer, Integer> map = new HashMap<>(); for (Point point : points) { if (map.put(point.getX(), point.getY()) != null) { throw new IllegalStateException("Duplicate key"); } } System.out.println(map); } } |
Output:
{0=1, 1=2, 2=3, 3=4}
That’s all about building a Map from a List of keys and values 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 :)