Java HashMap Example - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Wednesday, February 22, 2017

Java HashMap Example

In previous post, I have talked about what is Java HashMap, It's constructors, It's methods, example on commonly used method of HashMap before Java 8, and traversal or iterate over HashMap

In this post I will discuss about methods of HashMap, was introduced in Java 8.

Here is the topic that will cover in this post.

  1. HashMap method getOrDefault
  2. HashMap method forEach
  3. HashMap method replaceAll
  4. HashMap method putIfAbsent
  5. HashMap method remove
  6. HashMap method replace
  7. HashMap method computeIfAbsent
  8. HashMap method computeIfPresent
  9. HashMap method compute
  10. HashMap method merge

HashMap method getOrDefault

package com.walking.techie;

import java.util.HashMap;

public class HashMapGetOrDefaultDemo {

  public static void main(String[] args) {
    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    System.out.println(httpStatus.getOrDefault(429, "Many Requests"));
    System.out.println(httpStatus.getOrDefault(420, "Default Message"));
  }
}
    

Output of above program is shown below:

Too Many Requests
Default Message

HashMap method forEach

package com.walking.techie;

import java.util.HashMap;
import java.util.function.BiConsumer;

public class HashMapForEachDemo {

  public static void main(String[] args) {
    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");
    MyBiConsumer action = new MyBiConsumer();
    httpStatus.forEach(action);

  }

  static class MyBiConsumer implements BiConsumer<Integer, String> {

    @Override
    public void accept(Integer key, String value) {
      System.out.println("Key : " + key + " is processed with value : " + value);
    }
  }
}

Output of above program is shown below:

Key : 422 is processed with value : Unprocessable Entity
Key : 424 is processed with value : Failed Dependency
Key : 504 is processed with value : Gateway Timeout
Key : 409 is processed with value : Conflict
Key : 429 is processed with value : Too Many Requests

HashMap method replaceAll

package com.walking.techie;

import java.util.HashMap;
import java.util.function.BiFunction;

public class HashMapReplaceAllDemo {

  public static void main(String[] args) {
    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    MyBiFunction action = new MyBiFunction();
    httpStatus.replaceAll(action);

    for (Integer code : httpStatus.keySet()) {
      System.out.println("Http Status Code : " + code + " Http Message : " + httpStatus.get(code));
    }
  }

  private static class MyBiFunction implements BiFunction<Integer, String, String> {

    @Override
    public String apply(Integer key, String value) {
      return key + "-->" + value;
    }
  }
}
    

Output of above program is shown below:

Http Status Code : 422 Http Message : 422-->Unprocessable Entity
Http Status Code : 424 Http Message : 424-->Failed Dependency
Http Status Code : 504 Http Message : 504-->Gateway Timeout
Http Status Code : 409 Http Message : 409-->Conflict
Http Status Code : 429 Http Message : 429-->Too Many Requests

HashMap method putIfAbsent

package com.walking.techie;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapPutIfAbsentDemo {

  public static void main(String[] args) {
    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    httpStatus.putIfAbsent(429, "Dependency failed.");
    httpStatus.putIfAbsent(401, "Unauthorized");

    Set<Entry<Integer, String>> entrySet = httpStatus.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
      System.out.println("Http Status Code : " + entry.getKey() + " Http Message : " + entry.getValue());
    }
  }
}
    

Output of above program is shown below:

Http Status Code : 401 Http Message : Unauthorized
Http Status Code : 422 Http Message : Unprocessable Entity
Http Status Code : 424 Http Message : Failed Dependency
Http Status Code : 504 Http Message : Gateway Timeout
Http Status Code : 409 Http Message : Conflict
Http Status Code : 429 Http Message : Too Many Requests

HashMap method remove

package com.walking.techie;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapRemoveDemo {

  public static void main(String[] args) {
    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    httpStatus.remove(429);
    // It will remove from map
    httpStatus.remove(422, "Unprocessable Entity");
    // It will not remove from map
    httpStatus.remove(504, "Gateway time over");
    Set<Entry<Integer, String>> entrySet = httpStatus.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
      System.out.println("Http Status Code : " + entry.getKey() + " Http Message : " + entry.getValue());
    }
  }
}
    

Output of above program is shown below:

Http Status Code : 424 Http Message : Failed Dependency
Http Status Code : 504 Http Message : Gateway Timeout
Http Status Code : 409 Http Message : Conflict

HashMap method replace

package com.walking.techie;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapReplaceDemo {

  public static void main(String[] args) {
    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    httpStatus.replace(429, "Many Requests");
    httpStatus.replace(504, "Gateway Timeout", "Timeout happened in Gateway");

    Set<Entry<Integer, String>> entrySet = httpStatus.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
      System.out.println("Http Status Code : " + entry.getKey() + " Http Message : " + entry.getValue());
    }
  }
}
    

