Google

Apr 23, 2014

Understanding Java generics with the help of scenarios -- part 2

Extends Understanding Java generics with the help of scenarios -- part 1

Scenario 2: A custom generic class GenericMultipleTypesScenario class that handles Integer and Double types.



import java.util.List;

public class GenericMultipleTypesScenario<Number> {

 public void readOnly(List<? extends Number> numbers) {
   for (Number number : numbers) {
   System.out.println("readOnly: " + number);
  }
 }

 public void witeOnly(List<? super Number> numbers, Number aNumber) {
  numbers.add(aNumber);
 }

 public void witeAndRead(List<Number> numbers, Number aNumber) {
  numbers.add(aNumber);
  for (Number number : numbers) {
   System.out.println("readAndWrite: " + number);
  }

 }
}


The test class

import java.util.ArrayList;
import java.util.List;

public class GenericMultipleTypesScenarioTest {
 
 public static void main(String[] args) {
     //Integer
  GenericMultipleTypesScenario<Integer> integerType = new GenericMultipleTypesScenario<Integer>();
  List<Integer> numbers = new ArrayList<Integer>();
  numbers.add(1); //autoboxes 1 to type Integer
  
  integerType.readOnly(numbers);
  integerType.witeOnly(numbers, 6);  //autoboxes 6 to type Integer
  integerType.witeAndRead(numbers, Integer.valueOf(9));
  
  //Double
  GenericMultipleTypesScenario<Double> doubleType = new GenericMultipleTypesScenario<Double>();
  List<Double> numbersDouble = new ArrayList<Double>();
  numbersDouble.add(1.5); //autoboxes 1.5 to type Double
  
  doubleType.readOnly(numbersDouble);
  doubleType.witeOnly(numbersDouble, 6.5);  //autoboxes 6.5 to type Double
  doubleType.witeAndRead(numbersDouble, Double.valueOf(9.5));
  
 }
}



Output:

readOnly: 1
readAndWrite: 1
readAndWrite: 6
readAndWrite: 9
readOnly: 1.5
readAndWrite: 1.5
readAndWrite: 6.5
readAndWrite: 9.5


Scenario 3: A custom generic class GenericAnyTypesScenario class that handles any type like Integer, Double, String, etc. The any type is defined in this example as letter T. It can be any letter.



import java.util.List;

public class GenericAnyTypesScenario<T> {

 public void readOnly(List<? extends T> values) {
   for (T value : values) {
   System.out.println("readOnly: " + value);
  }
 }

 public void witeOnly(List<? super T> values, T aValue) {
  values.add(aValue);
 }

 public void witeAndRead(List<T> values, T aValue) {
  values.add(aValue);
  for (T value : values) {
   System.out.println("readAndWrite: " + value);
  }

 }
}


Test class





import java.util.ArrayList;
import java.util.List;

public class GenericAnyTypesScenarioTest {
 
 public static void main(String[] args) {
  //integer type
  GenericAnyTypesScenario<Integer> integerType = new GenericAnyTypesScenario<Integer>();
  List<Integer> valuesIntegers = new ArrayList<Integer>();
  valuesIntegers.add(1); //autoboxes 1 to type Integer
  
  integerType.readOnly(valuesIntegers);
  integerType.witeOnly(valuesIntegers, 6);  //autoboxes 6 to type Integer
  integerType.witeAndRead(valuesIntegers, Integer.valueOf(9));
  
  
  //double type
  GenericAnyTypesScenario<Double> doubleType = new GenericAnyTypesScenario<Double>();
  List<Double> valuesDouble = new ArrayList<Double>();
  valuesDouble.add(1.5); //autoboxes 1.5 to type Double
  
  doubleType.readOnly(valuesDouble);
  doubleType.witeOnly(valuesDouble, 6.5);  //autoboxes 6.5 to type Double
  doubleType.witeAndRead(valuesDouble, Double.valueOf(9.5));
  
  
  //string type
  GenericAnyTypesScenario<String> stringType = new GenericAnyTypesScenario<String>();
  List<String> valuesString = new ArrayList<String>();
  valuesString.add("apple");
  
  stringType.readOnly(valuesString);
  stringType.witeOnly(valuesString, "orange");
  stringType.witeAndRead(valuesString, "mango");
  
 }
}



Output:

readOnly: 1
readAndWrite: 1
readAndWrite: 6
readAndWrite: 9
readOnly: 1.5
readAndWrite: 1.5
readAndWrite: 6.5
readAndWrite: 9.5
readOnly: apple
readAndWrite: apple
readAndWrite: orange
readAndWrite: mango


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home