Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package POMOS;
- import java.util.HashMap;
- public class HashMapPomos {
- public static void main(String[] args) {
- HashMap<Integer,String> map=new HashMap<>();
- map.put(10,"Ivana");
- map.put(20,"Ivona");
- map.put(30,"Angela");
- //System.out.println("HashMap:\n"+map.toString());
- //provide value for new key which is absent with computeIfAbsent
- map.computeIfAbsent(40,value->"Sandra");
- //this will not affect anything because key 10 is present
- map.computeIfAbsent(10, value->"Ivana");
- //System.out.println("New HashMap:\n" +map.toString());
- HashMap<String,Integer> prices=new HashMap<>();
- prices.put("Shoes",200);
- prices.put("Bag",100);
- prices.put("Pants",300);
- System.out.println("Prices"+ prices.toString());
- System.out.println();
- //compute if absent value of shirt
- prices.computeIfAbsent("Shirt",value->500);
- System.out.println("New prices"+prices.toString());
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement