集合框架-二叉树-实现Comparator方式排序
2014-08-01 09:04
344 查看
当元素自身不具备比较性,或者具备的比较性不是所需要的
这时需要让容器自身具备比较性。
定义了比较器,将比较器对象作为参数传递给TreeSet集合构造函数
当两种排序都存在时,以比较器为主
定义一个类,实现Comparator接口,覆盖compare方法import java.util.*;
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;
System.out.println(this.name+"...compateto..."+s.name);
if(this.age>s.age)
return 1;
if(this.age==s.age)
return 0;
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class TreeSetDemo2
{
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("lisi08",19));
// ts.add(new Student("lisi01",40));
Iterator it = ts.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()
}
}
}
这时需要让容器自身具备比较性。
定义了比较器,将比较器对象作为参数传递给TreeSet集合构造函数
当两种排序都存在时,以比较器为主
定义一个类,实现Comparator接口,覆盖compare方法import java.util.*;
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;
System.out.println(this.name+"...compateto..."+s.name);
if(this.age>s.age)
return 1;
if(this.age==s.age)
return 0;
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class TreeSetDemo2
{
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("lisi08",19));
// ts.add(new Student("lisi01",40));
Iterator it = ts.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()
}
}
}
相关文章推荐
- TreeSet集合排序两种实现方式Comparable和Comparator比较
- JAVA之旅(二十)—HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习
- 黑马程序员_day15 TreeSet,二叉树,实现Comparator方式排序,泛型,泛型接口,泛型限定)
- 集合框架——Set(TreeSet实现Comparator方法排序)
- TreeSet集合排序两种实现方式Comparable和Comparator比较
- JAVA之旅(二十)—HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习
- TreeSet集合排序两种实现方式Comparable和Comparator比较
- TreeSet集合排序方式二:定制排序Comparator
- java中Comparator的用法 -- 实现集合和数组排序
- 使用Comparable、Comparator接口实现对对象数组、List集合自定义排序
- 黑马程序员------TreeSet集合框架存储自定义元素之排序Comparable与Comparator
- Java基础-实现Comparator方式排序
- java程序——用集合框架实现学生类的排序输出
- JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序
- JAVA基础再回首(十七)——Set集合、增强for、HashSet类、LinkedHashSet类、TreeSet类、二叉树、Comparator 排序
- JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序
- 集合框架_模拟斗地主洗牌和发牌并对牌进行排序的代码实现
- 黑马程序员--07.集合框架--08.【TreeSet的Comparator排序】【TreeSet总结】
- 插入排序与冒泡排序集合框架实现
- Java基础---集合框架---迭代器、ListIterator、Vector中枚举、LinkedList、ArrayList、HashSet、TreeSet、二叉树、Comparator