Java Generics (Notes)

Have List in Map value

request: The value of Map is List, and the object managed by this List has a relation of Child extends Parent. Then, there is an instance of each List , and I want to process the object in List as Parent.

problem

Generics are not covariant, so you can't:

Map<Key, List<Parent>> map1;
Map<Key, List<Child>> map2;
list1 = list2;  //Compile error here

Therefore, we use wildcards. However, the following is also NG

Map<Key, List<? extends Parent>> map1;
Map<Key, List<Child>> map2;
list1 = list2; //Compile error here

this is,

List<? extends Parent> list1;
List<Child> list2;
list1 = list2;

I could do it, but I thought it was strange. And the following is the correct answer

Map<Key, ? extends List<? extends Parent>> map1;
Map<Key, List<Child>> map2;
map1 = map2;

As you can see, I had to use the wildcard twice. I used this to define a method and execute it.

  List<Integer> ilist = new ArrayList<Integer>();
  ilist.add(1);
  ilist.add(2);
  ilist.add(3);			
		
  Map<String, List<Integer>> imap = new HashMap<String, List<Integer>>();
  imap.put("1", ilist);
  doit(imap);

  public void doit(Map<String, ? extends List<? extends Number>> inmap) {
      for (String key : inmap.keySet()) {
          System.out.println("key = " + key);
          List<? extends Number> list = inmap.get(key);
          for (Number n : list) {
              System.out.println(n);
          }
      } 
  }

bonus:

A method that returns a Map with List <Parent> as the value from List <Child>

Part 1

public <V, K extends T, T> Map<V, List<? extends T>> convert(Map<V, List<K>> srcMap) {
    Map<V, List<? extends T>> dstMap = new HashMap<V, List<? extends T>>();
    for (V key : srcMap.keySet()) {
        dstMap.put(key, srcMap.get(key));
    }
    return dstMap;
}

Part 2

public <V, K extends T, T> Map<V, List<T>> toUpper(Map<V, List<K>> srcMap) {
    Map<V, List<T>> dstMap = new HashMap<V, List<T>>();
        for (V key : srcMap.keySet()) {
	    List<T> tlist = new ArrayList<T>();
	    for (K val : srcMap.get(key)) {
		tlist.add(val);
	    }
	    dstMap.put(key, tlist);
	}
	return dstMap;
}

Probably "No. 2" is better. The reason is that the return type is Map <V, List <T >>. According to Effective Java, it is not good to make this method aware of wildcards on the execution side (e.g., List <? Extends T>) as in "Part 1" (probably ...).

Please point out any mistakes.

Recommended Posts

Java Generics (Notes)
java notes
[Java] Generics
Java Generics Summary
[Java] Generics sample
[Java] Array notes
[Java] Study notes
Java serialization notes
[Java] Stream Collectors notes
Java formatted output [Notes]
[Java] Control syntax notes
Java NIO 2 review notes
Frequently used Java generics
[Java] Basic method notes
Try scraping using java [Notes]
[Java] Generics classes and generics methods
[Implementation] Java Process class notes
Java Collections Framework Review Notes
Java
java Generics T and? Difference
Java
[Java] Basic types and instruction notes
Effective Java 3rd Edition Chapter 5 Generics
Notes on signal control in Java
[Java] Try to implement using generics
Java generics (defines classes and methods)
Notes on Android (java) thread processing
Generics of Kotlin for Java developers
Notes on Java path and Package
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
JUnit 4 notes
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
java shellsort
[Java] compareTo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array