黑马程序员——第15天——(API)集合框架(TreeSet,泛型)


Set:无序,不可以重复元素。
|--HashSet:数据结构是哈希表。线程是非同步的。
保证元素唯一性的原理:判断元素的hashcode值是否相同。
如果相同,还会继续判断元素的equals方法,是否为true.


|--TreeSet: 可以对Set集合中的元素进行排序。
底层数据结构是二叉树。
保证元素唯一性的依据:


compareTo方法return 0。

TreeSet排序的第一种方式:让元素自身具备比较性。
元素需要实现Compareable接口覆盖compareTo方法.
这种方式也成为元素的自然顺序,或者叫做默认顺序。

TreeSet的第二种排序方式。
当元素自身不具备比较性时,或者具备的比较性不是所需要的。
这时就需要让集合自身具备比较性。

在集合初始化时,就有了比较方式。

import java.util.*;

/*

需求:
往TreeSet集合中存储自定义对象学生。
想按照学生的年龄进行排序。


<strong>排序时,当主要条件相同时,一定要判断一下次要条件。</strong>
*/


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 = it.iterator();
		while(it.hasNext())
		{
			System.out.println(it.next());
		}
	}
}

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)
		{
			this.name.compareTo(s.name);//当主要条件相同时,一定要判断一下次要条件。
		}
		return -1;
	}

	public String getName()
	{
		return name;
	}

	public int getAge()
	{
		return age;
	}
}
<strong>比较器</strong>
import java.util.*;

/*
当元素自身不具备比较性,或者具备的比较性不是所需要的。
这是需要让容器自身具备比较性。
定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。

当两种排序都存在时,以比较器为主。

定义一个类,实现Comparetor接口,覆盖compare方法。


*/
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)
		{
			this.name.compareTo(s.name);//当主要条件相同时,一定要判断一下次要条件。
		}
		return -1;
	}

	public String getName()
	{
		return name;
	}

	public int getAge()
	{
		return age;
	}
}

class TreeSetDemo2
{
	public static voic main(String[] args);
	{
		TreeSet ts = new TreeSet(new MyCompare());

		ts.add(new Student("lisi02",22));
		ts.add(new Student("lisi02",21));
		ts.add(new Student("lisi007",20));
		ts.add(new Student("lisi09",19));
		ts.add(new Student("lisi06",18));
		ts.add(new Student("lisi06",18));
		ts.add(new Student("lisi007",29));
		//ts.add(new Student("lisi007",20));
		//ts.add(new Student("lisi01",40));

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

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)
		{
			return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge());
			/*
			if(s1.getAge()>s2.getAge())
				return 1;
			if(s1.getAge()==s2.getAge())
				return 0;
			return -1;
			*/		
		}
	}
}
<strong>练习</strong>:

按照字符串长度排序。

字符串本身具备比较性。但是它的比较方式不是所需要的。

这时就只能使用比较器。

再做一遍!!


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

		ts.add("abcd");
		ts.add("abcd");
		ts.add("abcd");
		ts.add("abcd");
		ts.add("abcd");

		Iterator it = ts.iterator();

		while(it.hasNext())
		{
			System.out.println(it.next());
		}
	}
}

class StrLenComparator implements Comparator
{
	public int compare(Object o1,Object o2)
	{
		String s1 = (String)o1;
		String s2 = (String)o2;
		
		/*
		if(s1.length()>s2.length())
			return 1;
		if(s1.length()==s2.length())
			return 0;
		*/



		int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
		
		if(num==0)
			return s1.compareTo(s2);
		
		return num;
	}
}
<strong>
<span style="font-size:32px;">泛型:</span>
</strong>
JDK1.5版本以后出现的新特性,用于解决安全问题。是一个类型安全机制。

好处
1,将运行时期出现的问题ClassCastException,转移到了编译时期。
	方便于程序员解决问题。让运行时期问题减少,安全。
2,避免了强制转换麻烦。


泛型格式:通过<>来定义要操作的引用数据类型。

在使用java提供的对象时,什么时候写泛型呢?

通常在集合框架张很常见。
只要见到<>就要定义泛型

其实<>就是用来接收类型的。

当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可。


import java.util.*;

class GenericDemo 
{
	public static void main(String[] args) 
	{		
		ArrayList<String> al = new ArrayList<String>();

		al.add("abc01");
		al.add("abc0991");
		al.add("abcc014");

		//al.add(4);//al.add(new Integer(4));自动拆箱装箱动作
		
		Iterator<String> it = al.iterator();
		while(it.next())
		{
			String s = (String)it.naxt();

			System.out.println(s+":"+s.length());
		}
	}
}

import java.util.*;
class GenericDemo2 
{
	public static void main(String[] args) 
	{
		TreeSet<String> ts = new TreeSet<String>();
		ts.add("abcd");
		ts.add("cc");
		ts.add("cba");
		ts.add("aaa");
		ts.add("hahaha");

		Iterator<String> it = ts.iterator();
		
		while(it.hasNext())
		{
			String s = it.next();
			System.out.println(s);
		}
	}
}

