当前位置:网站首页>Collection set interface
Collection set interface
2022-08-10 05:51:00 【hagong9】
目录
set接口基本介绍

package com.collection_.set;
import java.util.HashSet;
public class HashSet_ {
public static void main(String[] args) {
HashSet set = new HashSet();
//执行add方法后会返回一个boolean值,添加成功为true 失败为false
// 可以通过remove指定删除哪个对象,
System.out.println(set.add("john"));//T
System.out.println(set.add("lucy"));//T
System.out.println(set.add("jack"));//T
System.out.println(set.add("Rose"));//T
System.out.println(set.add("john"));//F
System.out.println(set);
System.out.println("========移除john");
set.remove("john");
System.out.println(set);
//重置set
set = new HashSet();
System.out.println(set);//set为空
//HashSet The same element cannot be added?
set.add("lucy");//T
set.add("lucy");//F
//这里两个jackThe reason they all join is that they arenew出来的,Essentially two different pieces of data,It just happens to be the namejack而已.
set.add(new Dog("jack"));//T
set.add(new Dog("jack"));//T
set.add(new String("rose"));//T
set.add(new String("rose"));//F
System.out.println(set);
}
}
class Dog{
private String name;
public Dog(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}

数组链表模拟

例子
package com.collection_.set;
import java.util.HashSet;
//Build is an arraytable 大小为16,在索引为2where a linked list is stored
public class HashSetStructure {
public static void main(String[] args) {
Node[] table = new Node[16];
System.out.println(table);
//在索引为2where the node is createdjohn
Node john = new Node("john", null);
table[2] = john;
//创建结点jack
Node jack = new Node("jack", null);
john.next = jack;//将jack挂载到john
System.out.println(table);
//创建结点rose
Node rose = new Node("rose", null);
jack.next = rose;//将rose挂载到jack后
}
}
class Node{
Object item;
Node next;
public Node(Object item, Node next) {
this.item = item;
this.next = next;
}
}结构,Index if necessary3Nodes are also created,the same steps

HashSet底层代码
第6点补充:The number of elements arrives8个就行,而不是超过8.If the number of elements arrives8而table大小<64,会对table进行扩容.
小练习
package com.collection_.set;
//添加3个人,If the name and age are the same, it is determined to be the same person,就只添加一个.
import java.util.HashSet;
import java.util.Objects;
public class HashSetTest01 {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add(new People("张三",18));
set.add(new People("李四",28));
set.add(new People("李四",28));
set.add(new People("王五",38));
System.out.println(set);
}
}
class People{
private String name;
private int age;
@Override
public String toString() {
return "people{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public People(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
//ale+ins 重写equals和hashSet
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
People people = (People) o;
return age == people.age &&
Objects.equals(name, people.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}练习2

package com.collection_.set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
@SuppressWarnings("all")
public class HashSetTest02 {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add(new People2("张三",8000,new Mydate(2000,2,12)));
set.add(new People2("张三",9000,new Mydate(2000,2,12)));
set.add(new People2("李四",9000,new Mydate(2000,6,1)));
set.add(new People2("王五",10000,new Mydate(1998,6,29)));
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
}
}
class People2{
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
People2 people2 = (People2) o;
return Objects.equals(name, people2.name) &&
Objects.equals(brithday, people2.brithday);
}
@Override
public int hashCode() {
return Objects.hash(name, brithday);
}
private String name;
private int sal;
private Mydate brithday;
@Override
public String toString() {
return "People2{" +
"name='" + name + '\'' +
", sal=" + sal +
", brithday=" + brithday +
'}';
}
public People2(String name, int sal, Mydate brithday) {
this.name = name;
this.sal = sal;
this.brithday = brithday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public Mydate getBrithday() {
return brithday;
}
public void setBrithday(Mydate brithday) {
this.brithday = brithday;
}
}
class Mydate{
private int year;
private int month;
private int day;
@Override
public String toString() {
return "brithday="+ year + "/" +month +"/" + day;
}
public Mydate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mydate mydate = (Mydate) o;
return year == mydate.year &&
month == mydate.month &&
day == mydate.day;
}
@Override
public int hashCode() {
return Objects.hash(year, month, day);
}
}
LinkedHashSet
和HashSetIt will almost print in the order in which they were added.

treeset
package com.collection_.set;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSet_ {
public static void main(String[] args) {
TreeSet treeSet = new TreeSet();
treeSet.add("50");
treeSet.add("38");
treeSet.add("john");
treeSet.add("som");
treeSet.add("an");
treeSet.add("tom");
treeSet.add("ber");
//当使用无参构造器,创建treeset时,仍然是无序的
System.out.println(treeSet);
//使用TreeSet提供的一个构造器,A comparator can be provided(匿名内部类)
//并指定排序规则
TreeSet treeSet1 = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
//调用String的compareto方法
return ((String)o1).compareTo((String)o2);//按照ASCII码顺序排序 ASCIIAfter the code length is equal, it will not be added
//return ((String)o1).length() - ((String)o2).length();//按照长度排序,That is, if the lengths are the same(相减等于0)cannot be added
}
});
treeSet1.add("50");
treeSet1.add("38");
treeSet1.add("john");
treeSet1.add("som");
treeSet1.add("an");
treeSet1.add("tom");
treeSet1.add("ber");
System.out.println(treeSet1);
}
}
底层代码

边栏推荐
猜你喜欢

Reflection 【Notes】

树结构——二叉查找树原理与实现

String常用方法

Chain Reading Good Article: Jeff Garzik Launches Web3 Production Company

The complex "metaverse" will be interpreted for you, and the Link Reading APP will be launched soon!

并查集原理与API设计

基于 .NET Core MVC 的权限管理系统

The latest and most complete digital collection sales calendar-07.27

ORACLE system table space SYSTEM is full and cannot expand table space problem solving process

集合 set接口
随机推荐
链表API设计
String common methods
Chain Reading Recommendation: From Tiles to Generative NFTs
PCL点云配准--ICP or keypoints+features
MySql 约束
transaction, storage engine
ORACLE系统表空间SYSTEM占满无法扩充表空间问题解决过程
The latest and most complete digital collection sales calendar-07.27
2021-07-09
Database Notes Create Database, Table Backup
redis集群模式
力扣——省份数量
Collection工具类
el-cascader级联选择器的子菜单双击两次才显示被选中的内容
String常用方法
Knowledge Distillation Thesis Learning
连接 Nacos 报超时错误
第二次实验
栈和队列
[List Exercise] Traverse the collection and sort by price from low to high,