Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to quickly create Map

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report