Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static double sum(Collection<? extends Number> nums) {
- double s = 0.0;
- for (Number num : nums)
- s += num.doubleValue();
- return s;
- }
- List<Integer>ints = Arrays.asList(1,2,3);
- assert sum(ints) == 6.0;
- List<Double>doubles = Arrays.asList(2.78,3.14);
- assert sum(doubles) == 5.92;
- List<Number>nums = Arrays.<Number>asList(1,2,2.78,3.14);
- assert sum(nums) == 8.92;
- List<Integer> ints = new ArrayList<Integer>();
- ints.add(1);
- ints.add(2);
- List<? extends Number> nums = ints;
- nums.add(null); // ok
- assert nums.toString().equals("[1, 2, null]");
- public static void count(Collection<? super Integer> ints, int n) {
- for (int i = 0; i < n; i++) ints.add(i);
- }
- List<Integer>ints = new ArrayList<Integer>();
- count(ints, 5);
- assert ints.toString().equals("[0, 1, 2, 3, 4]");
- List<Number>nums = new ArrayList<Number>();
- count(nums, 5); nums.add(5.0);
- assert nums.toString().equals("[0, 1, 2, 3, 4, 5.0]");
- List<Object>objs = new ArrayList<Object>();
- count(objs, 5); objs.add("five");
- assert objs.toString().equals("[0, 1, 2, 3, 4, five]");
- List<Object> objs = Arrays.<Object>asList(1,"two");
- List<? super Integer> ints = objs;
- String str = "";
- for (Object obj : ints) str += obj.toString();
- assert str.equals("1two");
- public static double sumCount(Collection<Number> nums, int n) {
- count(nums, n);
- return sum(nums);
- }
- public class Test {
- public class A {}
- public class B extends A {}
- public class C extends B {}
- public void testCoVariance(List<? extends B> myBlist) {
- B b = new B();
- C c = new C();
- myBlist.add(b); // does not compile
- myBlist.add(c); // does not compile
- A a = myBlist.get(0);
- }
- public void testContraVariance(List<? super B> myBlist) {
- B b = new B();
- C c = new C();
- myBlist.add(b);
- myBlist.add(c);
- A a = myBlist.get(0); // does not compile
- }
- }
- class Super {
- Object testCoVariance(){ return null;} //Covariance of return types in the subtype.
- void testContraVariance(Object parameter){} // Contravariance of method arguments in the subtype.
- }
- class Sub extends Super {
- @Override
- String testCoVariance(){ return null;} //compiles successfully i.e. return type is don't care(String is subtype of Object)
- @Override
- void testContraVariance(String parameter){} //doesn't support even though String is subtype of Object
- }
- Object name= new String("prem"); //works
- List<Number> numbers = new ArrayList<Integer>();//gets compile time error
- Integer[] myInts = {1,2,3,4};
- Number[] myNumber = myInts;
- myNumber[0] = 3.14; //attempt of heap pollution i.e. at runtime gets java.lang.ArrayStoreException: java.lang.Double(we can fool compiler but not run-time)
- List<String> list=new ArrayList<>();
- list.add("prem");
- List<Object> listObject=list; //Type mismatch: cannot convert from List<String> to List<Object> at Compiletime
- class Shape { void draw() {}}
- class Circle extends Shape {void draw() {}}
- class Square extends Shape {void draw() {}}
- class Rectangle extends Shape {void draw() {}}
- public class Test {
- /*
- * Example for an upper bound wildcard (Get values i.e Producer `extends`)
- *
- * */
- public void testCoVariance(List<? extends Shape> list) {
- list.add(new Shape()); // Error: is not applicable for the arguments (Shape) i.e. inheritance is not supporting
- list.add(new Circle()); // Error: is not applicable for the arguments (Circle) i.e. inheritance is not supporting
- list.add(new Square()); // Error: is not applicable for the arguments (Square) i.e. inheritance is not supporting
- list.add(new Rectangle()); // Error: is not applicable for the arguments (Rectangle) i.e. inheritance is not supporting
- Shape shape= list.get(0);//compiles so list act as produces only
- /*You can't add a Shape,Circle,Square,Rectangle to a List<? extends Shape>
- * You can get an object and know that it will be an Shape
- */
- }
- /*
- * Example for a lower bound wildcard (Put values i.e Consumer`super`)
- * */
- public void testContraVariance(List<? super Shape> list) {
- list.add(new Shape());//compiles i.e. inheritance is supporting
- list.add(new Circle());//compiles i.e. inheritance is supporting
- list.add(new Square());//compiles i.e. inheritance is supporting
- list.add(new Rectangle());//compiles i.e. inheritance is supporting
- Shape shape= list.get(0); // Error: Type mismatch, so list acts only as consumer
- Object object= list.get(0); // gets an object, but we don't know what kind of Object it is.
- /*You can add a Shape,Circle,Square,Rectangle to a List<? super Shape>
- * You can't get an Shape(but can get Object) and don't know what kind of Shape it is.
- */
- }
- }
- // Source
- List<Integer> intList = Arrays.asList(1,2,3);
- List<Double> doubleList = Arrays.asList(2.78,3.14);
- List<Number> numList = Arrays.asList(1,2,2.78,3.14,5);
- // Destination
- List<Integer> intList2 = new ArrayList<>();
- List<Double> doublesList2 = new ArrayList<>();
- List<Number> numList2 = new ArrayList<>();
- // Works
- copyElements1(intList,intList2); // from int to int
- copyElements1(doubleList,doublesList2); // from double to double
- static <T> void copyElements1(Collection<T> src, Collection<T> dest) {
- for(T n : src){
- dest.add(n);
- }
- }
- // Let's try to copy intList to its supertype
- copyElements1(intList,numList2); // error, method signature just says "T"
- // and here the compiler is given
- // two types: Integer and Number,
- // so which one shall it be?
- // PECS to the rescue!
- copyElements2(intList,numList2); // possible
- // copy Integer (? extends T) to its supertype (Number is super of Integer)
- private static <T> void copyElements2(Collection<? extends T> src,
- Collection<? super T> dest) {
- for(T n : src){
- dest.add(n);
- }
- }
- class Creature{}// X
- class Animal extends Creature{}// Y
- class Fish extends Animal{}// Z
- class Shark extends Fish{}// A
- class HammerSkark extends Shark{}// B
- class DeadHammerShark extends HammerSkark{}// C
- List<? extends Shark> sharks = new ArrayList<>();
- sharks.add(new HammerShark());//will result in compilation error
- sharks.add(new HammerShark());
- List<? super Shark> sharks = new ArrayList<>();
- sharks.add(new Shark());
- sharks.add(new DeadHammerShark());
- sharks.add(new HammerSkark());
- Object o;
- o = sharks.get(2);// only assignment that works
- Animal s;
- s = sharks.get(2);//doen't work
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement