TreeSet的实现:

TreeMap实现:

需要注意:

1. 当利用comparator比较两个元素相等时,插入的时候会失败。而hashset是发现两个元素相等(即:两个元素的hashcode相同,equals方法比较之后也相等)时,插入失败返回false。这说明可能treeset并不是真正的set,因为即使两个元素的hashcode相同,equals方法比较之后也相等,但是如果comparator返回两个对象不相等,也是可以插入的,而hashset插入事则会失败。

2. 排序只在插入时发生,如果后面你修改了元素的数据,元素的位置不会变。

测试类:

ChineseCharCount, 保存一个字符和它出现的次数。本来想测试读取一个文件中的所有中文字符,然后得到出现最多的字符以及出现了多少次的。本想用treeset进行排序,但是发现不行,最后还是直接用的ArrayList,然后用Collections的sort

(Comparator comparator)方法完成的。

package _00_ReadAndCountChineseCharacters;

public class ChineseCharCount {

char ch;

int count;

public ChineseCharCount(char ch, int count) {

// TODO Auto-generated constructor stub

this.ch = ch;

this.count = count;

}

public char getCh() {

return ch;

}

public void setCh(char ch) {

this.ch = ch;

}

public void setCount(int count) {

this.count = count;

}

public String toString() {

return ch + ":appeared " + count + " times";

}

public int getCount() {

return count;

}

@Override

public boolean equals(Object obj) {

// TODO Auto-generated method stub

if (obj == null) {

return false;

}

if (obj == this) {

return true;

}

if (!(obj instanceof ChineseCharCount)) {

return false;

}

ChineseCharCount te = (ChineseCharCount) obj;

if (te.ch == this.ch) {

return true;

}

return false;

}

@Override

public int hashCode() {

// TODO Auto-generated method stub

return (ch + "").hashCode();

}

}

ChineseCharComparator,直接通过比较count来对比大小。

package _00_ReadAndCountChineseCharacters;

import java.util.Comparator;

public class ChineseCharComparator implements Comparator{

public int compare(ChineseCharCount o1, ChineseCharCount o2) {

// TODO Auto-generated method stub

if(o1 == o2)

return 0;

if(o1!=null&&o2==null)

return -1;

else if(o1==null&&o2!=null)

return 1;

if (o1.count < o2.count)

return 1;

else if (o1.count > o2.count)

return -1;

else

return 0;

}

}

ReadAndCountChineseCharacters,测试类。直接读取文件,count,然后排序,输出前三。

package _00_ReadAndCountChineseCharacters;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashSet;

import java.util.List;

import java.util.TreeSet;

public class ReadAndCountChineseCharacters {

public static void main(String[] args) throws Exception {

String classPath = ReadAndCountChineseCharacters.class.getResource("").getPath();

System.out.println(classPath);

String filePath = classPath + "ChineseSentence.txt";

// InputStream in = new FileInputStream(filePath);

List chList = new ArrayList();

FileReader fr = new FileReader(filePath);

int i;

while ((i = fr.read()) != -1) {

char next = (char) i;

if(next == '\n' || next == '\t')

continue;

boolean hasCh = false;

for (ChineseCharCount chCount : chList) {

if (next == chCount.ch) {

hasCh = true;

chCount.count = chCount.count + 1;

break;

}

}

if (!hasCh) {

chList.add(new ChineseCharCount(next, 1));

}

}

fr.close();

ChineseCharComparator comparator = new ChineseCharComparator();

Collections.sort(chList, comparator);

for (int j = 0; j < 3; j++) {

System.out.println(chList.get(j));

}

}

}

TestTreeSet 编写排序时,遇到的问题,通过下面的测试类进行了总结:

package _00_ReadAndCountChineseCharacters;

import java.util.HashMap;

import java.util.HashSet;

import java.util.TreeSet;

public class TestTreeSet {

public static void main(String[] args) {

ChineseCharCount ch = new ChineseCharCount('a', 1);

ChineseCharCount ch2 = new ChineseCharCount('a', 2);

ChineseCharCount ch3 = new ChineseCharCount('a', 1);

ChineseCharCount ch4 = new ChineseCharCount('c', 1);

ChineseCharComparator comparator = new ChineseCharComparator();

TreeSet treeSet = new TreeSet<>(comparator);

HashSet hashSet = new HashSet<>();

HashMap haspMap = new HashMap<>();

/*

* Associates the specified value with the specified key in this map.

* If the map previously contained a mapping for the key, the old value is replaced.

*

* the previous value associated with key, or null if there was no mapping for key.

* (A null return can also indicate that the map previously associated null with key.)

*/

haspMap.put("1", "1");

System.out.println("abc123".hashCode());

System.out.println("".hashCode());

System.out.println(new String("abc123").hashCode());

System.out.println(new String("").hashCode());

/*

* Adds the specified element to this set if it is not already present.

* More formally, adds the specified element e to this set if the set

* contains no element e2 such that (e==null ? e2==null : e.equals(e2)).

* If this set already contains the element, the call leaves the set unchanged and returns false.

*/

System.out.println("add into treeset");

System.out.println(treeSet.add(ch));

System.out.println(treeSet.add(ch2));

System.out.println(treeSet.add(ch3));

System.out.println(treeSet.add(ch4));

/*

* Adds the specified element to this set if it is not already present.

* More formally, adds the specified element e to this set if this set

* contains no element e2 such that (e==null ? e2==null : e.equals(e2)).

* If this set already contains the element, the call leaves the set unchanged and returns false.

*/

System.out.println("add into hashSet");

System.out.println(hashSet.add(ch));

System.out.println(hashSet.add(ch2));

System.out.println(hashSet.add(ch3));

System.out.println(hashSet.add(ch4));

System.out.println("From the test result we can see, "

+ "treeset is not really a set. AS it allowed duplicate "

+ "attribute(determinter by hashCode and equals method). "

+ "Instead, it didn't allwed two attributes wihich is "

+ "equals to each other(determined by the comparator).");

/*

* 从测试结果可以看出,如果两个元素利用comparator进行比较是相等的插入会失败。

*/

for(ChineseCharCount chs : treeSet)

System.out.println(chs);

treeSet.first().count = 0;

treeSet.last().count = 2;

System.out.println(treeSet.add(ch3));

/*

* 从测试结果可以看出,treeset只会在存入set的时候进行排序,

* 当做了更改时不会重新排序。

*/

for(ChineseCharCount chs : treeSet)

System.out.println(chs);

}

}

