黑马程序员——集合框架(三)

------- android培训java培训、期待与您交流! ----------

一、TreeSte

TreeSet可以对Set集合中的元素进行排序。底层数据结构是二叉树。

保证元素唯一性的依据:campareTo方法return 0

TreeSet排序的有两种方式。

1.第一种方式,让元素自身具备比较性。元素要实现comparable接口,覆盖compareTo方法。

这种方式也称为元素的自然顺序。

程序代码实现如下:

 

import java.util.*;
class  TreeSetDemo
{
	public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet();

		ts.add(new Student("lisi02",22));
		ts.add(new Student("lisi04",22));
		ts.add(new Student("lisi02",22));
		ts.add(new Student("zhangsen",22));
		ts.add(new Student("lisi02",45));
		

		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			Student s = (Student)it.next();
			System.out.println(s.getName()+"----:"+s.getAge());
		}
	}
}

class Student implements Comparable
{
	private String name;
	private int age;
	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return 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 this.name.compareTo(s.name);
		return -1;
	}

}

2.TreeSet排序的第二种方式:当元素自身不具备比较性时,或者具备的比较性不是所需要的。这时就需要让集合自身具备比较性。定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。

当两种排序都存在时,以比较器为主。定义一个类,实现Comparator接口,覆盖compae方法。

程序代码实现如下:

import java.util.*;
class  TreeSetDemo2
{
	public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet(new MyCompare());

		ts.add(new Student("lisi02",22));
		ts.add(new Student("lisi04",22));
		ts.add(new Student("lisi02",22));
		ts.add(new Student("zhangsen",22));
		ts.add(new Student("lisi02",45));
		

		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			Student s = (Student)it.next();
			System.out.println(s.getName()+"----:"+s.getAge());
		}
	}
}

class Student implements Comparable
{
	private String name;
	private int age;
	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return 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 this.name.compareTo(s.name);
		return -1;
	}

}

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)
		{
			if (s1.getAge()>s2.getAge())
			return 1;
			if (s1.getAge()==s2.getAge())
			return 0;			
		}
		return num;
	}
}


二、泛类型

当类中要操作的引用数据类型不确定的时候。早期定义Object来完成扩展,JDK1.5版本以后出现的新特性。用于解决安全问题,是一个类型安全机制。定义泛型来完成扩展。如此一来,将运行时期问题转移到编译时期,也省去了类几下转型的步骤。

泛型类定义的泛型,在整个类中有效。如果被方法使用,那么泛型类的对象要明确操作具体类型后,所有要操作的类型就已经固定。为了让不同方法操作不同类型,而且类型还不确定。那么可以将泛型定义在函数上。当然也有特殊之处,那就是静态方法不可以访问类上定义的泛型。如果静态方法操作应用数据类型不确定,可以将泛型定义在方法上。

程序代码实现如下:

 

class Demo<T>
{
	public  void show(T t)
	{
		System.out.println("show----:"+t);
	}
	public <Q> void print(Q q)//可以重复用Q
	{
		System.out.println("show----:"+q);
	}
	public static <W> void method(W w)
	{
		System.out.println("method----:"+w);
	}
}
class GenericDemo4 
{
	public static void main(String[] args) 
	{
		Demo<String>  d = new Demo<String>();
		d.show("haha");
		d.print(12);
		d.print("dfdf");
		d.method("11gd");

		/*
		Demo<String>  d = new Demo<String>();
		d.show("haa");
		d.print("12311sfdkfj");
		Demo<Integer>  i = new Demo<Integer>();
		i.show(new Integer(8));
		i.print(11);//自动装箱
		i.print("heihei");//【错误: 不兼容的类型: String无法转换为Integer】
		*/
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值