The world is in chaos, and Ma Huateng dare not hire Java engineers...

This article mainly introduces you to some Sao operations about Map in java8. The article introduces in detail through sample code, which has a certain reference learning value for everyone's study or work. Friends who need it, follow the editor below. Learn to learn

The content of this article is an introduction to some methods of using the new features of map. If there are any shortcomings, please add it! !

1. New features of map

The computational knowledge seekers of the following functional programming functions have replaced them with simple strings, and the parameters are nothing more than Key, value;

2.1 forEach

forEach iteration, equivalent to for loop

public static void main(String[] args) {
    
    
 HashMap<String, Object> hashMap = new HashMap<>();
 hashMap.put("key1","小熊猫");
 hashMap.put("key2","大熊猫");
 // 遍历
 hashMap.forEach((key,value) -> {
    
    
  System.out.println("key:"+key + " value: "+value);
 });
 
}

Output
key: key1 value: red panda
key: key2 value: panda

2.2 computeIfAbsentjava

If the value of key does not exist, use the function result instead;

There is an example of the value, but the old value is still used;

public static void main(String[] args) {
    
    
 HashMap<String, Object> hashMap = new HashMap<>();
 hashMap.put("key","无墨生香");
 // 如果key的值不存在就使用 函数值代替
 hashMap.computeIfAbsent("key",s-> {
    
     return "处处香";});
 // {key=无墨生香}
 System.out.println(hashMap);
}

If the value does not exist, the function value will be used instead of the new value;

public static void main(String[] args) {
    
    
 HashMap<String, Object> hashMap = new HashMap<>();
 // 如果key的值不存在就使用 函数值代替
 hashMap.computeIfAbsent("key",s-> {
    
     return "处处香";});
 // {key=处处香}
 System.out.println(hashMap);
}

2.3 computeIfPresent

If the key value exists, use the function value instead, if the function value is null, the key will be removed;

Examples of values ​​exist, function values ​​will be used instead of old values

public static void main(String[] args) {
    
    
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.put("key","无墨生香");
  // 如果key的值存在就使用函数值代替
  hashMap.computeIfPresent("key",(key,value)-> {
    
     return "处处香";});
  // {key4=处处香}
  System.out.println(hashMap);
}

Value does not exist example, is empty

public static void main(String[] args) {
    
    
  HashMap<String, Object> hashMap = new HashMap<>();
  // 如果key的值存在就使用函数值代替
  hashMap.computeIfPresent("key",(key,value)-> {
    
     return "处处香";});
  // {}
  System.out.println(hashMap);
}

If the function value is null, the key will be removed;

public static void main(String[] args) {
    
    
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.put("key","无墨生香");
  // 如果key的值存在就使用函数值代替
  hashMap.computeIfPresent("key",(key,value)-> {
    
     return null;});
  // {}
  System.out.println(hashMap);
}

2.4 putIfAbsent

When the key value exists, the value is not replaced; when the key value does not exist, the key value is replaced;

When the key has an example, it is still the old value;

public static void main(String[] args) {
    
    
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.put("key","无墨生香");
  hashMap.putIfAbsent("key","处处香");
  //{key=无墨生香}
  System.out.println(hashMap);
}

When the key does not exist, it is actually a put operation;

public static void main(String[] args) {
    
    
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.putIfAbsent("key","处处香");
  //{key=处处香}
  System.out.println(hashMap);
}

2.5 getOrDefault

Get the value when the key value exists, otherwise get the specified default value;

Key value existence example

public static void main(String[] args) {
    
    
   HashMap<String, Object> hashMap = new HashMap<>();
   hashMap.put("key","无墨生香");
   //无墨生香
   System.out.println(hashMap.getOrDefault("key","处处香"));
 }

Key value does not exist example

public static void main(String[] args) {
    
    
   HashMap<String, Object> hashMap = new HashMap<>();
   //处处香
   System.out.println(hashMap.getOrDefault("key","处处香"));
 }

2.6 merge

If the key value does not exist, it will be replaced with a new value. If the key value exists, the function value will be used to replace the old value. When the function value is empty, the key will be removed;

If the value of key does not exist, it will be replaced with a new value (the second parameter)

public static void main(String[] args) {
    
    
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.merge("key","处处香",(key,value) -> {
    
     return "点击在看";});
  //{key=处处香}
  System.out.println(hashMap);
}

If the key value exists, the function value will be used to replace the old value; the new value (the second parameter) does not work

public static void main(String[] args) {
    
    
   HashMap<String, Object> hashMap = new HashMap<>();
   hashMap.put("key","无墨生香");
   hashMap.merge("key","处处香",(key,value) -> {
    
     return "点击在看";});
   //{key=点击在看}
   System.out.println(hashMap);
 }

When the function value is empty, the key will be removed;

public static void main(String[] args) {
    
    
   HashMap<String, Object> hashMap = new HashMap<>();
   hashMap.put("key","无墨生香");
   hashMap.merge("key","处处香",(key,value) -> {
    
     return null;});
   //{}
   System.out.println(hashMap);
 }

to sum up

So far this article about some operations of Map in java8 is introduced. For more related content of Map operations in java8, please pay attention to the previous articles of the editor or continue to browse the related articles below. Hope you will support the editor a lot in the future!

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/115265566