Output of above program is shown below:

Http Status Code : 422 Http Message : Unprocessable Entity
Http Status Code : 424 Http Message : Failed Dependency
Http Status Code : 504 Http Message : Timeout happened in Gateway
Http Status Code : 409 Http Message : Conflict
Http Status Code : 429 Http Message : Many Requests

HashMap method computeIfAbsent

package com.walking.techie;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;

public class HashMapComputeIfAbsentDemo {

  public static void main(String[] args) {

    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    httpStatus.computeIfAbsent(401, new MyFunction());

    Set<Entry<Integer, String>> entrySet = httpStatus.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
      System.out.println("Http Status Code : " + entry.getKey() + " Http Message : " + entry.getValue());
    }
  }

  private static class MyFunction implements Function<Integer, String> {

    @Override
    public String apply(Integer integer) {
      return "Unauthorized";
    }
  }
}
Http Status Code : 401 Http Message : Unauthorized
Http Status Code : 422 Http Message : Unprocessable Entity
Http Status Code : 424 Http Message : Failed Dependency
Http Status Code : 504 Http Message : Gateway Timeout
Http Status Code : 409 Http Message : Conflict
Http Status Code : 429 Http Message : Too Many Requests

HashMap method computeIfPresent

package com.walking.techie;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiFunction;

public class HashMapComputeIfPresentDemo {

  public static void main(String[] args) {

    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    httpStatus.computeIfPresent(424, new MyBiFunction());

    Set<Entry<Integer, String>> entrySet = httpStatus.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
      System.out.println("Http Status Code : " + entry.getKey() + " Http Message : " + entry.getValue());
    }
  }

  private static class MyBiFunction implements BiFunction<Integer, String, String> {

    @Override
    public String apply(Integer key, String value) {
      return key + "--->" + value;
    }
  }
}

Output of above program is shown below:

Http Status Code : 422 Http Message : Unprocessable Entity
Http Status Code : 424 Http Message : 424--->Failed Dependency
Http Status Code : 504 Http Message : Gateway Timeout
Http Status Code : 409 Http Message : Conflict
Http Status Code : 429 Http Message : Too Many Requests

HashMap method compute

package com.walking.techie;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiFunction;

public class HashMapComputeDemo {

  public static void main(String[] args) {

    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    httpStatus.compute(422, new MyBiFunction());

    Set<Entry<Integer, String>> entrySet = httpStatus.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
      System.out.println("Http Status Code : " + entry.getKey() + " Http Message : " + entry.getValue());
    }
  }

  private static class MyBiFunction implements BiFunction<Integer, String, String> {

    @Override
    public String apply(Integer key, String value) {
      return key + "--->" + value;
    }
  }
}
    

Output of above program is shown below:

Http Status Code : 422 Http Message : 422--->Unprocessable Entity
Http Status Code : 424 Http Message : Failed Dependency
Http Status Code : 504 Http Message : Gateway Timeout
Http Status Code : 409 Http Message : Conflict
Http Status Code : 429 Http Message : Too Many Requests

HashMap method merge

package com.walking.techie;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiFunction;

public class HashMapMergeDemo {

  public static void main(String[] args) {
    HashMap<Integer, String> httpStatus = new HashMap<>();
    httpStatus.put(424, "Failed Dependency");
    httpStatus.put(422, "Unprocessable Entity");
    httpStatus.put(429, "Too Many Requests");
    httpStatus.put(504, "Gateway Timeout");
    httpStatus.put(409, "Conflict");

    MyBiFunction myBiFunction = new MyBiFunction();
    // key is not present
    httpStatus.merge(401, "Unauthorized", myBiFunction);

    // key is present
    httpStatus.merge(429, "So many requests", myBiFunction);

    Set<Entry<Integer, String>> entrySet = httpStatus.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
      System.out.println("Http Status Code : " + entry.getKey() + " Http Message : " + entry.getValue());
    }
  }

  private static class MyBiFunction implements BiFunction<String, String, String> {

    @Override
    public String apply(String key, String value) {
      return key + "--->" + value;
    }
  }
}
    

Output of above program is shown below:

Http Status Code : 401 Http Message : Unauthorized
Http Status Code : 422 Http Message : Unprocessable Entity
Http Status Code : 424 Http Message : Failed Dependency
Http Status Code : 504 Http Message : Gateway Timeout
Http Status Code : 409 Http Message : Conflict
Http Status Code : 429 Http Message : Too Many Requests--->So may requests

No comments :

Post a Comment