热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

集合排序TreeSet

从视频中学得:集合排序需要注意两点:(1)传入对象要自己能排序(自己提供排序方法);

从视频中学得:

集合排序需要注意两点:

(1)传入对象要自己能排序(自己提供排序方法);

(2)要使用排序类

下面两个类:

1、自己定义的类

package com.collection.test1;

public class UserModule1 implements Comparable{
// 定义一些属性
private String userId, name;
private int age;

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

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;
}

//重写hashCode和equals,不用每个都比较,一般只比较userId就行了
@Override
public int hashCode() {
System.out.println(
"call hashCode...");
final int prime = 31;
int result = 1;
result
= prime * result + ((userId == null) ? 0 : userId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
System.out.println(
"call equals...");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserModule1 other
= (UserModule1) obj;
if (userId == null) {
if (other.userId != null)
return false;
}
else if (!userId.equals(other.userId))
return false;
return true;
}

//覆盖toString
@Override
public String toString(){
return "userId=="+userId+",age=="+age+",name=="+name;
}

@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
System.out.println("call compareTo");
//1、对象强制造型
UserModule1 us1= (UserModule1)o;
//2、比较
System.out.println("this.getUserId()=="+this.getUserId()+",us1.getUserId()=="+us1.getUserId());
if(this.getUserId().compareTo(us1.getUserId())>0){
System.out.println(
">>>>");
return 1;
}
else if(this.getUserId().compareTo(us1.getUserId())==0){
System.out.println(
"====");
return 0;
}
else{
System.out.println(
"<<<");
return -1;
}

}
}

Collection类

package com.collection.test1;

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

public class CollectionTest4 {
public static void main(String args[]) {
addMethod();
}

private static void addMethod() {
//Collection col &#61; new ArrayList();
Collection col &#61; new TreeSet();
//Collection col &#61; new HashSet();
UserModule1 user1 &#61; new UserModule1();
user1.setAge(
1);
user1.setName(
"suus");
user1.setUserId(
"1");
System.out.println(
"call set11");
// 调用add&#xff0c;就会调用hashCode
col.add(user1);


UserModule1 user2
&#61; new UserModule1();
user2.setAge(
2);
user2.setName(
"suus2");
user2.setUserId(
"5");
System.out.println(
"call set22");
col.add(user2);


UserModule1 user3
&#61; new UserModule1();
user3.setAge(
3);
user3.setName(
"suus3");
user3.setUserId(
"6");
System.out.println(
"call set33");
col.add(user3);

// 2enum,Iterator是一个接口&#xff0c;不能直接new&#xff0c;
Iterator it &#61; col.iterator();
while (it.hasNext()) {
// 得到的it是Object类型的
UserModule1 a &#61; (UserModule1) it.next();
// 如果要想打印自己定义的类的各个对象&#xff0c;必须要重写toString方法。打印对象的时候&#xff0c;调用toString方法
System.out.println("a&#61;&#61;" &#43; a);
}

}

}

如果UserModule类不实现Comparable接口的话&#xff0c;在CollectionTest4 中直接用TreeSet&#xff0c;运行结果会出错&#xff1a;

如下&#xff1a;

 

要想用TreeSet&#xff0c;UserModule必须提供比较的方法&#xff0c;所以必须implements Comparable。

注意&#xff1a;

&#xff08;1&#xff09;对于在UserModule中重写的equals方法和hashCode方法&#xff0c;当在Collection4中&#xff0c;new HashSet的时候&#xff0c;才需要重写&#xff0c;new其他的子类的时候&#xff0c;不需要重写&#xff01;&#xff01;

&#xff08;2&#xff09;要想能打印出对象的各个属性值&#xff0c;必须要重写toString方法&#xff0c;对new任何子类都是如此。

代码如上贴的时候&#xff0c;运行结果为&#xff1a;

call set11
call set22
call compareTo
this.getUserId()&#61;&#61;5,us1.getUserId()&#61;&#61;1
>>>>
call set33
call compareTo
this.getUserId()&#61;&#61;6,us1.getUserId()&#61;&#61;1
>>>>
call compareTo
this.getUserId()&#61;&#61;6,us1.getUserId()&#61;&#61;5
>>>>
a&#61;&#61;userId&#61;&#61;1,age&#61;&#61;1&#xff0c;name&#61;&#61;suus
a&#61;&#61;userId&#61;&#61;5,age&#61;&#61;2&#xff0c;name&#61;&#61;suus2
a&#61;&#61;userId&#61;&#61;6,age&#61;&#61;3&#xff0c;name&#61;&#61;suus3

 

要想实现倒序排列&#xff0c;在返回1 的时候返回-1&#xff0c;在返回-1的地方返回1即可。

 如果把上面的new TreeSet改成 new HashSet&#xff0c;运行结果如下&#xff1a;

call set11
call hashCode...
call set22
call hashCode...
call set33
call hashCode...
call set44
call hashCode...
call set5
call hashCode...
call set56
call hashCode...
a&#61;&#61;userId&#61;&#61;6,age&#61;&#61;2&#xff0c;name&#61;&#61;suus2
a&#61;&#61;userId&#61;&#61;5,age&#61;&#61;3&#xff0c;name&#61;&#61;suus3
a&#61;&#61;userId&#61;&#61;2,age&#61;&#61;4&#xff0c;name&#61;&#61;suus4
a&#61;&#61;userId&#61;&#61;1,age&#61;&#61;1&#xff0c;name&#61;&#61;suus
a&#61;&#61;userId&#61;&#61;4,age&#61;&#61;6&#xff0c;name&#61;&#61;suus6
a&#61;&#61;userId&#61;&#61;3,age&#61;&#61;5&#xff0c;name&#61;&#61;suus5

 

与加入顺序无关

 

如果改成new ArrayList&#xff0c;运行结果如下&#xff1a;

call set11
call set22
call set33
call set44
call set5
call set56
a&#61;&#61;userId&#61;&#61;1,age&#61;&#61;1&#xff0c;name&#61;&#61;suus
a&#61;&#61;userId&#61;&#61;6,age&#61;&#61;2&#xff0c;name&#61;&#61;suus2
a&#61;&#61;userId&#61;&#61;5,age&#61;&#61;3&#xff0c;name&#61;&#61;suus3
a&#61;&#61;userId&#61;&#61;2,age&#61;&#61;4&#xff0c;name&#61;&#61;suus4
a&#61;&#61;userId&#61;&#61;3,age&#61;&#61;5&#xff0c;name&#61;&#61;suus5
a&#61;&#61;userId&#61;&#61;4,age&#61;&#61;6&#xff0c;name&#61;&#61;suus6

打印出来的元素顺序与加入顺序相同

转:https://www.cnblogs.com/snowdrop/articles/2140439.html



推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
author-avatar
只是遇不到他_740
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有