当前位置:网站首页>set集合中的唯一性和排序问题
set集合中的唯一性和排序问题
2022-07-20 17:46:00 【王小小鸭】
1.去除重复值
在List集合内去除重复数字值,要求尽量简单(可借助其他集合!!)
package com.B.Container_13.Set.MAp.Practice;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
//在List集合内去除重复数字值,要求尽量简单(可借助其他集合!!)
public class A5 {
public static void main(String[] args) {
// 创建list集合对象
ArrayList<Integer> list = new ArrayList<Integer>();
// 添加元素
list.add(12);
list.add(15);
list.add(24);
list.add(12);
list.add(24);
// 创建set集合
HashSet<Integer> set = new HashSet<Integer>();
// 创建一个新的list集合
List newList = new ArrayList();
//将list集合的元素添加到set集合,达到去除重复数字值的目的
for (Integer s :list) {
if(set.add(s)){
newList.add(s);
}
}
System.out.println( "去重后的集合: " + newList);
}
}
输出:
去重后的集合: [12, 15, 24]
进程已结束,退出代码 0
2.去除重复值
现有若干图书信息(包含名称title、作者author、定价price)需要存储到set集合中,保证集合中无重复元素,并遍历查看。可以认为所有信息都相同的图书为重复数据。
package com.B.Container_13.Set.MAp.Practice;
//图书信息类(包含名称title、作者author、定价price)
public class A7_books {
private String title;
private String author;
private double price;
public A7_books() {
}
public A7_books(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// 重写方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
A7_books a7_01 = (A7_books) o;
if (Double.compare(a7_01.price, price) != 0) return false;
if (title != null ? !title.equals(a7_01.title) : a7_01.title != null) return false;
return author != null ? author.equals(a7_01.author) : a7_01.author == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = title != null ? title.hashCode() : 0;
result = 31 * result + (author != null ? author.hashCode() : 0);
temp = Double.doubleToLongBits(price);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
测试类:
package com.B.Container_13.Set.MAp.Practice;
import java.util.HashSet;
//现有若干图书信息(包含名称title、作者author、定价price)
// 需要存储到set集合中,保证集合中无重复元素,并遍历查看。可以认为所有信息都相同的图书为重复数据。
public class A7 {
public static void main(String[] args) {
//创建对象
HashSet<A7_books> hs = new HashSet<A7_books>();
//添加元素
A7_books book1 = new A7_books("西游记","吴承恩",69.9);
A7_books book2 = new A7_books("三国演义","罗贯中",59.9);
A7_books book3 = new A7_books("西游记","吴承恩",69.9);
A7_books book4 = new A7_books("红楼梦","曹雪芹",58.9);
A7_books book5 = new A7_books("水浒传","施耐庵",46.9);
hs.add(book1);
hs.add(book2);
hs.add(book3);
hs.add(book4);
hs.add(book5);
//输出
for (A7_books h : hs) {
System.out.println("《"+h.getTitle()+"》作者是"+h.getAuthor()+",价格为"+h.getPrice()+"元");
}
Tom
}
}
输出结果:
《水浒传》作者是施耐庵,价格为46.9元
《红楼梦》作者是曹雪芹,价格为58.9元
《西游记》作者是吴承恩,价格为69.9元
《三国演义》作者是罗贯中,价格为59.9元
进程已结束,退出代码 0
3.排序(有主次之分)
在某次考试中,学生的成绩信息如下(公有属性): 姓名(String) 年龄(int) 成绩(int): Tom 20 90; Jerry 22 95; John 20 100; Lily 22 100 ;Lucy 22 90; Kevin 22 90 请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序,成绩和年龄都一样,则按照姓名的字典顺序排序。
出现的问题:
输出结果成绩没有降序排列
出现原因:
排序逻辑有误
改进措施:
交换s1和s2位置
完整代码:
Comparable接口排序:
package com.B.Container_13.Set.MAp.Practice;
//Comparable接口排序
//学生类
//在某次考试中,学生的成绩信息如下(公有属性): 姓名(String) 年龄(int) 成绩(int)
//Tom 20 90; Jerry 22 95; John 20 100; Lily 22 100 ;Lucy 22 90; Kevin 22 90
public class A8_Student01 implements Comparable<A8_Student01> {
private String name;
private int age;
private int score;
public A8_Student01() {
}
public A8_Student01(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
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;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public int compareTo(A8_Student01 s) {
// 成绩做降序排序,从大到小
int num = s.score - this.score;
//年龄相同时,按照年龄从小到大排序
int num1 = num == 0 ? this.age - s.age : num;
// 成绩和年龄都一样,则按照姓名的字典顺序排序。
int num2 = num1 == 0 ? this.name.compareTo(s.name) : num1;
return num2;
}
}
测试类:
package com.B.Container_13.Set.MAp.Practice;
//Comparable接口排序
//请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序,
//如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序,
// 成绩和年龄都一样,则按照姓名的字典顺序排序。
import java.util.TreeSet;
public class A8_01 {
public static void main(String[] args) {
//创建集合对象
TreeSet<A8_Student01> stu = new TreeSet<A8_Student01>();
//创建学生对象
A8_Student01 s1 = new A8_Student01("Tom", 20,90);
A8_Student01 s2 = new A8_Student01("Jerry", 22,95);
A8_Student01 s3 = new A8_Student01("John", 20,100);
A8_Student01 s4 = new A8_Student01("Lily", 22,100);
A8_Student01 s5 = new A8_Student01("Lucy", 22,90);
A8_Student01 s6 = new A8_Student01("Kevin", 22,90);
//把学生添加到集合
stu.add(s1);
stu.add(s2);
stu.add(s3);
stu.add(s4);
stu.add(s5);
stu.add(s6);
//遍历集合
for (A8_Student01 s : stu) {
System.out.println(s.getName() +","+s.getAge()+","+s.getScore());
}
}
}
运行结果:
John,20,100
Lily,22,100
Jerry,22,95
Tom,20,90
Kevin,22,90
Lucy,22,90
进程已结束,退出代码 0
Comparator接口排序:
package com.B.Container_13.Set.MAp.Practice;
Comparator接口排序
//在某次考试中,学生的成绩信息如下(公有属性): 姓名(String) 年龄(int) 成绩(int):
public class A8_Student02 {
String name;
int age;
int score;
public A8_Student02() {
}
public A8_Student02(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
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;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "A8_Student02{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
测试类:
package com.B.Container_13.Set.MAp.Practice;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
//Comparator接口排序
public class A8_02 {
public static void main(String[] args) {
//创建一个TreeSet集合对象
Set<A8_Student02> treeSet =new TreeSet<A8_Student02>(new Comparator<A8_Student02>() {
@Override
public int compare(A8_Student02 s1, A8_Student02 s2) {
// 成绩做降序排序,从大到小
int num = s2.getScore() - s1.getScore();
//年龄相同时,按照年龄从小到大排序
int num1 = num == 0 ? s1.getAge() - s2.getAge() : num;
// 成绩和年龄都一样,则按照姓名的字典顺序排序。
int num2 = num1 == 0 ? s1.getName().compareTo(s2.getName()) : num1;
return num2;
}
});
//创建学生对象
treeSet.add(new A8_Student02("Tom", 20,90));
treeSet.add(new A8_Student02("Jerry", 22,95));
treeSet.add(new A8_Student02("John", 20,100));
treeSet.add(new A8_Student02("Lily", 22,100));
treeSet.add(new A8_Student02("Lucy", 22,90));
treeSet.add(new A8_Student02("Kevin", 22,90));
//遍历数组
for (A8_Student02 s02 : treeSet) {
System.out.println(s02);
}
}
}
结果:
A8_Student02{name='John', age=20, score=100}
A8_Student02{name='Lily', age=22, score=100}
A8_Student02{name='Jerry', age=22, score=95}
A8_Student02{name='Tom', age=20, score=90}
A8_Student02{name='Kevin', age=22, score=90}
A8_Student02{name='Lucy', age=22, score=90}
进程已结束,退出代码 0
边栏推荐
- Sqlserver BCP参数解释和字符格式选择和故障处理小结
- 浅拷贝 深拷贝
- MySQL - UPDATE statement execution process
- Maixll-Dock 条形码识别
- My creation anniversary (July 18, 2021 - July 18, 2022)
- 看了这篇我也懂了this
- Systematic thinking and practice of data management of meituan accommodation business
- 通过专线/VPN网关/智能接入网关的自建数据库传输之间的差异是什么?
- 斥资2900万美元!以色列成立量子计算研发中心
- 选择云企业网CEN接入自建数据库,需要怎么选择和链接?
猜你喜欢
【C语言】文件操作
Regular expressions match all Chinese characters or characters with double quotation marks
MySQL—update语句执行流程
The 22 pictures show you in-depth analysis of prefix, infix, suffix expressions and expression evaluation
This should be done in the face of medical disputes
Maixll dock barcode recognition
@Configuration and @bean
IM即时通讯开发千万级并发长连接网关技术
项目经理如何有效地进行项目工作量估算?
Winform UI界面设计例程——获取电脑SN号
随机推荐
Docker installs myql5.7 and mysql8.0
What is public IP self built database?
Huawei summer internship general software development experience
MaxCompute实例相关操作
关于数据库分库分表的一切都在这里了
查询mysql的最大连接数和当前连接数
docker安装myql5.7和mysql8.0
Market Research and investment forecast report of China's tungsten powder industry (2022 Edition)
Exercice leetcode - Échange de doigts 66. Construire un tableau de produits
2022清华暑校笔记之L2_2 CNN和RNN基础介绍
How does AI predict the COVID-19? A summary of Georgia Institute of technology's latest "data centric epidemic prediction"
选择DTS数据库类型时需要注意什么?
Openvino安装时的一些记录
Go mod créer un projet
visual studio输入!没有提示
线程池学习
从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」
一代「博雅」大师离世!缅怀复旦大学原校长、中国科学院院士杨福家教授
什么是无公网IP: Port的数据库?
综述 | 实例分割研究