Set的子类接口特有方法和使用方法

本文深入探讨了Java集合框架中的Set接口及其实现类,包括HashSet、LinkedHashSet和TreeSet的特点与使用场景。阐述了如何利用equals方法和hashCode方法确保元素唯一性,并提供了自定义对象存储于ArrayList和HashSet集合的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Set接口中的方法和Collection中方法一致的。Set接口取出方式只有一种,迭代器

  |--HashSet底层数据结构是哈希表,线程是不同步的无序,高效;

      HashSet集合保证元素唯一性:通过元素的hashCode方法,和equals方法完成的。

      当元素的hashCode值相同时,才继续判断元素的equals是否为true。

      如果为true,那么视为相同元素,不存。如果为false,那么存储。

      如果hashCode值不同,那么不判断equals,从而提高对象比较的速度。

      |--LinkedHashSet有序,hashset的子类。

  |--TreeSet对Set集合中的元素的进行指定顺序的排序。不同步。TreeSet底层的数据结构就是二叉树。

对于ArrayList集合,判断元素是否存在,或者删元素底层依据都是equals方法。

例如:

将自定义对象作为元素存到ArrayList集合中,并去除重复元素。

比如:存人对象。同姓名同年龄,视为同一个人。为重复元素。


思路:
1,对人描述,将数据封装进人对象。
2,定义容器,将人存入。
3,取出。



List集合判断元素是否相同,依据是元素的equals方法。
class PersonALDemo
{
	private String name;
	private int age;
	PersonALDemo(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	public String getName()
	{
		return name;

	}
	public int getAge()
	{
		return age;
	}
	public boolean equals(Object obj)
	{
		if(!(obj instanceof PersonALDemo))
			return false;
		PersonALDemo p = (PersonALDemo)obj;
		
		return this.name.equals(p.name)&&this.age==p.age;
	}
}
public static ArrayList singleElement(ArrayList al)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//定义一个临时容器。
<span style="white-space:pre">		</span>ArrayList newAl = new ArrayList();


<span style="white-space:pre">		</span>Iterator it = al.iterator();


<span style="white-space:pre">		</span>while(it.hasNext())
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>Object obj = it.next();


<span style="white-space:pre">			</span>if(!newAl.contains(obj))
<span style="white-space:pre">				</span>newAl.add(obj);


<span style="white-space:pre">		</span>}


<span style="white-space:pre">		</span>return newAl;
<span style="white-space:pre">	</span>}
//迭代器
Iterator it = al.iterator();




<span style="white-space:pre">		</span>while(it.hasNext())
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>Person p = (Person)it.next();
<span style="white-space:pre">			</span>sop(p.getName()+"::"+p.getAge());
<span style="white-space:pre">		</span>}


对于HashSet集合,判断元素是否存在,或者删除元素,底层依据的是hashCode方法和equals方法

例如:

往hashSet集合中存入自定对象
姓名和年龄相同为同一个人,重复元素。

class PersonHSDemo
{
	private String name;
	private int age;
	PersonHSDemo(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
	public int hashCode()
	{
		System.out.println(this.name+"..hashCode");
		return name.hashCode()+age*34; //保证哈希值唯一性就age全部乘以30以后的数字
	}
	public boolean equlas(Object obj)
	{
		if(!(obj instanceof PersonHSDemo))
			return false;
		
		PersonHSDemo p = (PersonHSDemo)obj;
		System.out.println(this.name+" equals "+p.name);
		return this.name.equals(p.name)&&this.age==p.age;
		
	}
}


//迭代器
Iterator it = hs.iterator();

		while(it.hasNext())
		{
			Person p = (Person)it.next();
			sop(p.getName()+"::"+p.getAge());
		}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值