2020-11-20 java---------------Set,hashset,treeset

2024-04-27 18:48

本文主要是介绍2020-11-20 java---------------Set,hashset,treeset,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Set,hashset,treeset

set

package cn.itcast_01;
/*
collection:
list有序是指存储顺序和取出顺序一致,可重复
set无序是指存储顺序和取出顺序不一致,唯一hashset不保证set迭代顺序,不能保证该顺序恒久不变linkedhashset底层是hash表和链表(存储和取出顺序一致)*/import java.util.HashSet;
import java.util.Set;public class SetDemo {public static void main(String[] args) {Set<String> set=new HashSet<String>();set.add("hello");set.add("java");set.add("ee");set.add("java");set.add("ee");for(String i :set){System.out.println(i);}}
}
/*
元素唯一且无序
ee
java
hello*/

hashset

的唯一性是通过hashcode和equals实现的,其实是哈希表结构,元素hash值相同并且元素值相同不会加入。
注意string类重写了hashcode和equals方法所以可以比较出相同,如果不重写一般不相同。如下

package cn.itcast_01;import java.util.HashSet;
import java.util.Set;public class SetDemo {public static void main(String[] args) {Set<Student> set=new HashSet<Student>();Student s1=new Student("小红",18);Student s2=new Student("小黄",18);Student s3=new Student("小红",20);Student s4=new Student("小黑",18);Student s5=new Student("小红",18);set.add(s1);set.add(s2);set.add(s3);set.add(s4);set.add(s5);for( Student i :set){System.out.println(i.toString());}}
}
/*
元素重复
Student{name='小红', age=20}
Student{name='小红', age=18}
Student{name='小黄', age=18}
Student{name='小红', age=18}
Student{name='小黑', age=18}*/
 @Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age &&Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}

Student{name=‘小黄’, age=18}
Student{name=‘小红’, age=18}
Student{name=‘小红’, age=20}
Student{name=‘小黑’, age=18}
没有重复了

TreeSet

能够按照某种顺序给元素排序(选哪个取决于用哪个构造方法)
A:自然排序 -------实现compareable接口重写compareto方法
B:比较器排序 (常见)

无参构造默认自然排序

自然排序 -------实现compareable接口重写compareto方法

package cn.itcast_01;import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;public class SetDemo {public static void main(String[] args) {TreeSet<Integer> ts=new TreeSet<>();  //无参构造默认自然排序ts.add(3);ts.add(1);ts.add(23);ts.add(15);ts.add(15);for( Integer i :ts){System.out.println(i);}}
}
/*
1
3
15
23*/
public class SetDemo {public static void main(String[] args) {TreeSet<Student> set=new TreeSet<Student>();Student s1=new Student("小红",18);Student s2=new Student("小黄",18);Student s3=new Student("小红",20);Student s4=new Student("小黑",18);Student s5=new Student("小红",18);set.add(s1);set.add(s2);set.add(s3);set.add(s4);set.add(s5);for( Student i :set){System.out.println(i.toString());}}
}
/*
报错:lang.ClassCastException: cn.itcast_01.Student cannot be cast to java.lang.Comparable
类要实现自然排序就必须实现自然排序接口*/

类要实现自然排序就必须实现自然排序接口重写的compareto方法要自己写

  public int compareTo(Student o) {//    return 0;  因为底层是红黑树,比根节点小往左子树大往右子树,所以比较得0认为相同大小不会插树 只存进去一个根节点 Student{name='小红', age=18}//    return 1;  同理,怎么进怎么出// return -1; 按输入顺序倒着输出//实际应该按照排序规则返回/*    int num=this.age-o.age;return num;结果:年龄相同名字相同的也无法存进来Student{name='小红', age=18}Student{name='小红', age=20}*/int num1=this.age-o.age;int num=num1==0?this.name.compareTo(o.name):num1;  //字符串自带comparetoreturn num;}

按照名字比较只需改写类的compareto

 @Overridepublic int compareTo(Student o) {int num1=this.name.length()-o.name.length();int num=num1==0?this.name.compareTo(o.name) :num1;return num;}
 public static void main(String[] args) {TreeSet<Student> set=new TreeSet<Student>();Student s1=new Student("小红",18);Student s2=new Student("小黄黄黄黄黄",18);Student s3=new Student("小红红",20);Student s4=new Student("小黑黑黑黑",18);Student s5=new Student("小红",19);set.add(s1);set.add(s2);set.add(s3);set.add(s4);set.add(s5);for( Student i :set){System.out.println(i.toString());}
/*
Student{name='小红', age=18}
Student{name='小红红', age=20}
Student{name='小黑黑黑黑', age=18}
Student{name='小黄黄黄黄黄', age=18}
实现从高到底排序只需要交换是this o
*/

但是这样名字一样并且长度一样的人年龄不同也不一定是一个人,比如19岁的小红没加进去

@Overridepublic int compareTo(Student o) {int num1=this.name.length()-o.name.length();int num2=(num1==0?this.name.compareTo(o.name) :num1);int num3=(num2==0?this.age-o.age:num2);return num3;}
/*
Student{name='小红', age=18}
Student{name='小红', age=19}
Student{name='小红红', age=20}
Student{name='小黑黑黑黑', age=18}
Student{name='小黄黄黄黄黄', age=18}
*/

比较器排序

public class MyComparator implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {int num1=o1.getName().length()-o2.getName().length();int num2=(num1==0?o1.getName().compareTo(o2.getName()) :num1);int num3=(num2==0?o1.getAge()-o2.getAge():num2);return num3;}
}
public static void main(String[] args) {
//        TreeSet<Student> set=new TreeSet<Student>();TreeSet<Student> set=new TreeSet<Student>(new MyComparator());  //接口类型的参数传一个实现该接口的类的实例Student s1=new Student("小红",18);Student s2=new Student("小黄黄黄黄黄",18);Student s3=new Student("小红红",20);Student s4=new Student("小黑黑黑黑",18);Student s5=new Student("小红",19);

实现从高到底排序只需要交换是s1 s2
只用一次就造个类很浪费,匿名内部类正好解决

 public static void main(String[] args) {
//        TreeSet<Student> set=new TreeSet<Student>();TreeSet<Student> set=new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {int num1=o1.getName().length()-o2.getName().length();int num2=(num1==0?o1.getName().compareTo(o2.getName()) :num1);int num3=(num2==0?o1.getAge()-o2.getAge():num2);return num3;}});Student s1=new Student("小红",18);Student s2=new Student("小黄黄黄黄黄",18);Student s3=new Student("小红红",20);Student s4=new Student("小黑黑黑黑",18);Student s5=new Student("小红",19);

匿名内部类格式
new类名或接口名,大括号里面重写方法(此例子中接口是泛型)

一定要注意,给了排序标准后还要考虑潜在的
比如按总分排序,那总分一样的并不一定是一个人,还要看语数英等
Integer.parseint()可以把string类型转换成int类型

这篇关于2020-11-20 java---------------Set,hashset,treeset的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/941256

相关文章

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过