Home > Java > javaTutorial > body text

What are the new features of Java Map collections?

WBOY
Release: 2023-05-08 18:22:17
forward
1015 people have browsed it

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));
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!