当前位置:网站首页>The problem of storing elements in TreeSet collection
The problem of storing elements in TreeSet collection
2022-07-27 02:25:00 【Current affairs reader hjj】
List of articles
TreeSet Problems with collection storage elements
Use TreeSet Set to test
/** * @Author Handsome * @Date 2022/6/20 17:24 * @Version 1.0. * * TreeSet Problems with collection storage elements */
class Person{
String name;
public Person(String name) {
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
TreeSet<String> set1 = new TreeSet<>();
set1.add(new String("hjj"));
set1.add(new String("hjj"));
System.out.println("set1 The size is :"+set1.size());
TreeSet<Person> set2 = new TreeSet<>();
set2.add(new Person("hjj"));
set2.add(new Person("hjj"));
System.out.println("set2 The size is :"+set1.size());
}
}
Running results
set1 The size is :1
Exception in thread "main" java.lang.ClassCastException: class com.question.Person cannot be cast to class java.lang.Comparable (com.question.Person is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
at java.base/java.util.TreeMap.compare(TreeMap.java:1291)
at java.base/java.util.TreeMap.put(TreeMap.java:536)
at java.base/java.util.TreeSet.add(TreeSet.java:255)
at com.question.Test.main(Test.java:24)
set1 The size is 1 Easier to understand
because TreeSet It's disordered and not repetitive , Cannot store the same element , namely set1 The size is 1
What then? set2 Will report a mistake ?
because compareTo yes TreeSet The underlying comparison rules , From Compareable Interface .
Not every object has compareTo Method , Need to achieve Compareable It's the interface compareTo Method . Want to save objects into Tree Collection , This object needs to be implemented Compareable Interface , rewrite compareTo The way to do it . Otherwise, put the object directly into the interface ,Tree The collection will call the object compareTo Method , It turns out that there's no way , Will appear
java.lang.ClassCastException
Rethinking : If you will TreeSet Replace with HashSet, What is the result ?
Use HashSet Set to test
/** * @Author Handsome * @Date 2022/6/20 17:24 * @Version 1.0 * * HashSet Problems with collection storage elements */
class Person{
String name;
public Person(String name) {
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
HashSet<String> set1 = new HashSet<>();
set1.add(new String("hjj"));
set1.add(new String("hjj"));
System.out.println("set1 The size is :"+set1.size());
HashSet<Person> set2 = new HashSet<>();
set2.add(new Person("hjj"));
set2.add(new Person("hjj"));
System.out.println("set2 The size is :"+set2.size());
}
}
Running results
set1 The size is :1
set2 The size is :2
set1 Cannot store the same element , therefore set1 The size is 1
set2 Why not report an error and the result is 2?
because HashSet Based on the hashCode() and equals() Method to determine whether the object is the same element ,String Rewrote hashCode() and equals() Method , That is to compare whether the strings are the same ,Pserson Not rewritten hashCode() and equals() Method , That is, the address is compared , Address different , So it's not the same object .
summary
HashSet Based on the hashCode() and equals() Method de duplication
TreeSet According to the object compareTo() Method de duplication
My learning forum
HandsomeForum: use Java Prepared Learning Forum , Build our own circle !(http://huangjunjie.vip:66)
The article links :http://huangjunjie.vip:66/blog/read/oviupvehzj312b0n8n
边栏推荐
- C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
- Golang implements TCP chat room
- NB-IOT接入云平台
- CF 1333C Eugene and an array
- 勤写标兵——云小井
- NAT网络地址转化实验
- Tim output comparison - PWM
- C language - value range of data type and basic data type
- Esp8266wi fi access cloud platform
- HCIA Basics (1)
猜你喜欢

Sort the three integers from large to small (introduce various methods in detail)

有趣的C语言

RIP路由信息协议-拓扑实验
![[C language] factorial implementation](/img/a1/9e603321d364c7f457116bd9e462aa.png)
[C language] factorial implementation

Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay

Introduction to STM32 lesson 1

MySQL课程1.简单命令行--简单记录 欢迎补充纠错

NB-IOT联网通信

Ogeek meetup phase I, together with cubefs, is hot

RISC-V工具链编译笔记
随机推荐
Ogeek meetup phase I, together with cubefs, is hot
求解100~200之间的素数
Detailed source code of golang bufio reader
Sort the three integers from large to small (introduce various methods in detail)
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
JVM面试题(面试必备)
Republishing and routing strategy of OSPF
HCIP oSPF综合实验
CF 1333C Eugene and an array
OSPF路由信息协议-拓扑实验
Timer interrupt experiment
SQL优化的N种方法
离开页面的提示
Golang bufio Reader 源码详解
NAT网络地址转化实验
数字集成电路:MOS管器件章(二)
【降维打击,带你深度学习CPU(上)】
HCIA (network elementary comprehensive experimental exercise)
MGRE, PPP, HDLC comprehensive experiment
[C language] factorial implementation