How to use map interface in Java

2 Answers

0 votes
package javaapplication1;

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

public class Example {
    public static void main(String[] args) {
             
        Map<String, String> map = new HashMap<>();
	    map.put("1", "C");
	    map.put("2", "C++");
	    map.put("3", "Java");
	
	    for (Map.Entry<String, String> entry : map.entrySet()) 
            System.out.println(entry.getKey() + " - " + entry.getValue());
    }
}


/*
run:
 
1 - C
2 - C++
3 - Java
 
*/

 



answered Jan 17, 2016 by avibootz
0 votes
package javaapplication1;

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

public class Example {
    public static void main(String[] args) {
             
        Map<Integer, String> map = new HashMap<>();
	    map.put(1, "C");
	    map.put(2, "C++");
	    map.put(3, "Java");
	
        map.entrySet().stream().forEach((entry) -> { 
            System.out.println(entry.getKey() + " - " + entry.getValue());
        });
    }
}


/*
run:
 
1 - C
2 - C++
3 - Java
 
*/

 



answered Jan 17, 2016 by avibootz
...