The document discusses Java maps. It shows how to add elements to a map, remove elements, search for a key, get a value, insert one map into another, and print all keys and values. It also shows an example of setting an attribute in one JSP and forwarding to another JSP to display it.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
17 views
Java
The document discusses Java maps. It shows how to add elements to a map, remove elements, search for a key, get a value, insert one map into another, and print all keys and values. It also shows an example of setting an attribute in one JSP and forwarding to another JSP to display it.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Abhishek bhalkhede
05
1) 1)add items in the map.2)remove items from int value = map.get("cherry");
the map.3)search specifickey fromthe map4)get value of the specific key 5)insert map System.out.println("The value of the key elementsofthe map in to other map 6)print all 'cherry' is " + value); keys and values of the map injav injavapackage hashSet; System.out.println(" "); import java.util.HashMap; System.out.println("inset map element of import java.util.Map; one map into other map ."); public class MapExample { Map<String, Integer> map2 = new HashMap<>(); public static void main(String[] args) { map2.put("orange", 4); Map<String, Integer> map = new HashMap<>();System.out.println("add items in map2.put("pear", 5); the map."); map.putAll(map2); map.put("apple", 1); System.out.println(map2); map.put("banana", 2); System.out.println(" "); map.put("cherry", 3); System.out.println("print all keys and System.out.println(map); values of the map"); System.out.println(" "); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println("remove items in the map."); System.out.println("Key: " + entry.getKey() + ", Value: " + map.remove("banana"); entry.getValue()); }}} System.out.println(map); Output: System.out.println(" ");