TreeSet使用时注意问题
来源:互联网 发布:java 逐层截取路径 编辑:程序博客网 时间:2024/05/19 00:54
TreeSet是依靠TreeMap来实现的。
TreeSet是一个有序集合,TreeSet中的元素将按照升序排列,缺省是按照自然排序进行排列,意味着TreeSet中的元素要实现Comparable接口。或者有一个自定义的比较器。
我们可以在构造TreeSet对象时,传递实现Comparator接口的比较器对象。
TreeSet的排序是在添加元素的时候进行排序的,即add(元素)的时候,如果在添加了元素以后,再对集合里面的数据进行修改,则会导致TreeSet集合中的元素重复,举个例子说明一下:
新建一个Persion类
public class Persion {
private String pname;//名字
private int hands;//帅气指数
public Persion() {
// TODO Auto-generated constructor stub
}
public Persion(String pname, int hands) { this.pname = pname; this.hands = hands;}public int getHands() { return hands;}public void setHands(int hands) { this.hands = hands;}public String getPname() { return pname;}public void setPname(String pname) { this.pname = pname;} @Overridepublic String toString() { return "姓名:"+this.pname+"帅气指数"+this.hands;}
}
新建一个TestTreeSet测试类:
package com.mybatias.model;
import java.util.Comparator;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) { TreeSet<Persion> treeset=new TreeSet<Persion>(new Comparator<Persion>() { public int compare(Persion o1, Persion o2) { int p1=o1.getHands(); int p2=o2.getHands(); return p1-p2; } }); Persion p1=new Persion("张三",100); Persion p2=new Persion("李四",50); Persion p3=new Persion("你",1000); Persion p4=new Persion("小明",900); treeset.add(p1); treeset.add(p2); treeset.add(p3); treeset.add(p4); System.out.println("修改对象p4前的排序输出结果:"); System.out.println(treeset); p4.setPname("张三"); p4.setHands(100); System.out.println("修改对象p4后的排序输出结果:"); System.out.println(treeset); }
}
测试结果:
修改对象p4前的排序输出结果:
[姓名:李四帅气指数50, 姓名:张三帅气指数100, 姓名:小明帅气指数900, 姓名:你帅气指数1000]
修改对象p4后的排序输出结果:
[姓名:李四帅气指数50, 姓名:张三帅气指数100, 姓名:张三帅气指数100, 姓名:你帅气指数1000]
对比发现对排序后的TreeSet集合中的数据进行修改后,输出结果有重复数据,显然违背了Set集合中的数据不能重复的规则。
通过上面的分析可以通过如下方式解决:
将属性值定义为final类型的
public class Persion {
//定义为final类型的属性时,属性的初始化时必须要有值
private final String pname;
private final int hands;
//Alt+/
public Persion() {
pname=null;//pname为final类型,初始化的时候,给属性赋值null
hands=0; //hands初始化值为0
}
//Alt+Shift+S,o,回车
public Persion(String pname, int hands) {
this.pname = pname;
this.hands = hands;
}
//快捷键 Alt+Shift+S+R,空格,回车
public int getHands() {
return hands;
}
//public void setHands(int hands) {
// this.hands = hands;
//}
public String getPname() {
return pname;
}
//public void setPname(String pname) {
// this.pname = pname;
//}
@Overridepublic String toString() { // TODO Auto-generated method stub return "姓名:"+this.pname+"帅气指数"+this.hands;}
}
package test;
import java.util.Comparator;
import java.util.TreeSet;
public class TestTreeSet {
/** * @Description : * @return : void * @Creation Date : 2016-1-15 上午8:59:11 * @Author : */public static void main(String[] args) { TreeSet<Persion> treeset=new TreeSet<Persion>(new Comparator<Persion>() { public int compare(Persion o1, Persion o2) { int p1=o1.getHands(); int p2=o2.getHands(); return p1-p2; } }); Persion p1=new Persion("张三",100); Persion p2=new Persion("李四",50); Persion p3=new Persion("你",1000); Persion p4=new Persion("小明",900); treeset.add(p1); treeset.add(p2); treeset.add(p3); treeset.add(p4); System.out.println(treeset); //为了避免有重复数据,将属性的类型定义为final类型的,不能对属性值进行修改 //p4.setPname("张三"); // p4.setHands(200); //System.out.println(treeset); }
}
通过上面这样方式的修改,可以有效的避免TreeSet中的重复数据。
- TreeSet使用时注意问题
- TreeSet使用时注意问题
- 从头认识java-15.4 使用TreeSet需要注意的地方
- TreeSet使用
- 使用TreeSet
- 使用fopen时应注意的问题
- 使用WaitForMultipleObjects时注意的问题
- 使用Sql时应注意的问题
- 使用ProGuard时需要注意的问题
- Hibernate中使用dao时注意问题
- ssh 使用时应该注意的问题
- 指针使用时需要注意的问题
- NSMutableArray设置值,使用时注意问题
- 使用数组时注意的一些问题
- 使用cloneNode时需要注意的问题
- 使用pthread时需要注意的问题
- 使用cvCanny函数时注意的问题
- 使用iperf时需要注意的问题
- 手把手教你使用UltraISO软碟通制作win10 Enterprise G企业政府版ISO安装镜像
- 大数据生态系统基础:Apache Kafka基础(三):最新kafka编程入门:Consumer
- 旋转矩阵、欧拉角、四元数比较
- 设计模式--职责链模式
- python错误:UnicodeDecodeError: 'ascii' codec can't decode
- TreeSet使用时注意问题
- CentOS 安装配置LAMP服务器(Apache+PHP5+MySQL)
- 程序员都有一颗摇滚的心
- 实验吧-看起来有点难【基于sleep的sql注入脚本】
- Java 反射在实际开发中的应用
- HMM和CRF简单推导
- 综合---小知识点
- 已知字符串msg='get-element-by-id';,写一个函数将其转化成驼峰表示法:'getElementById'
- Python3.5 文档学习笔记