class LenComparator implements Comparator
{
	public int compare(Object o1,Object o2)
	{
		int num = new Integer(o2.length()).compareTo(new Integer(o1.length()));

		if(num==o)
			return o2.compareTo(o1);
		return num;
	}
}
泛型的出现可以避免强转,将错误转移到编译时期。
/*
class Tool
{
	private Worker w;
	public void setWorker(Worker w)
	{
		this.w = w;
	}
	public Worker getWorker()
	{
		return w;
	}
}
*/

class Worker
{

}

class Student
{
}
//泛型前做法。
class Tool
{
	private Object obj;
	public void setObject(Object obj)
	{
		this.obj = obj;
	}
	public Object getObject()
	{
		return obj;
	}
}
//泛型类。
/*
什么时候定义泛型类?
当类中要操作的引用数据类型不确定的时候,
早期定义Object完成扩展。
现在定义泛型来完成扩展。
*/
class Utils<QQ>
{
	private QQ q;
	public void setObject(QQ q)
	{
		this.q = q;
	}
	public QQ getObject()
	{
		return q;
	}
}

class GenericDemo3 
{
	public static void main(String[] args) 
	{
		Utils<Worker> u = new Utils<Worker>();

		u.setObject(new Worker());
		Work w = (Worker)u.getObject();
		/*
		Tool t = new Tool();
		t.setObject(new Student());
		Wroker w = (Worker)t.getObject();
		*/
	}
}

/*
class Demo<T> 
{
	public void show(T t)
	{
		System.out.println("show:"+t);
	}
	public void print(T t)
	{
		System.out.println("print:"+t);
	}

}
*/
//泛型类定义的泛型,在整个类中有效,如果被方法使用。
//那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。
//
//为了让不同方法可以操作不同类型,而且类型还不确定。
//那么可以将泛型定义在方法上。


/*
特殊之处:
静态方法不可以访问类上定义的泛型。
如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。
*/
class Demo<T>
{
	public void show(T t)
	{
		System.out.println("show:"+t);
	}
	public <Q> void print(Q q)//写成<T>不影响
	{
		System.out.println("print:"+q);
	}
	public static <W> void method(W t)//泛型定义在方法上,放在返回值前,
	{
		System.out.println("method:"+t);
	}
}
/*
<span style="font-size:32px;">不懂啊,什么类型???</span>
*/
class GenericDemo4
{
	public static void main(String[] args) 
	{
		Demo d = new Demo();
		d.show("haha");
		d.show(new Integer(4));
		d.print("heihei");

		Demo.method("hahahah");
		
		/*
		Demo<String> d = new Demo<String>();

		d.show("haha");
		d.print("hehe");
		*/
	}
}
<strong>
泛型定义在接口上。</strong>
interface Inter<T>
{
	void show(T t);
}
/*
class InterImp implements Inter<String>
{
	public void show(String t)
	{
		System.out.println("show:"+t);
	}
}
*/
class InterImpl<T> implement Inter<T>
{
	public void show(T t)
	{
		Systrm.out.println("show:"+t);
	}
}
class GenericDemo5 
{
	public static void main(String[] args) 
	{
		InterImpl<Integer> i = new InterImpl<Integer>;
		i.show(4);
	//	InterImpl i = new InterImpl();
	//	i.show("haha");
	}
}
<pre name="code" class="java" style="background-color: rgb(255, 255, 255); ">
<strong>?通配符,也可理解为占位符。</strong>
 
import java.util.*;
/*
?通配符,也可理解为占位符。
泛型的限定:
? extends E:可以接收E类型或者E的子类型。上限
? super E:可以接收E类型或者E的父类型。下限

*/
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(7);
		al1.add(1);
		
		printcoll(al);
		printColl(all);
		*/

		ArrayList<Person> al = new ArrayList<Person>();
		al.add(new Person("abc1"));
		al.add(new Person("abc2"));
		al.add(new Person("abc3"));
		//printColl(al);

		ArrayList<Student> al1 = new ArrayList<Student>();
		al1.add(new Student("abc--1"));
		al1.add(new Student("abc--2"));
		al1.add(new Student("abc--3"));
		printColl(al1);//ArrayList<Person> al = new ArrayList<Student>();//error
	}
	public static void printColl(ArrayList<? extends Person> al)
	{
		Iterator<? extends Person> it = al.iterator();
		while(it.hasNext())
		{
			System.out.println(it.next().getName());	
		}
	}
/*
	public static void printColl(ArrayList<?> al)//ArrayList<String> al = new ArrayList<Integer>();
//	public static <T> void printColl(ArrayList<T> al)区别是<T>可以在下面进行操作,<?>不可以
	{
		Iterator<?> it = al.iterator();
		while(it.hasNext())
		{
			System.out.println(it.next());	
		}
	}*/
}

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 Student implements Comparable<Student>//<? super E>
{
	public int compareTo(Student s)
	{
		this.getName()
	}
}

class Comp implements Comparator<Person>
{
	public int compare(Person s1,Person s2)
	{
		Person s1 = new Student("abc1");
		return s1.getName().compareTo(s2.getName());
	}
}

TreeSet<Student> ts = new TreeeSet<Student>(new Comp());
ts.add(new Student("abc1"));
ts.add(new Student("abc2"));
ts.add(new Student("abc3"));


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值