question_013-JAVA之Set之TreeSet
2016-03-03 10:36
375 查看
question_013-JAVA之Set之TreeSet
TreeSet
------------------------------------------------------
TreeSet【唯一性和排序性】
强制对加入的元素进行排序【排序的方法:自然排序和比较器排序】
·········默认是自然排序,对于【基础类型,会默认自然排序】
·········实现Comparable<T>接口的compare()方法,比较器排序【自定义类型一般要自己使用比较器排序】
-----------------------------------------------------
1)
使用元素的自然顺序对元素进行排序
2)
或者根据创建 set 时提供的 Comparator进行排序具体取决于使用的构造方法。
---------------------------------------------
对于自定义对象的排序方式1
package com.lyTs;
/**
* 如果一个类的元素要实现自然排序,必须实现自然排序接口
*
* @author Jack
*
*/
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 实现比较接口,自己定义比较器
@Override
public int compareTo(Student o) {
// return -1;
// 名称相同吗,不同则加入
// 名称相同,则比较name
int num = (this.age - o.age);
int num2 = num == 0 ? this.name.compareTo(o.name): num;
return num2;
}
}
·············································
package com.lyTs;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
·····················结果···················
fengqi----6
zhangsan----18
lisi----21
wangba.----33
wangwu----33
zhaoliu----45
················································
-----------------------------------------------------
对于自定义对象的排序方式2
·······学生类中没有比较方法···············
package com.lyTsComparator;
/**
* @author Jack
*/
public class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
···············比较器··················
package com.lyTsComparator;
import java.util.Comparator;
public class MyComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// 姓名的长度相同
int num = o1.getName().length() - o2.getName().length();
// 姓名的长度相同,不代表内容相同
int num2 = num == 0? o1.getName().compareTo(o2.getName()):num;
int num3 = num2 == 0?o1.getAge() - o2.getAge():num2;
return num3;
}
}
···············测试··················
package com.lyTsComparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s7 = new Student("fengqi",33);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
```````````````j结果```````````````
lisi----21
fengqi----6
wangwu----33
wangba.----33
zhaoliu----45
zhangsan----18
-----------------------------------------------------
对于自定义对象的排序方式3
·······学生类中没有比较方法···············
package com.lyTsComparator;
/**
* @author Jack
*/
public class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
···············测试··················
使用了匿名比较器
package com.lyTsComparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
// 使用匿名内部类,来实现
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 姓名的长度相同
int num = o1.getName().length() - o2.getName().length();
// 姓名的长度相同,不代表内容相同
int num2 = num == 0? o1.getName().compareTo(o2.getName()):num;
int num3 = num2 == 0?o1.getAge() - o2.getAge():num2;
return num3;
}
});
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s7 = new Student("fengqi",33);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
```````````````j结果```````````````
lisi----21
fengqi----6
wangwu----33
wangba.----33
zhaoliu----45
zhangsan----18
··························································
TreeSet
------------------------------------------------------
TreeSet【唯一性和排序性】
强制对加入的元素进行排序【排序的方法:自然排序和比较器排序】
·········默认是自然排序,对于【基础类型,会默认自然排序】
·········实现Comparable<T>接口的compare()方法,比较器排序【自定义类型一般要自己使用比较器排序】
-----------------------------------------------------
1)
使用元素的自然顺序对元素进行排序
2)
或者根据创建 set 时提供的 Comparator进行排序具体取决于使用的构造方法。
---------------------------------------------
对于自定义对象的排序方式1
package com.lyTs;
/**
* 如果一个类的元素要实现自然排序,必须实现自然排序接口
*
* @author Jack
*
*/
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 实现比较接口,自己定义比较器
@Override
public int compareTo(Student o) {
// return -1;
// 名称相同吗,不同则加入
// 名称相同,则比较name
int num = (this.age - o.age);
int num2 = num == 0 ? this.name.compareTo(o.name): num;
return num2;
}
}
·············································
package com.lyTs;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
·····················结果···················
fengqi----6
zhangsan----18
lisi----21
wangba.----33
wangwu----33
zhaoliu----45
················································
-----------------------------------------------------
对于自定义对象的排序方式2
·······学生类中没有比较方法···············
package com.lyTsComparator;
/**
* @author Jack
*/
public class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
···············比较器··················
package com.lyTsComparator;
import java.util.Comparator;
public class MyComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// 姓名的长度相同
int num = o1.getName().length() - o2.getName().length();
// 姓名的长度相同,不代表内容相同
int num2 = num == 0? o1.getName().compareTo(o2.getName()):num;
int num3 = num2 == 0?o1.getAge() - o2.getAge():num2;
return num3;
}
}
···············测试··················
package com.lyTsComparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s7 = new Student("fengqi",33);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
```````````````j结果```````````````
lisi----21
fengqi----6
wangwu----33
wangba.----33
zhaoliu----45
zhangsan----18
-----------------------------------------------------
对于自定义对象的排序方式3
·······学生类中没有比较方法···············
package com.lyTsComparator;
/**
* @author Jack
*/
public class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
···············测试··················
使用了匿名比较器
package com.lyTsComparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
// 使用匿名内部类,来实现
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 姓名的长度相同
int num = o1.getName().length() - o2.getName().length();
// 姓名的长度相同,不代表内容相同
int num2 = num == 0? o1.getName().compareTo(o2.getName()):num;
int num3 = num2 == 0?o1.getAge() - o2.getAge():num2;
return num3;
}
});
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s7 = new Student("fengqi",33);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
```````````````j结果```````````````
lisi----21
fengqi----6
wangwu----33
wangba.----33
zhaoliu----45
zhangsan----18
··························································
相关文章推荐
- UICollectionView自定义布局教程:Pinterest (1)(翻译自raywenderlich)
- UICollectionView自定义布局教程:Pinterest (2)
- Glassfish Request URI is too large 问题的解决
- iOS开发UI篇—CAlayer层的属性
- 更换UITextView的换行键为完成键
- iOS个人整理20-UITableViewCell自定义,cell高度的自适应(纯代码)
- 使用StingBuffer、StringBuild、substring、replace对字符串的增加,删除 ,逆序输入
- IOS UIStepper(步进控件)使用总结
- 关于阿里巴巴开源数据源Druid的基础说明
- 使用UINavigationController后导致UIScollView尺寸变化
- SPOJ D-query 区间不同数的个数 [在线主席树 or 离线树状数组]
- question_012-JAVA之泛型通配符
- iOS状态栏UIStatusBar详解
- iOS7的适配小问题, uiscrollview中view向下偏移64
- HDU 2817 A sequence of numbers(快速幂)
- hdu---3177 Crixalis's Equipment 根据 两个元素 之间的权衡进行排序
- Build Active Architecture Only
- Break和Continue的一些注意事项
- NSAttributedString UIKit Additions 文章翻译参考
- =Cursor cursor = dbInstance.query(String table,String []Columns, String selection, String[]selectionArgs, String having, string orderBy);参数的意思