We have to use Generic to create a type specific collection object. In the following program I have created a Map whose key is an Integer. It also have the value of a String. There is no need to cast value obtained from type specific collection. We have top create the List that will contains a String only values.
Code:
package org.kodejava.example.intro;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
public class TSColl {
public static void main(String[] args) {
Map<Integers, Strings> grades1 = new HashMap<Integesr, Strings>();
grades1.put(1, "X");
grades1.put(2, "Y");
grades1.put(3, "Z");
grades1.put(4, "V");
grades1.put(5, "B");
String values = grades1.get(1);
List<String> dayNames = new ArrayList<String>();
dayNames.add("thursday");
dayNames.add("firday");
dayNames.add("saturday");
dayNames.add("Wednesday");
String firstDays = dayNames1.get(0);
}
}
Bookmarks