java自动排序_java中的自动排序集合 ---- 20160809相关推荐

  1. java map 自动排序_Java中Map的排序

    Map的种类 在Java中,Map的主要作用是存储键值对.由于是根据键得到值,所以不允许键重复.它主要有如下几个类别: HashMap: 最常用的Map,它根据键的HashCode值存储数据,根据键可 ...

  2. java 排序_Java中常见的排序算法有哪些?---选择排序

    排序相关的的基本概念 排序: 将一组杂乱无章的数据按一定的规律顺次排列起来. 数据表( data list): 它是待排序数据对象的有限集合. 排序码(key):通常数据对象有多个属性域, 即多个数据 ...

  3. java中queue排序_Java中常见的排序算法有哪些?---选择排序

    排序相关的的基本概念 排序: 将一组杂乱无章的数据按一定的规律顺次排列起来. 数据表( data list): 它是待排序数据对象的有限集合. 排序码(key):通常数据对象有多个属性域, 即多个数据 ...

  4. java util 排序_Java中常见的排序方法

    本博主要介绍Java中几种常见的排序算法: /* 排序方法的演示 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分 ...

  5. java合并排序_Java中的合并排序算法

    合并排序算法是一种分而治之的算法.在分而治之的范式中,一个问题被分解成较小的问题,其中每个小问题仍然保留着大问题的所有属性--大小除外.为了解决原始问题,每个部分都是单独解决的,然后这些部分又合并在一 ...

  6. java自然排序_Java中的自然排序顺序字符串比较 - 是内置的吗?

    String实现了Comparable,这就是Java中的自然顺序(使用类似的接口进行比较).您可以将字符串放在TreeSet中,也可以使用Collections或Arrays类进行排序. 但是,在您 ...

  7. java线程排序_Java中的并发排序

    我目前正在开发一个程序来同时对字符串进行排序.我的程序接收一个文件,将文件的每一行读入一个数组,并将字符串数组拆分成较小的字符串数组.然后,程序为每个较小的数组启动一个线程,并对它们进行快速排序.每个 ...

  8. java list 排序_Java中List的排序

    场景 Bean定义如下,仅有一个类型为Integer的age字段. @NoArgsConstructor @AllArgsConstructor(staticName = "of" ...

  9. java中对象排序_java中 对象的排序

    1:排序类 package com.tixa.bad.customer.util; import java.util.ArrayList; import java.util.Collections; ...

最新文章

  1. 如何解决android 通知栏不显示的问题
  2. linux卸载minicoda2,MiniConda2下载 MiniConda python 2.7 v4.3.30.2 Linux 64位 官方免费版(附安装步骤) 下载-脚本之家...
  3. android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例
  4. 各种渲染软件设计理论详解
  5. Spark中的数据本地性
  6. LINUX编译java3d/j3d
  7. 国内遥感卫星资源综述
  8. 计算机主机时间不保存,电脑主板系统时间不能保存
  9. win7在网上邻居上看不到别的电脑如何解决
  10. stack容器—C++笔记
  11. Laravel Eloquent 必备的实用技巧
  12. 盘点2018年化工行业大事故!回顾那些令人心痛的瞬间......
  13. EXFO MAX-710B光时域反射仪OTDR主要功能
  14. WebService 及java网络编程等基础概念(一)
  15. 大华nvr服务器返回消息错误,GB/T28181协议EasyGBS问题解决对接大华硬盘录像机NVR时查询录像失败...
  16. jason字符串解析
  17. FreeSwitch调试小技巧
  18. 酷狗android平板,安卓平板车载共享 体验酷狗音乐HD版
  19. 【数学建模】第一篇 matlab安装及基本介绍
  20. 用dblink能修改_oracle dblink设置

热门文章

  1. NLP情感分析笔记(五):多类型情感分析
  2. 从三大方面全面解析物联网卡
  3. java 保存 设置_java的保存按钮怎么设置?
  4. python字符串常用函数-Python字符串常用函数详解
  5. dart 语言中的列表(List)
  6. php 入库乱码,解决php 中文字符入库或显示乱码的简单示例
  7. php mysql追踪器_zf框架的数据库追踪器使用示例
  8. 叠螺机_叠螺机如何实现全自动喷淋维护
  9. mysql 6.2 安装教程_CentOS 6.2 安装 MySQL 5.7.28的教程(mysql 笔记)
  10. torch的使用笔记