Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
123
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. public class Test {
  48.  
  49. public class A {}
  50.  
  51. public class B extends A {}
  52.  
  53. public class C extends B {}
  54.  
  55. public void testCoVariance(List<? extends B> myBlist) {
  56. B b = new B();
  57. C c = new C();
  58. myBlist.add(b); // does not compile
  59. myBlist.add(c); // does not compile
  60. A a = myBlist.get(0);
  61. }
  62.  
  63. public void testContraVariance(List<? super B> myBlist) {
  64. B b = new B();
  65. C c = new C();
  66. myBlist.add(b);
  67. myBlist.add(c);
  68. A a = myBlist.get(0); // does not compile
  69. }
  70. }
  71.  
  72. class Super {
  73.  
  74. Object testCoVariance(){ return null;} //Covariance of return types in the subtype.
  75. void testContraVariance(Object parameter){} // Contravariance of method arguments in the subtype.
  76. }
  77.  
  78. class Sub extends Super {
  79.  
  80. @Override
  81. String testCoVariance(){ return null;} //compiles successfully i.e. return type is don't care(String is subtype of Object)
  82. @Override
  83. void testContraVariance(String parameter){} //doesn't support even though String is subtype of Object
  84.  
  85. }
  86.  
  87. Object name= new String("prem"); //works
  88. List<Number> numbers = new ArrayList<Integer>();//gets compile time error
  89.  
  90. Integer[] myInts = {1,2,3,4};
  91. Number[] myNumber = myInts;
  92. 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)
  93.  
  94. List<String> list=new ArrayList<>();
  95. list.add("prem");
  96. List<Object> listObject=list; //Type mismatch: cannot convert from List<String> to List<Object> at Compiletime
  97.  
  98. class Shape { void draw() {}}
  99.  
  100. class Circle extends Shape {void draw() {}}
  101.  
  102. class Square extends Shape {void draw() {}}
  103.  
  104. class Rectangle extends Shape {void draw() {}}
  105.  
  106. public class Test {
  107. /*
  108. * Example for an upper bound wildcard (Get values i.e Producer `extends`)
  109. *
  110. * */
  111.  
  112. public void testCoVariance(List<? extends Shape> list) {
  113. list.add(new Shape()); // Error: is not applicable for the arguments (Shape) i.e. inheritance is not supporting
  114. list.add(new Circle()); // Error: is not applicable for the arguments (Circle) i.e. inheritance is not supporting
  115. list.add(new Square()); // Error: is not applicable for the arguments (Square) i.e. inheritance is not supporting
  116. list.add(new Rectangle()); // Error: is not applicable for the arguments (Rectangle) i.e. inheritance is not supporting
  117. Shape shape= list.get(0);//compiles so list act as produces only
  118.  
  119. /*You can't add a Shape,Circle,Square,Rectangle to a List<? extends Shape>
  120. * You can get an object and know that it will be an Shape
  121. */
  122. }
  123. /*
  124. * Example for a lower bound wildcard (Put values i.e Consumer`super`)
  125. * */
  126. public void testContraVariance(List<? super Shape> list) {
  127. list.add(new Shape());//compiles i.e. inheritance is supporting
  128. list.add(new Circle());//compiles i.e. inheritance is supporting
  129. list.add(new Square());//compiles i.e. inheritance is supporting
  130. list.add(new Rectangle());//compiles i.e. inheritance is supporting
  131. Shape shape= list.get(0); // Error: Type mismatch, so list acts only as consumer
  132. Object object= list.get(0); // gets an object, but we don't know what kind of Object it is.
  133.  
  134. /*You can add a Shape,Circle,Square,Rectangle to a List<? super Shape>
  135. * You can't get an Shape(but can get Object) and don't know what kind of Shape it is.
  136. */
  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