Illuminatiiiiii
6/29/2019 - 7:21 PM

HashMaps

package me.illuminatiproductions;

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

public class Main {

    public static void main(String[] args) {

        //Hashmaps - Store pairs of values

        HashMap<String, Long> phone_numbers = new HashMap<>();
        phone_numbers.put("Bob", 2132112411L); //add new pairs
        phone_numbers.put("Robby", 4134535411L);
        phone_numbers.put("Ricky", 9176876861L);

        //Get a value from a key
        System.out.println(phone_numbers.get("Bob"));

        //update a pair
        System.out.println(phone_numbers.get("Robby"));
        phone_numbers.put("Robby", 66666666666L);
        System.out.println(phone_numbers.get("Robby"));

        //remove a pair
        phone_numbers.remove("Ricky");

        //Print out a HashMap by converting it to a set
        for (Map.Entry<String, Long> set : phone_numbers.entrySet()){
            System.out.println(set.getKey() + " " + set.getValue());
        }


    }
}