Summary of some operations of Map in java8

  • 2021-08-12 02:56:19
  • OfStack

1 Preface

This article is about map new features of 1 method on the use of the introduction, if there are deficiencies welcome to add! !

2 new features of map

The computational knowledge of the following functional programming functions is replaced by simple strings, and the parameters are nothing more than Key and 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"," Red panda ");
  hashMap.put("key2"," Giant panda ");
  //  Traversal 
  hashMap.forEach((key,value) -> {
   System.out.println("key : "+key + " value: "+value);
  });

 }

Output

key: key1 value: Red Panda
key: key2 value: Giant Panda

2.2 computeIfAbsentjava

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

There are examples of values, but the old values are still used;


 public static void main(String[] args) {
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.put("key"," Ink-free fragrance ");
  //  If key Use if the value of does not exist   Function value substitution 
  hashMap.computeIfAbsent("key",s-> { return " Fragrance everywhere ";});
  // {key= Ink-free fragrance }
  System.out.println(hashMap);
 }

There is no example of the value, and the function value is used instead of the new value;


 public static void main(String[] args) {
  HashMap<String, Object> hashMap = new HashMap<>();
  //  If key Use if the value of does not exist   Function value substitution 
  hashMap.computeIfAbsent("key",s-> { return " Fragrance everywhere ";});
  // {key= Fragrance everywhere }
  System.out.println(hashMap);
 }

2.3 computeIfPresent

If the value of key exists, the function value is used instead, and if the function value is null, key is removed;

Value exists, and the function value is used instead of the old value


  public static void main(String[] args) {
    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("key"," Ink-free fragrance ");
    //  If key If the value of exists, use the function value instead 
    hashMap.computeIfPresent("key",(key,value)-> { return " Fragrance everywhere ";});
    // {key4= Fragrance everywhere }
    System.out.println(hashMap);
  }

Value does not have an example, it is empty


  public static void main(String[] args) {
    HashMap<String, Object> hashMap = new HashMap<>();
    //  If key If the value of exists, use the function value instead 
    hashMap.computeIfPresent("key",(key,value)-> { return " Fragrance everywhere ";});
    // {}
    System.out.println(hashMap);
  }

If the function value is null, key is removed;


  public static void main(String[] args) {
    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("key"," Ink-free fragrance ");
    //  If key If the value of exists, use the function value instead 
    hashMap.computeIfPresent("key",(key,value)-> { return null;});
    // {}
    System.out.println(hashMap);
  }

2.4 putIfAbsent

When the value of key exists, it does not replace the value; Replacing the value of key when the value of key does not exist;

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


   public static void main(String[] args) {
     HashMap<String, Object> hashMap = new HashMap<>();
     hashMap.put("key"," Ink-free fragrance ");
     hashMap.putIfAbsent("key"," Fragrance everywhere ");
     //{key= Ink-free fragrance }
     System.out.println(hashMap);
   }

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


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

2.5 getOrDefault

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

Example of key Value Existence


  public static void main(String[] args) {
     HashMap<String, Object> hashMap = new HashMap<>();
     hashMap.put("key"," Ink-free fragrance ");
     // Ink-free fragrance 
     System.out.println(hashMap.getOrDefault("key"," Fragrance everywhere "));
   }

No sample exists for key value


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

2.6 merge

If the value of key does not exist, it will be replaced by a new value, if the value of key exists, the old value will be replaced by a function value, and when the function value is empty, key will be removed;

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


 public static void main(String[] args) {
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.put("key"," Ink-free fragrance ");
  //  If key Use if the value of does not exist   Function value substitution 
  hashMap.computeIfAbsent("key",s-> { return " Fragrance everywhere ";});
  // {key= Ink-free fragrance }
  System.out.println(hashMap);
 }
0

If the value of key exists, the old value will be replaced by the function value; The new value (Parameter 2) does not work


 public static void main(String[] args) {
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.put("key"," Ink-free fragrance ");
  //  If key Use if the value of does not exist   Function value substitution 
  hashMap.computeIfAbsent("key",s-> { return " Fragrance everywhere ";});
  // {key= Ink-free fragrance }
  System.out.println(hashMap);
 }
1

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


 public static void main(String[] args) {
  HashMap<String, Object> hashMap = new HashMap<>();
  hashMap.put("key"," Ink-free fragrance ");
  //  If key Use if the value of does not exist   Function value substitution 
  hashMap.computeIfAbsent("key",s-> { return " Fragrance everywhere ";});
  // {key= Ink-free fragrance }
  System.out.println(hashMap);
 }
2

Summarize


Related articles: