How to use java to quickly create Map
Today, I will talk to you about how to use java to quickly create Map. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.
To create a Map quickly, do not use frequent new, the fastest way is to use Guava, use ImmutableMap.of("a", 1, "b", 2, "c", 3);
Guava
Map left = ImmutableMap.of("a", 1, "b", 2, "c", 3);
java9
Map map = Map.of(1, "A", 2, "B", 3, "C");
More than 10 groups will not support it, so it will be like this:
Map.ofEntries( Map.entry( 1, false ), Map.entry( 2, true ), Map.entry( 3, false ), Map.entry( 4, true ), Map.entry( 5, false ), Map.entry( 6, true ), Map.entry( 7, false ), Map.entry( 8, true ), Map.entry( 9, false ), Map.entry( 10, true ), Map.entry( 11, false ));
anonymous
Map mymap = new HashMap() { { put(1, "one"); put(2, "two"); }};Collections.unmodifiableMap(new HashMap() { { put(0, "zero"); put(1, "one"); put(2, "two"); put(3, "three"); put(4, "four"); put(5, "five"); put(6, "six"); put(7, "seven"); put(8, "eight"); put(9, "nine"); put(10, "ten"); put(11, "eleven"); put(12, "twelve"); } After reading the above, do you have any further understanding of how to use java to quickly create maps? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.