Skip to content

Instantly share code, notes, and snippets.

@pmihalcin
Created April 3, 2018 07:38
Show Gist options
  • Save pmihalcin/acb746c7facff0c3cf05979be52dae4f to your computer and use it in GitHub Desktop.
Save pmihalcin/acb746c7facff0c3cf05979be52dae4f to your computer and use it in GitHub Desktop.
bounded wildcards
package net.homecredit.mer.web;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Patrik.Mihalcin on 29.03.2018
*/
public class GenericsBoundedWildcards {
// more restrictive than it need be
public static <T> void copy1(List<T> src, List<T> dest) { // uses no wildcards
for (int i = 0; i < src.size(); i++)
dest.set(i, src.get(i));
}
// any type without any restrictions
// method takes one type of list as a source and copies the content into another - totally unrelated - type of destination list
public static void copy2(List<?> src, List<?> dest) { // uses unbounded wildcards
// References pointing to objects of unknown type are usually expressed as a reference of type Object
Object o = src.get(0);
// dest.set(0, o);
// for (int i = 0; i < src.size(); i++)
// java: incompatible types: java.lang.Object cannot be converted to capture#1 of ?
// dest.set(i, src.get(i));
}
// we require that a type T exists that is subtype of the output list's element type and supertype of the input list's element type
// the input src list must have an element type with an upper bound T
// the output dest list is required to have an element type with a lower bound T
public static <T> void copy3(List<? extends T> src, List<? super T> dest) { // uses bounded wildcards
for (int i = 0; i < src.size(); i++)
dest.set(i, src.get(i));
}
public static void main(String[] args) {
List<Long> input = new ArrayList<>();
List<Object> output = new ArrayList<>();
// java: method copy1 in class net.homecredit.mer.web.Main cannot be applied to given types;
// required: java.util.List<T>,java.util.List<T>
// found: java.util.List<java.lang.Object>,java.util.List<java.lang.Long>
// reason: inferred type does not conform to equality constraint(s)
// inferred: java.lang.Long
// equality constraints(s): java.lang.Long,java.lang.Object
// copy1(output, input);
// T needs to be a supertype of Long and a subtype of Object
// T := Number & Serializable & Comparable
copy3(input, output);
// java: method copy3 in class net.homecredit.mer.web.Main cannot be applied to given types;
// required: java.util.List<? extends T>,java.util.List<? super T>
// found: java.util.List<java.lang.Long>,java.util.List<java.lang.String>
// reason: inferred type does not conform to upper bound(s)
// inferred: java.lang.Long
// upper bound(s): java.lang.String,java.lang.Object
List<Long> input2 = new ArrayList<>();
List<String> output2 = new ArrayList<>();
// compiler realizes that there is no type that is subtype of String and supertype of Long
// T needs to be a supertype of Long and a subtype of String
// copy3(input2, output2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment