Java Tutorial/Generics/Generic Parameters

Материал из Java эксперт
Перейти к: навигация, поиск

Constraints on a Wildcard

   <source lang="java">

public static void saveAll(LinkedList<? extends java.io.Serializable> list) {

  }
  public static void analyze(LinkedList<? super MyClass> list) {
   
  }</source>
   
  
 
  



Multiple Type Parameters

   <source lang="java">

class Pair<KeyType, ValueType> {

 // Constructor
 public Pair(KeyType aKey, ValueType aValue) {
   key = aKey;
   value = aValue;
 }
 // Get the key for this pair
 public KeyType getKey() {
   return key;
 }
 // Get the value for this pair
 public ValueType getValue() {
   return value;
 }
 // Set the value for this pair
 public void setValue(ValueType aValue) {
   value = aValue;
 }
 private KeyType key;
 private ValueType value;

} public class MainClass {

 public static void main(String[] a) {
   Pair<Integer, String> p = new Pair<Integer, String>(1, "A");
   System.out.println(p.getKey().getClass().getName());
 }

}</source>



java.lang.Integer


Type Parameter Bounds

   <source lang="java">

import static java.lang.Math.random; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;

public class MainClass {

 public static void main(String[] args) throws Exception {
   LinkedList<Integer> numbers = new LinkedList<Integer>();
   for (int i = 0; i < 10; i++) {
     numbers.addItem(1 + (int) (100.0 * random()));
   }
   System.out.println("\nnumbers list contains:");
   listAll(numbers);
   String filename = "C:/Numbers.bin";
   ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(filename));
   objOut.writeObject(numbers);
   objOut.close();
   LinkedList<Integer> values = null;
   ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(filename));
   values = (LinkedList<Integer>) (objIn.readObject());
   objIn.close();
   System.out.println("\nvalues list contains:");
   listAll(values);
 }
 static void listAll(LinkedList<Integer> list) {
   Integer number = list.getFirst();
   int count = 0;
   do {
     System.out.printf("%5d", number);
     if (++count % 5 == 0) {
       System.out.println();
     }
   } while ((number = list.getNext()) != null);
 }

}</source>



numbers list contains:
   39   63   34   47   86
   88   21   39   40    1
values list contains:
   39   63   34   47   86
   88   21   39   40    1


Using Bounded Wildcards in Methods

The syntax for using an upper bound is as follows:



   <source lang="java">

GenericType<? extends upperBoundType></source>



111.0
18.0


Using the ? wildcard

   <source lang="java">

import java.util.ArrayList; import java.util.List; public class MainClass {

   public static void printList (List<?> list) {
       for (Object element : list) {
           System.out.println(element);
       }
   }
   public static void main(String[] args) {
       List<String> list1 = new ArrayList<String>();
       list1.add ("Hello");
       list1.add ("World");
       printList (list1);
       List<Integer> list2 = new ArrayList<Integer>();
       list2.add(100);
       list2.add(200);
       printList(list2);
   }

}</source>



Hello
World
100
200