Home >Java >javaTutorial >What are the new features of Java Map collections?

What are the new features of Java Map collections?

WBOY
WBOYforward
2023-05-08 18:22:171094browse

Explanation

1. If there is no key-value pair corresponding to Key in the Map, the result of PutKey and bringing Key into the function operation is Value key-value pair; if If the Key exists, the Put operation is ignored.

Delete the situation where the key-value pair in the Map is equal to the parameters Key and Value.

2. Get the Value of the specified Key from the Map. If not, return the specified default value.

Set the Value of the specified Key in the Map to a new value that is the result of calculating the existing value and the transmitted value through the function.

Example

Map<Integer, String> map = new HashMap<>();
 
for (int i = 0; i < 10; i++) {
    // 与老版不同的是,putIfAbent() 方法在 put 之前,
    // 会判断 key 是否已经存在,存在则直接返回 value, 否则 put, 再返回 value
    map.putIfAbsent(i, "val" + i);
}
 
// forEach 可以很方便地对 map 进行遍历操作
map.forEach((key, value) -> System.out.println(value));

What is Java

Java is an object-oriented programming language that can write desktop applications, Web applications, distributed systems and embedded system applications.

The above is the detailed content of What are the new features of Java Map collections?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete