三大集合---Set集合 超详细易懂,加案例分析

目录

一.Set集合的特点

无序 没有下标 因此没有修改

不可添加重复项

二.Set集合的遍历方式

 foreach

 iterator

 三.set集合去重

底层就是调的实体类重写的hashCode方法和equals方法

四.排序

4.1自然排序

4.2比较器排序

注意:当比较器接口与自然排序接口同时使用,比较器接口的优先级高于自然排序接口


一.Set集合的特点

集合都具备的特点就是自带有增删改查,但是set集合没有修改,因为其是无序的!

其特点为:

  • 无序 没有下标 因此没有修改

  • 不可添加重复项

 

二.Set集合的遍历方式

        因为其没有下标所有就没有fori的遍历方式

        遍历方式

  •  foreach

  •  iterator

 三.set集合去重

底层就是调的实体类重写的hashCode方法和equals方法

首先是字符串形式

 对象形式需要调HashCode和equals方法将其重写

class Yinzi{
	private int id;
	private String name;
	public Yinzi() {
		// TODO Auto-generated constructor stub
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Yinzi(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Yinzi [id=" + id + ", name=" + name + "]";
	}
	@Override
	public int hashCode() {
		System.out.println("调用了hashCode方法");
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		System.out.println("equals方法被调用了");
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Yinzi other = (Yinzi) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	

 

 

注意:先调用hashCode方法进行对比,如果最后返回的结果相同,则再调用equals方法

四.排序

  •         自然排序
  •         比较器排序

4.1自然排序

        实体类实现Comparable 重写compareTo方法

class YZ implements Comparable<YZ>{
	private int id;
	private String name;
	private int age;
	private int money;
	
	public YZ() {
		// TODO Auto-generated constructor stub
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	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 getMoney() {
		return money;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public YZ(int id, String name, int age, int money) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.money = money;
	}

	@Override
	public String toString() {
		return "YZ [id=" + id + ", name=" + name + ", age=" + age + ", money=" + money + "]";
	}

	@Override
	public int hashCode() {
		System.out.println("hashCode被调用了");
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + id;
		result = prime * result + money;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		System.out.println("equals被调用了");
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		YZ other = (YZ) obj;
		if (age != other.age)
			return false;
		if (id != other.id)
			return false;
		if (money != other.money)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	

	@Override
	public int compareTo(YZ o) {
		//根据money降序排序
		//return  o.money-this.money ;
		//根据money排序且再按照年龄排序
		
		return   o.money-this.money ;
		
	}
	

 

4.2比较器排序

        TreeSet构造器使用实现java.util.Comparator的匿名内部类

	//比较器排序
		TreeSet ts2=new TreeSet<>(new Comparator<YZ>() {
			//比较
//			@Override
//			public int compare(YZ o1, YZ o2) {
//				return o2.getId()-o1.getId();
//			}
			//双重比较
			@Override
			public int compare(YZ o1, YZ o2) {
				//判断金钱
				int num=o2.getMoney()-o1.getMoney();
				if(num==0) {
					//再还要根据年龄排序
					return o1.getAge()-o2.getAge();
				}
				return num;
			}
			
		});
		//添加
		for (Object object : set) {
			ts2.add(object);
		}
		//打印
		for (Object object : ts2) {
			System.out.println("后"+object);
		}
	}
	
	

注意:当比较器接口与自然排序接口同时使用,比较器接口的优先级高于自然排序接口

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值