当前位置:网站首页>集合 set接口
集合 set接口
2022-08-10 05:32: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 不能添加相同元素?
set.add("lucy");//T
set.add("lucy");//F
//这里两个jack都加入的原因是它们是new出来的,本质上是两个不同的数据,只是名字恰巧都叫jack而已。
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;
//建立是一个数组table 大小为16,在索引为2的地方存放一个链表
public class HashSetStructure {
public static void main(String[] args) {
Node[] table = new Node[16];
System.out.println(table);
//在索引为2的地方创建结点john
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;
}
}结构,如果要才索引3也创建结点,步骤也一样

HashSet底层代码
第6点补充:元素个数到达8个就行,而不是超过8。如果元素个数到达8而table大小<64,会对table进行扩容。
小练习
package com.collection_.set;
//添加3个人,如果名字和年龄相同则判定为同一个人,就只添加一个。
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
和HashSet差不多就是会按照添加顺序打印。

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提供的一个构造器,可以提供一个比较器(匿名内部类)
//并指定排序规则
TreeSet treeSet1 = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
//调用String的compareto方法
return ((String)o1).compareTo((String)o2);//按照ASCII码顺序排序 ASCII码长度相等后面的就添加不进去
//return ((String)o1).length() - ((String)o2).length();//按照长度排序,就是如果长度相同(相减等于0)就不能添加进去
}
});
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);
}
}
底层代码

边栏推荐
猜你喜欢
随机推荐
Chain Reading|The latest and most complete digital collection sales calendar-08.02
pytorch框架学习(1)网络的简单构建
IDEA连接MySQL数据库并执行SQL查询操作
PyTorch 入门之旅
深度学习中数据到底要不要归一化?实测数据来说明!
redis集群模式
Four characteristics of ACID
PCL点云配准--ICP or keypoints+features
ORACLE system table space SYSTEM is full and cannot expand table space problem solving process
cesium 添加点,移动点
十年磨一剑!数字藏品行情软件,链读APP正式开放内测!
各个架构指令集对应的机型
Count down the six weapons of the domestic interface collaboration platform!
Canal reports Could not find first log file name in binary log index file
三维点云分割
共识计算和激励机制
Ten years of sharpening a sword!The digital collection market software, Link Reading APP is officially open for internal testing!
第十天作业
去中心化和p2p网络以及中心化为核心的传统通信
kaggle小白必看:小白常见的2个错误解决方案






