fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List<Specific> specificList = Arrays.asList(new Specific(), new Specific());
  13. Map<Integer, ? extends Abstract> abstractMap = new HashMap<>();
  14.  
  15. specificList.forEach(device -> abstractMap.put(device.getIdentity(), device));
  16. }
  17. }
  18.  
  19. class Abstract {
  20. Integer identity;
  21.  
  22. Abstract() {
  23. Random rand = new Random();
  24. identity = rand.nextInt(9000);
  25. }
  26.  
  27. public Integer getIdentity() {
  28. return identity;
  29. }
  30. }
  31.  
  32. class Specific extends Abstract { }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:15: error: incompatible types: Specific cannot be converted to CAP#1
        specificList.forEach(device -> abstractMap.put(device.getIdentity(), device));
                                                                             ^
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Abstract from capture of ? extends Abstract
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
stdout
Standard output is empty