------- android培训、java培训、期待与您交流! ----------
TreeSet:可以对Set集合中的元素进行排序。
对自定义对象的操作:
需求:往TreeSet集合中存储自定义对象学生。
想按照学生的年龄进行排序。
class Student implements Comparable{//该接口强制让学生具备比较性。
private String name;
private int age;
Student(String name, int age){
this.name = name;
this.age = age;
}
public int compareTo(Object obj){
if(!(obj instanceof Student)){
throw new RuntimeException(“不是学生对象”);
}
Student s = (Student)obj;
if(this.age>s.age){
return 1;
}
if(this.age == s.age){
//return 0;//返回0时代表这两个对象是相同的对象,因此该对象不会存入集合。
reurn this.name.compareTo(s.name);//由于String类自身已实现Comparable接口,因此当年龄相同时,用其compareTo()方法来比较姓名的排序。
//*排序时,当主要条件相同时,一定判断一下次要条件。
}
return -1;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
class TreeSetDemo{
public static void main(String[] args){
TreeSet ts = new TreeSet();
ts.add(new Student(“lisi02”,22));
ts.add(new Student(“lisi007”,20));
ts.add(new Student(“lisi09”,19));
ts.add(new Student(“lisi01”,40));
Iterator it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
TreeSet底层数据结构:二叉树
保证元素唯一性的依据:compareTo方法return 0。
TreeSet排序的第一种方式:让元素自身具备比较性,元素需要实现Comparable接口,覆盖compareTo方法。这种方法也称为元素的自然顺序,或者叫默认顺序。
TreeSet的第二种排序方式:当元素自身不具备比较性,或者具备的比较性不是所需要的,这时就需要让集合自身具备比较性。在集合初始化时,就有了比较方式:定义比较器,将比较器对象作为参数传递给TreeSet集合的构造方法:TreeSet(Comparator comparator)。
*如果元素具有比较性,而且集合也存在比较器,优先按照比较器排序。
定义比较器:定义一个类,实现Comparator接口,覆盖compare方法。
例,不改Person类代码前提下,按照姓名排序。
class TreeSetDemo2{
public static void main(String[] args){
TreeSet ts = new TreeSet(new MyCompare());
ts.add(new Student(“lisi02”,22));
ts.add(new Student(“lisi007”,20));
ts.add(new Student(“lisi09”,19));
ts.add(new Student(“lisi01”,40));
Iterator it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class MyCompare implements Comparator{//创建比较器类。
public int compare(Object o1, Object o2){
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int num = s1.getName().compareTo(s2.getName());
if(num == 0){//当姓名相同时,按照年龄排序。
num = s1.getAge() – s2.getAge();
}
return num;
}
}
泛型:JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制。
泛型的好处:
1, 将运行时期出现问题ClassCastException,转移到了编译时期,方便于程序员解决问题,让运行时问题减少,安全。
2, 避免了强制转换的麻烦。
泛型格式:通过<>来定义要操作的引用数据类型。
在使用java提供的对象时,什么时候写泛型?
通常在集合框架中很常见。只要见到<>就要定义泛型。
其实<>就是用来接收类型的。当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可。
自定义泛型类:当类中要操作的引用数据类型不确定的时候,早期定义Object来完成扩展,现在定义泛型来完成扩展。
class Worker{
}
class Student{
}
class Tool{//当只存在Worker时,定义一个操作worker的工具类Tool。
private Worker w;
public void setWorker(Worker w){
this.w = w;
}
public Worker getWorker(){
return w;
}
}
class Utils<QQ>{//出现Student类后,发现其与Worker具有可提取的共通操作,之前的Tool就不能满足Student的需求了。我们需要一个功能:当对方在调用工具类的时候,由对方来指定是什么类型的对象。因此产生了此Utils类
private QQ q;
public void setObject(QQ q){
this.q = q;
}
public QQ getObject(){
return q;
}
}
class GenericDemo3{
public static void main(String[] args){
Tool t = new Tool();
t.setWorker(new Worker());
t.getWorker();
Utils< Worker> u = new Utils<Worker>();//在使用泛型时定义好泛型类型,其他该对象的泛型方法也都是这个类型。只要对象建立,操作类型就确定了。
u.setObject(new Worker());//如果此处传进Student对象,则编辑会失败。
Worker w = u.getObject();
}
}
泛型方法:
泛型类定义的泛型,在整个类中有效,如果被方法使用,那么泛型类的对象明确要操作的具体类型后,所有方法要操作的类型就已经固定了。
为了让不同方法可以操作不同类型,而且类型还不确定,那么可以将泛型定义在方法上。
class Demo{
public <T> void show(T t){此写法为泛型方法的定义
System.out.println(“show:”+ t);
}
public <T> void print(T t){//*注意:此”T”的定义仅在此方法内有效,与上面的show方法中的”T”没有任何关系。
System.out.println(“print:” + t);
}
}
class GenericDemo{
public static void main(String[] args){
Demo d = new Demo();
d.show(“haha”);
d.show(new Integet(4));
d.print(new Integet(4));
}
}
*静态方法不可以访问类上定义的泛型,如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。写法:
class Demo<T>{
public static <W> void method(W t){//此为静态方法的泛型定义,注意不可以设置为与类上定义泛型一直的T
System.out.println(“method:” + t);
}
}
//泛型定义在接口上:
interface Inter<T>{
void show(T t);
}
class InterImpl<T> implements Inter<T>{//可以在此实现具体类型,写成class InterImpl implements Inter<String>,也可以继续定义成泛型类,由调用者再指定具体类型。
public void show(T t){
System.out.println(“show :”+ t);
}
}
class GenericDemo{
public static void main(String[] args){
InterImpl<Integer> i = new InterImpl<Integer>();
i.show(4);
}
}
泛型限定:
class GenericDemo{
public static void main(String[] args){
ArrayList<String> al = new ArrayList<String>();
al.add(“abc1”);
al.add(“abc2”);
al.add(“abc3”);
ArrayList<Integer> all = new ArrayList<Integer>();
all.add(4);
all.add(7);
all.add(1);
}
//当我们想用一个方法既能遍历出ArrayList<String>,又可以遍历出ArrayList<Integer>时,可以这样定义:
public static void printColl(ArrayList<?> al){//?叫做占位符,代表了一个不能确定的类型。其不能使用子类的特有方法(只可以使用Object中的方法)
Iterator<?> it = al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
上面的printColl方法也可以这样定义:
public static <T> void printColl(ArrayList<T> al){
Iterator<?> it = al.iterator();
while(it.hasNext()){
T t = it.next()//T与?的区别:T可以表示具体类型,可以继续操作,而?是不明确类型。
System.out.println(t);
}
}
?的主要应用:泛型限定
class Person
{
private String name;
Person(String name){
this.name = name;
}
public String getName(){
return name;
}
}
class Student extends Person{
Student(String name){
super(name);
}
}
class GenericDemo{
public static void main(String[] args){
ArrayList<Person> al = new ArrayList<Person>();
al.add(new Person(“abc1”));
al.add(new Person(“abc2”));
ArrayList<Student> al1 = new ArrayList<Student>();
al1.add(new Student(“abc-1”));
al1.add(new Student(“abc-2”));
printColl(al1);
}
public static void printColl(ArrayList<? extends Person> al){//只打印Person和Person的子类,其他的不管。这就叫做泛型限定。
Iterator<? extends Person> it = al.iterator();
while(it.hasNext()){
System.out.println(it.next().getName());
}
}
}
?:通配符,也可以理解为占位符。
泛型的限定:
? extends E :可以接受E类型或者E的子类型。上限。
? super E :可以接受E类型或者E的父类型。下限。