Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. public static double sum(Collection<? extends Number> nums) {
  2. double s = 0.0;
  3. for (Number num : nums)
  4. s += num.doubleValue();
  5. return s;
  6. }
  7.  
  8. List<Integer>ints = Arrays.asList(1,2,3);
  9. assert sum(ints) == 6.0;
  10. List<Double>doubles = Arrays.asList(2.78,3.14);
  11. assert sum(doubles) == 5.92;
  12. List<Number>nums = Arrays.<Number>asList(1,2,2.78,3.14);
  13. assert sum(nums) == 8.92;
  14.  
  15. List<Integer> ints = new ArrayList<Integer>();
  16. ints.add(1);
  17. ints.add(2);
  18. List<? extends Number> nums = ints;
  19. nums.add(null); // ok
  20. assert nums.toString().equals("[1, 2, null]");
  21.  
  22. public static void count(Collection<? super Integer> ints, int n) {
  23. for (int i = 0; i < n; i++) ints.add(i);
  24. }
  25.  
  26. List<Integer>ints = new ArrayList<Integer>();
  27. count(ints, 5);
  28. assert ints.toString().equals("[0, 1, 2, 3, 4]");
  29. List<Number>nums = new ArrayList<Number>();
  30. count(nums, 5); nums.add(5.0);
  31. assert nums.toString().equals("[0, 1, 2, 3, 4, 5.0]");
  32. List<Object>objs = new ArrayList<Object>();
  33. count(objs, 5); objs.add("five");
  34. assert objs.toString().equals("[0, 1, 2, 3, 4, five]");
  35.  
  36. List<Object> objs = Arrays.<Object>asList(1,"two");
  37. List<? super Integer> ints = objs;
  38. String str = "";
  39. for (Object obj : ints) str += obj.toString();
  40. assert str.equals("1two");
  41.  
  42. public static double sumCount(Collection<Number> nums, int n) {
  43. count(nums, n);
  44. return sum(nums);
  45. }
  46.  
  47. class Super {
  48.  
  49. Object testCoVariance(){ return null;} //Covariance of return types in the subtype.
  50. void testContraVariance(Object parameter){} // Contravariance of method arguments in the subtype.
  51. }
  52.  
  53. class Sub extends Super {
  54.  
  55. @Override
  56. String testCoVariance(){ return null;} //compiles successfully i.e. return type is don't care(String is subtype of Object)
  57. @Override
  58. void testContraVariance(String parameter){} //doesn't support even though String is subtype of Object
  59.  
  60. }
  61.  
  62. Object name= new String("prem"); //works
  63. List<Number> numbers = new ArrayList<Integer>();//gets compile time error
  64.  
  65. Integer[] myInts = {1,2,3,4};
  66. Number[] myNumber = myInts;
  67. 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)
  68.  
  69. List<String> list=new ArrayList<>();
  70. list.add("prem");
  71. List<Object> listObject=list; //Type mismatch: cannot convert from List<String> to List<Object> at Compiletime
  72.  
  73. class Shape { void draw() {}}
  74.  
  75. class Circle extends Shape {void draw() {}}
  76.  
  77. class Square extends Shape {void draw() {}}
  78.  
  79. class Rectangle extends Shape {void draw() {}}
  80.  
  81. public class Test {
  82. /*
  83. * Example for an upper bound wildcard (Get values i.e Producer `extends`)
  84. *
  85. * */
  86.  
  87. public void testCoVariance(List<? extends Shape> list) {
  88. list.add(new Shape()); // Error: is not applicable for the arguments (Shape) i.e. inheritance is not supporting
  89. list.add(new Circle()); // Error: is not applicable for the arguments (Circle) i.e. inheritance is not supporting
  90. list.add(new Square()); // Error: is not applicable for the arguments (Square) i.e. inheritance is not supporting
  91. list.add(new Rectangle()); // Error: is not applicable for the arguments (Rectangle) i.e. inheritance is not supporting
  92. Shape shape= list.get(0);//compiles so list act as produces only
  93.  
  94. /*You can't add a Shape,Circle,Square,Rectangle to a List<? extends Shape>
  95. * You can get an object and know that it will be an Shape
  96. */
  97. }
  98. /*
  99. * Example for a lower bound wildcard (Put values i.e Consumer`super`)
  100. * */
  101. public void testContraVariance(List<? super Shape> list) {
  102. list.add(new Shape());//compiles i.e. inheritance is supporting
  103. list.add(new Circle());//compiles i.e. inheritance is supporting
  104. list.add(new Square());//compiles i.e. inheritance is supporting
  105. list.add(new Rectangle());//compiles i.e. inheritance is supporting
  106. Shape shape= list.get(0); // Error: Type mismatch, so list acts only as consumer
  107. Object object= list.get(0); // gets an object, but we don't know what kind of Object it is.
  108.  
  109. /*You can add a Shape,Circle,Square,Rectangle to a List<? super Shape>
  110. * You can't get an Shape(but can get Object) and don't know what kind of Shape it is.
  111. */
  112. }
  113. }
  114.  
  115. public class Test {
  116.  
  117. public class A {}
  118.  
  119. public class B extends A {}
  120.  
  121. public class C extends B {}
  122.  
  123. public void testCoVariance(List<? extends B> myBlist) {
  124. B b = new B();
  125. C c = new C();
  126. myBlist.add(b); // does not compile
  127. myBlist.add(c); // does not compile
  128. A a = myBlist.get(0);
  129. }
  130.  
  131. public void testContraVariance(List<? super B> myBlist) {
  132. B b = new B();
  133. C c = new C();
  134. myBlist.add(b);
  135. myBlist.add(c);
  136. A a = myBlist.get(0); // does not compile
  137. }
  138. }
  139.  
  140. // Source
  141. List<Integer> intList = Arrays.asList(1,2,3);
  142. List<Double> doubleList = Arrays.asList(2.78,3.14);
  143. List<Number> numList = Arrays.asList(1,2,2.78,3.14,5);
  144.  
  145. // Destination
  146. List<Integer> intList2 = new ArrayList<>();
  147. List<Double> doublesList2 = new ArrayList<>();
  148. List<Number> numList2 = new ArrayList<>();
  149.  
  150. // Works
  151. copyElements1(intList,intList2); // from int to int
  152. copyElements1(doubleList,doublesList2); // from double to double
  153.  
  154.  
  155. static <T> void copyElements1(Collection<T> src, Collection<T> dest) {
  156. for(T n : src){
  157. dest.add(n);
  158. }
  159. }
  160.  
  161.  
  162. // Let's try to copy intList to its supertype
  163. copyElements1(intList,numList2); // error, method signature just says "T"
  164. // and here the compiler is given
  165. // two types: Integer and Number,
  166. // so which one shall it be?
  167.  
  168. // PECS to the rescue!
  169. copyElements2(intList,numList2); // possible
  170.  
  171.  
  172.  
  173. // copy Integer (? extends T) to its supertype (Number is super of Integer)
  174. private static <T> void copyElements2(Collection<? extends T> src,
  175. Collection<? super T> dest) {
  176. for(T n : src){
  177. dest.add(n);
  178. }
  179. }
  180.  
  181. class Creature{}// X
  182. class Animal extends Creature{}// Y
  183. class Fish extends Animal{}// Z
  184. class Shark extends Fish{}// A
  185. class HammerSkark extends Shark{}// B
  186. class DeadHammerShark extends HammerSkark{}// C
  187.  
  188. List<? extends Shark> sharks = new ArrayList<>();
  189.  
  190. sharks.add(new HammerShark());//will result in compilation error
  191.  
  192. sharks.add(new HammerShark());
  193.  
  194. List<? super Shark> sharks = new ArrayList<>();
  195.  
  196. sharks.add(new Shark());
  197. sharks.add(new DeadHammerShark());
  198. sharks.add(new HammerSkark());
  199.  
  200. Object o;
  201. o = sharks.get(2);// only assignment that works
  202.  
  203. Animal s;
  204. s = sharks.get(2);//doen't work
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement