13.4 Set 接 口(血干JAVA系列)

13.4.1 Set接口的定义

在这里插入图片描述

13.4.2 Set接口的常用子类

在这里插入图片描述

1.散列的存放:HashSet

HashSet是Set接口的一个子类,主要的特点是:里面不能存放重复元素,而且是采用散列的存储方式,所以没有顺序。

package jiaqi;

import java.util.HashSet;
import java.util.Set;

public class demo465_2 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Set<String> s = new HashSet<String>();
		s.add("A");
		s.add("B");
		s.add("C");
		s.add("C");
		s.add("C");//不可重复
		s.add("D");
		System.out.println(s);
	}
}

2.有序的存放:TreeSet

在这里插入图片描述

package jiaqi;

import java.util.TreeSet;
import java.util.Set;

public class demo465_2 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Set<String> s = new TreeSet<String>();
		s.add("B");
		s.add("A");
		s.add("C");
		s.add("C");
		s.add("C");//不可重复
		s.add("D");
		System.out.println(s);
	}
}

3.关于TreeSet的排序说明

【例13.13】自定义类排序

package jiaqi;
import java.util.*;

class P
{
	private String name;
	private int age;
	public P(String name,int age) 
	{
		this.age=age;
		this.name=name;
	}
	
	public String toString()
	{
		return this.name+"的年龄是"+this.age+"岁";
	}
	
}
public class demo465_2 {

	public static void main(String[] args) {
		
		Set<P> allSet = new TreeSet<P>();
		allSet.add(new P("张三",30));
		allSet.add(new P("李四",31));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));
		allSet.add(new P("赵六",33));
		allSet.add(new P("孙七",33));
		System.out.println(allSet);
	}
}

在这里插入图片描述

【例13.14】指定排序规则

package jiaqi;
import java.util.*;

class P implements Comparable<P>
{
	private String name;
	private int age;
	public P(String name,int age) 
	{
		this.age=age;
		this.name=name;
	}
	
	public int compareTo(P per) {
		if(this.age<per.age)
		{
			return -1;
		}
		else if(this.age>per.age)
		{
			return 1;
		}
		else 
		{
			return 0;
		}
	}
	public String toString()
	{
		return this.name+"的年龄是"+this.age+"岁";
	}
	
}
public class demo465_2 {

	public static void main(String[] args) {
		
		Set<P> allSet = new TreeSet<P>();
		allSet.add(new P("张三",30));
		allSet.add(new P("李四",31));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));
		allSet.add(new P("赵六",33));
		allSet.add(new P("孙七",33));
		System.out.println(allSet);
	}
}

在这里插入图片描述

【例13.15】修改Person的比较器

this.name.compareTo(per.name);//是返回int的,字典序
package jiaqi;
import java.util.*;

class P implements Comparable<P>
{
	private String name;
	private int age;
	public P(String name,int age) 
	{
		this.age=age;
		this.name=name;
	}
	
	public int compareTo(P per) {
		if(this.age<per.age)
		{
			return -1;
		}
		else if(this.age>per.age)
		{
			return 1;
		}
		else 
		{
			return this.name.compareTo(per.name);
		}
	}
	public String toString()
	{
		return this.name+"的年龄是"+this.age+"岁";
	}
	
}
public class demo465_2 {

	public static void main(String[] args) {
		
		Set<P> allSet = new TreeSet<P>();
		allSet.add(new P("张三",30));
		allSet.add(new P("李四",31));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));
		allSet.add(new P("赵六",33));
		allSet.add(new P("孙七",33));
		System.out.println(allSet);
	}
}

在这里插入图片描述

4.关于重复元素的说明

【例13.16】加入重复对象
package jiaqi;
import java.util.*;

class P implements Comparable<P>
{
	private String name;
	private int age;
	public P(String name,int age) 
	{
		this.age=age;
		this.name=name;
	}
	
	
	public String toString()
	{
		return this.name+"的年龄是"+this.age+"岁";
	}
	
}
public class demo465_2 {

	public static void main(String[] args) {
		
		Set<P> allSet = new HashSet<P>();
		allSet.add(new P("张三",30));
		allSet.add(new P("李四",31));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));
		allSet.add(new P("赵六",33));
		allSet.add(new P("孙七",33));
		System.out.println(allSet);
	}
}

在这里插入图片描述

【例13.17】去掉重复元素
package jiaqi;
import java.util.*;

class P implements Comparable<P>
{
	private String name;
	private int age;
	public P(String name,int age) 
	{
		this.age=age;
		this.name=name;
	}
	
	public boolean equals(Object obj) 
	{
		if(this==obj)return true;//地址相同
		if(!(obj instanceof P)) return false;
		P p = (P)obj;
		if(p.name.equals(this.name)&&p.age==this.age)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	public int hashCode() {
		// TODO 自动生成的方法存根
		return this.name.hashCode()*this.age;
	}
	public String toString()
	{
		return this.name+"的年龄是"+this.age+"岁";
	}
	
}
public class demo465_2 {

	public static void main(String[] args) {
		
		Set<P> allSet = new HashSet<P>();
		allSet.add(new P("张三",30));
		allSet.add(new P("李四",31));
		allSet.add(new P("王五",32));
		allSet.add(new P("王五",32));//不重复
		allSet.add(new P("王五",32));
		allSet.add(new P("赵六",33));
		allSet.add(new P("孙七",33));
		System.out.println(allSet);
	}
}

++++++++++总结Tree,Hash注意项+++++++++

TreeSet:排序问题
1.compareTo(P per),
2.compareTo,equals,hashcode
Hash:重复问题
1.equals,hashcode

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿斯卡码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值