Java Map get value for a key with default value

Description

Java Map get value for a key with default value

import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    Map<String, Integer> m = new HashMap<String, Integer>();

    m.put("CSS", 42);
    m.put("HTML", 3);
    m.put("Java", 1);
    /*from   w  w  w .  j av  a  2  s. c o  m*/
    Integer i = m.get("asdf");
    System.out.println(i);
    
    
    i = m.getOrDefault("asdf", 123);
    System.out.println(i);

  }
}



PreviousNext

Related