当前位置:网站首页>集合-Set
集合-Set
2022-06-21 20:56:00 【耳东哇】
import java.util.Iterator;
import java.util.TreeSet;
public class note144 implements Comparable<Object> {
String name;
long id;
public note144(String name, long id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int compareTo(Object o) {
//实现接口,比较此对象与指定对象的顺序
note144 upstu = (note144) o;
int result = id > upstu.id ? 1 : (id == upstu.id ? 0 : -1);
return result;
}
public static void main(String[] args) {
note144 stu1 = new note144("一一", 1011);
note144 stu2 = new note144("二娃", 1022);
note144 stu3 = new note144("三叔", 1033);
TreeSet<note144> tree = new TreeSet<>();//创建集合
tree.add(stu1);
tree.add(stu2);
tree.add(stu3);
Iterator<note144> it = tree.iterator();//Set集合中的所有对象的迭代器(获取迭代器)
while (it.hasNext()) {
note144 stu = (note144) it.next();
System.out.println(stu.getId() + ":" + stu.getName());
}
it = tree.headSet(stu2).iterator();//j截取排在stu2对象之前的对象
System.out.println("截取前面部分的集合");
while (it.hasNext()) {
note144 stu = (note144) it.next();
System.out.println(stu.getId() + stu.getName());
}
it = tree.subSet(stu2, stu3).iterator();
System.out.println("截取中间部分的集合");
while (it.hasNext()) {
note144 stu = (note144) it.next();
System.out.println(stu.getId() + stu.getName());
}
}
}
边栏推荐
- class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist
- 多进程和多线程的概念
- 实验二 栈和队列
- Matlab2020a how to export exe using app Designer
- WPF ComboBox设置选项与反显
- Resolve the invalidation of OpenCV code prompt in pycharm
- UWP Dispatcher用法
- [in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
- C# 删除正在使用的文件
- The concept of multiprocess and Multithread
猜你喜欢

LeetCode-543-二叉树的直径

电脑屏幕分辨率怎么调?电脑屏幕修改分辨率SwitchResX

Contact five heart matchmaker to take off the order

An Chaoyun was selected as one of the "top ten cloud computing solution providers" in the Asia Pacific region by cioreview in 2022

Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关
Swiftui basic learning journal (XI) SQLite data operation

Migration of stm32f407 program to stm32f429

celery+rabbit分布式任务处理

Jeu de boutons de force 4 (version MySQL)
![[deeply understand tcapulusdb technology] transaction execution of document acceptance](/img/7c/25a88f46e02cebd2e003b9590b9c13.png)
[deeply understand tcapulusdb technology] transaction execution of document acceptance
随机推荐
Matlab2020a使用App Designer如何导出exe
实验二 栈和队列
实践 DevOps 时,可能面临的六大挑战
分布式数据库使用逻辑卷管理存储之扩容
左手代码,右手开源,开源路上的一份子
WPF dependent properties
C# 删除正在使用的文件
个人的股票交易经验
Go service platform project (I) design of database tables and use of gendry Library
C# WindowFromPoint在64位程序中无效
plt label生效
js 监听和移除监听事件
Six possible challenges when practicing Devops
关于eureka启动成功但是访问404问题
FPGA之道——数字系统之间的接口电平标准
Pychart User Guide
Niuke month race - insectivores on the ring
GDB debugging practice (10) multi thread debugging
Pychart can run normally, and pyinstaller package software reports fatal error
语音断点检测(短时改进子带谱熵)