集合 --- TreeSet
2015-10-15 11:41
363 查看
一
二
练习一
练习二
import java.util.*; /* Set:无序,不可以重复元素。 |--HashSet:数据结构是哈希表。线程是非同步的。 保证元素唯一性的原理:判断元素的hashCode值是否相同。 如果相同,还会继续判断元素的equals方法,是否为true。 |--TreeSet:可以对Set集合中的元素进行排序。 底层数据结构是二叉树。 保证元素唯一性的依据: compareTo方法return 0. TreeSet排序的第一种方式:让元素自身具备比较性。 元素需要实现Comparable接口,覆盖compareTo方法。 也种方式也成为元素的自然顺序,或者叫做默认顺序。 TreeSet的第二种排序方式。 当元素自身不具备比较性时,或者具备的比较性不是所需要的。 这时就需要让集合自身具备比较性。 在集合初始化时,就有了比较方式。 需求: 往TreeSet集合中存储自定义对象学生。 想按照学生的年龄进行排序。 记住,排序时,当主要条件相同时,一定判断一下次要条件。 */ 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("lisi08",19)); //ts.add(new Student("lisi007",20)); //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 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) { //return 0; if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; System.out.println(this.name+"....compareto....."+s.name); if(this.age>s.age) return 1; if(this.age==s.age) { return this.name.compareTo(s.name); } return -1; /**/ } public String getName() { return name; } public int getAge() { return age; } }
二
import java.util.*; /* 当元素自身不具备比较性,或者具备的比较性不是所需要的。 这时需要让容器自身具备比较性。 定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。 当两种排序都存在时,以比较器为主。 定义一个类,实现Comparator接口,覆盖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) { //return 0; if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; //System.out.println(this.name+"....compareto....."+s.name); if(this.age>s.age) return 1; if(this.age==s.age) { return this.name.compareTo(s.name); } 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("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 = 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(s1.getAge()).compareTo(new Integer(s2.getAge())); /* if(s1.getAge()>s2.getAge()) return 1; if(s1.getAge()==s2.getAge()) return 0; return -1; */ } return num; } }
练习一
/* 练习:按照字符串长度排序。 字符串本身具备比较性。但是它的比较方式不是所需要的。 这时就只能使用比较器。 */ import java.util.*; class TreeSetTest { public static void main(String[] args) { TreeSet ts = new TreeSet(new StrLenComparator()); ts.add("abcd"); ts.add("cc"); ts.add("cba"); ts.add("aaa"); ts.add("z"); ts.add("hahaha"); 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; } }
练习二
/* "90 -7 0 18 2 45 4" 将字符串中的数值进行排序。使用TreeSet完成。 思路 1,将字符串切割。 2,可以将这些对象存入TreeSet集合。因为TreeSet自身具备排序功能。 */ import java.util.*; class TreeSetTest2 { public static void main(String[] args) { ArrayList al = new ArrayList(); String str = "90 -7 0 18 2 45 4"; String[] arr = str.split(" "); TreeSet ts = new TreeSet(); for(int x=0; x<arr.length; x++) { //ts.add(new Integer(arr[x])); ts.add(Integer.parseInt(arr[x]));// } System.out.println(ts); } }
相关文章推荐
- WPF之命令
- 禁止屏幕旋转并同时解决以至于导致Activity重启的方法
- fastclick
- [Flex] ButtonBar系列——labelFunction用户提供的函数,在每个项目上运行以确定其标签
- 对象的序列化
- 彻底理解ThreadLocal
- mysql 学习---->事件调度器
- UIViewController不dealloc問題
- Android成长笔记(2)——程序结构分析
- PhoneGap优化解决方案
- 阿里云CentOS7安装Docker
- 三代DeepID简介
- Comparable接口与Comparator接口
- The Constrained Application Protocol (CoAP)协议翻译
- Linux缓存释放
- UIWebView各种加载网页的方式
- 【GNU】WINDOWS平台之Objective-C开发(环境搭建)
- Tomcat – Java.Lang.OutOfMemoryError: PermGen Space
- ViewPage滑动背景渐变效果的Splash主页面
- AWR报告生成