? Pass. It can also be understood as a placeholder.
generic limitation:
? Extends E: You can accept the type E or E sub -type. Upper limit
? Super E: You can receive the father type of type E or E. Lower limit
import java.util.*;
class GenericDemo6
{
public static void main(String[] args)
{
ArrayList<String>al = new ArrayList<String>();
al.add("abc1");
al.add("abc2");
al.add("abc3");
ArrayList<Integer>al1 = new ArrayList<Integer>();
al1.add(4);
al1.add(5);
al1.add(6);
printColl(al);
printColl(al1);
}
public static void printColl(ArrayList<?> al)
{
Iterator<?>it = al.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Result:
abc1
abc2
abc3
4
5
6
import java.util.*;
class GenericDemo7
{
public static void main(String[] args)
{
TreeSet<Student> ts = new TreeSet<Student>(new Comp());
ts.add(new Student("abc03"));
ts.add(new Student("abc02"));
ts.add(new Student("abc06"));
ts.add(new Student("abc01"));
Iterator<Student> it = ts.iterator();
while(it.hasNext())
{
System.out.println(it.next().getName());
}
/**/
TreeSet<Worker> ts1 = new TreeSet<Worker>(new Comp());
ts1.add(new Worker("wabc--03"));
ts1.add(new Worker("wabc--02"));
ts1.add(new Worker("wabc--06"));
ts1.add(new Worker("wabc--01"));
Iterator<Worker> it1 = ts1.iterator();
while(it1.hasNext())
{
System.out.println(it1.next().getName());
}
}
}
/*
class StuComp implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
return s1.getName().compareTo(s2.getName());
}
}
class WorkerComp implements Comparator<Worker>
{
public int compare(Worker s1,Worker s2)
{
return s1.getName().compareTo(s2.getName());
}
}
*/
class Comp implements Comparator<Person>
{
public int compare(Person p1,Person p2)
{
return p2.getName().compareTo(p1.getName());
}
}
class Person
{
private String name;
Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public String toString()
{
return "person :"+name;
}
}
class Student extends Person
{
Student(String name)
{
super(name);
}
}
class Worker extends Person
{
Worker(String name)
{
super(name);
}
}
Look at the comments in the code, because Student and Worker do not have comparability, we have prepared the corresponding comparator for the two classes, respectively. But we see that this seems to be a bit redundant. Therefore, we replaced the following code:
class Comp implements Comparator<Person>
{
public int compare(Person p1,Person p2)
{
return p2.getName().compareTo(p1.getName());
}
}
Here you use the so -called lower limit.