Here are some most simple and common use of generics with collection.

Example 1:
List<String> names = new ArrayList<String>();
names.add("John");
System.out.printf("List<String> names: %s%n", names);
In the above example, names is a List of String. When retrieving elements from the List, the return value is of type String. So no need for casting, which is a big advantage over the old, non-parameterized collection.

Example 2:
Map<Integer, String> idToName = new HashMap<Integer, String>();
idToName.put(0, "John");
System.out.printf("Map<Integer, String> idToName: %s%n", idToName);
In the above example, idToName is a Map with a Integer key and String value. The output is:
Map<Integer, String> idToName: {0=John}

Example 3:
List<List<String>> listOfList = new ArrayList<List<String>>();
List<String> sublist1 = new ArrayList<String>();
sublist1.add("A String inside sublist1");
listOfList.add(sublist1);

List<String> sublist2 = new LinkedList<String>();
sublist2.add("A String inside sublist2");
listOfList.add(sublist2);
System.out.printf("List<List<String>> listOfList: %s%n", listOfList);
The above example shows a List whose elements are of type List, i.e., a List of List. The inner List declares that it can only hold String elements. The first inner list is an ArrayList of String, and the second is a LinkedList of String. Running this code snippet prints:
List<List<String>> listOfList: [[A String inside sublist1], [A String inside sublist2]]

Example 4:
private static <T> List<T> extractElements(List bag, Class<T> type) {
List<T> result = new ArrayList<T>();
for(Object e : bag) {
//if(e instanceof T) can't use instanceof
if(type.isAssignableFrom(e.getClass())) {
result.add((T) e);
}
}
return result;
}
This method takes a List of mixed elements and extracts those elements of the desired type. The following shows how to call this method:
List bag = new ArrayList();
bag.add(new Integer(0));
bag.add(new Integer(1));
bag.add(new Double(2008.5));
bag.add("a string");
List<Number> numbersInBag = extractElements(bag, Number.class);
System.out.printf("All elements in bag: %s%nNumber elements in bag: %s%n",
bag, numbersInBag);
List<Integer> integersInBag = extractElements(bag, Integer.class);
System.out.printf("All elements in bag: %s%nInteger elements in bag: %s%n",
bag, integersInBag);
-------- output -----------

All elements in bag: [0, 1, 2008.5, a string]
Number elements in bag: [0, 1, 2008.5]
All elements in bag: [0, 1, 2008.5, a string]
Integer elements in bag: [0, 1]
5

View comments

Labels
Archive
Popular Posts
Popular Posts
  • Two JVM options are often used to tune JVM heap size: -Xmx for maximum heap size, and -Xms for initial heap size. Here are some common mi...
  • Simple enum . The ; after the last element is optional, when this is the end of enum definition. public enum Color { WHITE, BLACK, RED, ...
  • How to set project classpath in Eclipse and NetBeans are similar: just right-click the project name, choose Properties to bring up the Prope...
  • Let's say I need to spawn multiple threads to do the work, and continue to the next step only after all of them complete. I will need t...
  • This is a sample web.xml based on Servlet 2.5 (part of Java EE 5) that declares common elements. All top-level elements are optional, and c...
  • The default string value for java enum is its face value, or the element name. However, you can customize the string value by overriding toS...
  • Prior to JDK 6, we can check if a string is empty in 2 ways: if(s != null && s.length() == 0) if(("").equals(s)) Checking ...
  • When writing javadocs, IntelliJ automatically adds a closing tag for html elements. For instance, after typing <lt>, it automaticaly a...
  • StringBuilder was introduced in JDK 1.5. What's the difference between StringBuilder and StringBuffer? According to javadoc , StringBu...
  • With array, we can easily declare and initialize it at the same time: String[] favorites = new String[] {"EJB", "JPA", ...
Loading