- November 24, 2020
- saptrxuy_learnit
- 0 Comments
- 2074 Views
- 0 Likes
- Java
Map with Different Data Types as Values
By Akshata Dupare
1. Introduction
2. Main class and main method
3. hashMap Example
4. Adding some String, String as key and value
5. Adding int as value
6. Adding boolean as value
7. Adding String[] as value
8. print array values
9. Adding list of String as value
10. Adding list of Integer as value
1.Introduction▲
This program shows how to use Object as value component type to allow all types of data to be stored in a map.
1.1.Code▲
10:/** 20: * Class Name: MapWithDifferentDataTypes 30: * Author: Akshata 40: * Date: 2019-11-6 50: * Details: Simple Map Operations with different data types 60: */
2.Main class and main method▲
2.1.Code▲
70: 80:package xv.tarining.core.maps; 150: 180: 190:public class MapWithDifferentDataTypes { 200: 210: // 200 Main class and main method 220: public static void main(String[] args) { 230: System.out.println("Program started\n"); 270: 280: System.out.println("\nProgram ended."); 290: } 800: 810:} 820:
2.2.Output▲
Program started Program ended.
3.hashMap Example▲
1. We create a variable of Map data type and initialize it with HashMap constructor.
2. The map has key data type as string
3. The value data type is Object so that we can add any datatype to value
3.1.Code▲
110://300 Create method for hashMapExample 120:import java.util.HashMap; 130:import java.util.List; 140:import java.util.Map; 170:import java.util.Map.Entry; 240: 250: // 300 a map with value of complex type 260: MapWithDifferentDataTypes.mapwithDifferentDataTypes(); 300: 310: // 300 a map with value of complex type 320: private static void mapwithDifferentDataTypes() { 330: 340: // 300 the map has key data type as string 350: // the value data type is Object so that we can add any datatype to value 360: Map<string, object=""> map1 = new HashMap<string, object="">(); 590: 600: // 300 Printing the map 620: for (Entry<string, object=""> entry : map1.entrySet()) { 630: System.out.println(entry.getKey() + ": " + entry.getValue()); 640: } 770: 780: System.out.println("\n"); 790: } </string,></string,></string,>
4.Adding some String, String as key and value▲
4.1.Code▲
370: 380: // 400 adding some String, String as key and value 390: map1.put("First Name", "Siddhant"); 400: map1.put("Last Name", "Dupare");
4.2.Output▲
First Name: Siddhant Last Name: Dupare
5.Adding int as value▲
5.1.Code▲
410: 420: // 500 adding int as value 430: map1.put("Age", 21);
5.2.Output▲
Age: 21
6.Adding boolean as value▲
6.1.Code▲
440: 450: // 600 Adding boolean as value 460: map1.put("Married", false);
6.2.Output▲
Married: false
7.Adding String[] as value▲
7.1.Code▲
470: 480: // 700 adding String[] as value 490: String[] friends = { "friend1", "friend2" }; 500: map1.put("Friends", friends);
7.2.Output▲
Friends: [Ljava.lang.String;@24d46ca6
8.print array values▲
1. Array friends is printed as [Ljava.lang.String;@24d46ca6, so we have to modify our print statement to print it properly.
2. We do this by checking the class of value item in for loop,
entry.getValue().getClass() == String[].class
3. if the class is String array, we call Arrays.deepToString(..) method. It will print each item properly even in multi-layer arrays.
4. We cast items as Object array to make sure that deepToString handles it properly.
(Object[]) entry.getValue()
8.1.Code▲
90: 100:import java.util.Arrays; 610: /* //800 650: */ //800 660: 670: // 800 print array values 680: // array friends is printed as [Ljava.lang.String;@24d46ca6 690: // so we have to print it properly 700: for (Entry<string, object=""> entry : map1.entrySet()) { 710: if (entry.getValue().getClass() == String[].class) { 720: System.out.println(entry.getKey() + ": " + Arrays.deepToString((Object[]) entry.getValue())); 730: } else { 740: System.out.println(entry.getKey() + ": " + entry.getValue()); 750: } 760: } </string,>
8.2.Output▲
Friends: [friend1, friend2]
9.Adding list of String as value▲
9.1.Code▲
510: 520: // 900 adding list of String as value 530: List classmates = List.of("classmate1", "classmate2"); 540: map1.put("Classmates", classmates);
9.2.Output▲
Classmates: [classmate1, classmate2]
10.Adding list of Integer as value▲
10.1.Code▲
550: 560: // 1000 adding list of Integer as value 570: List scores = List.of(96, 95, 94); 580: map1.put("Scores", scores);
10.2.Output▲
Scores: [96, 95, 94]
Leave a Comment