赤峰建网站的电话,今天时事新闻,电商设计美工,南宁建设银行官网招聘网站上边几篇#xff0c;我们对java的List集合进行相关介绍#xff0c;了解了关于List集合下的相关实现类的方法或者接口。
自本篇开始#xff0c;将围绕java的Set进行介绍#xff0c;也是对我java知识的巩固吧#xff0c;处理业务越多#xff0c;发现自己对基础知识的薄弱我们对java的List集合进行相关介绍了解了关于List集合下的相关实现类的方法或者接口。
自本篇开始将围绕java的Set进行介绍也是对我java知识的巩固吧处理业务越多发现自己对基础知识的薄弱于是又回头继续学习没办法学习就是这样忘记又学学了又忘听他们说把东西理解了并掺杂自己的想法就不会忘记了我也不知道是不是真的算了废话不多说了开始介绍Set集合了
Set集合 Set作为Java中的一个接口它继承自Collection接口用于存储不重复的元素 Set不保证元素的顺序且不允许包含重复元素。
Set的特点包括 不允许重复的元素 set中的元素是唯一的如果试图向Set中添加重复的元素则添加操作将被忽略。 具备无序性 Set中的元素没有固定的顺序不同的实现类可能以不同的方式存储和遍历元素。 具备高效性 Set提供了高效的元素查找和插入操作通常使用哈希表或者树等数据结构来实现。 Set定义的常用的方法例如
添加元素add(element)向Set中添加指定元素。删除元素remove(element)从Set中移除指定元素。判断是否包含元素contains(element)判断Set中是否包含指定元素。获取元素个数size()返回Set中元素的个数。清空Setclear()移除Set中的所有元素。遍历Set可以使用迭代器或增强for循环来遍历Set中的元素。
public static void main(String[] args) {SetString set new HashSet();// 添加元素set.add(apple);set.add(banana);set.add(orange);set.add(apple); // 重复元素将被忽略// 判断是否包含元素System.out.println(set.contains(apple)); // 输出trueSystem.out.println(set.contains(grape)); // 输出false// 获取元素个数System.out.println(set.size()); // 输出3// 遍历Setfor (String element : set) {System.out.println(element);}// 删除元素set.remove(banana);// 清空Setset.clear();}注意一下Set重写了equals和hashCode方法。
用途 去重当需要从一组数据中去除重复元素时可以使用Set来存储数据因为Set会自动去除重复元素。 查找由于Set内部使用哈希表或树等数据结构实现查找操作的时间复杂度为O(1)或O(log n)因此在需要快速查找元素的场景下Set是一个很好的选择。 数学集合运算Set提供了一些集合运算的方法如交集、并集、差集等可以方便地进行集合操作。
Set的优点
去重功能Set保证元素的唯一性可以自动去除重复元素简化了去重操作。高效的查找和插入操作Set的底层实现通常使用哈希表或树等数据结构使得查找和插入操作具有高效性。提供了集合运算方法Set提供了一些集合运算的方法方便进行集合操作。
Set的缺点 无序性Set中的元素没有固定的顺序无法按照特定的顺序进行访问和遍历。 不支持索引访问Set接口不提供索引访问元素的方法无法通过索引直接访问元素需要使用迭代器或其他方式进行遍历。 内存占用较大由于Set需要保证元素的唯一性可能需要额外的内存空间来存储哈希值或比较元素因此在存储大量数据时可能会占用较多的内存。
举例说明
交集Intersection获取两个Set中共同存在的元素。
SetInteger set1 new HashSet(Arrays.asList(1, 2, 3, 4, 5));
SetInteger set2 new HashSet(Arrays.asList(4, 5, 6, 7, 8));SetInteger intersection new HashSet(set1);
intersection.retainAll(set2);System.out.println(intersection); // 输出[4, 5]创建了两个Set分别包含一些整数元素。然后我们创建一个新的Set ntersection并将其初始化为set1的副本。接下来我们使用retainAll()方法将intersection与set2取交集最终得到的结果是[4, 5]即两个Set中共同存在的元素。
并集Union获取两个Set中所有的不重复元素。
SetInteger set1 new HashSet(Arrays.asList(1, 2, 3, 4, 5));
SetInteger set2 new HashSet(Arrays.asList(4, 5, 6, 7, 8));SetInteger union new HashSet(set1);
union.addAll(set2);System.out.println(union); // 输出[1, 2, 3, 4, 5, 6, 7, 8]我们创建了两个Set分别包含一些整数元素。然后我们创建一个新的Set union并将其初始化为set1的副本。接下来我们使用addAll()方法将set2中的元素添加到union中最终得到的结果是[1, 2, 3, 4, 5, 6, 7, 8]即两个Set中所有的不重复元素。
差集Difference获取在第一个Set中存在但在第二个Set中不存在的元素。
SetInteger set1 new HashSet(Arrays.asList(1, 2, 3, 4, 5));
SetInteger set2 new HashSet(Arrays.asList(4, 5, 6, 7, 8));SetInteger difference new HashSet(set1);
difference.removeAll(set2);System.out.println(difference); // 输出[1, 2, 3]我们创建了两个Set分别包含一些整数元素。然后我们创建一个新的Set difference并将其初始化为set1的副本。接下来我们使用removeAll()方法将set2中的元素从difference中移除最终得到的结果是[1, 2, 3]即在第一个Set中存在但在第二个Set中不存在的元素。 AbstractSet E
AbstractSet是一个抽象类它实现了Set接口的大部分方法提供了一些通用的实现使得编写自定义的Set实现类变得更加简单。 相关方法
add(E e)向集合中添加指定的元素。默认实现会抛出UnsupportedOperationException异常因此具体的子类需要重写该方法来实现添加元素的逻辑。addAll(Collection? extends E c)将指定集合中的所有元素添加到当前集合中。默认实现会遍历指定集合并逐个调用add()方法来添加元素。clear()从集合中移除所有的元素。默认实现会遍历集合并逐个调用remove()方法来移除元素。contains(Object o)判断集合是否包含指定的元素。默认实现会遍历集合并逐个调用equals()方法来比较元素。containsAll(Collection? c)判断集合是否包含指定集合中的所有元素。默认实现会遍历指定集合并逐个调用contains()方法来判断元素是否存在。equals(Object o)判断当前集合是否与指定对象相等。默认实现会比较集合的大小和元素是否相同。hashCode()返回当前集合的哈希码值。默认实现会遍历集合并累加每个元素的哈希码值。isEmpty()判断集合是否为空。默认实现会判断集合的大小是否为0。iterator()返回一个用于遍历集合的迭代器。默认实现会返回AbstractSet的内部类Iterator的实例。remove(Object o)从集合中移除指定的元素。默认实现会遍历集合并逐个调用equals()方法来比较元素。removeAll(Collection? c)从集合中移除指定集合中的所有元素。默认实现会遍历指定集合并逐个调用remove()方法来移除元素。retainAll(Collection? c)仅保留集合中与指定集合中相同的元素移除其他元素。默认实现会遍历集合并逐个调用contains()方法来判断元素是否存在。size()返回集合中的元素个数。默认实现会遍历集合并计数。toArray()将集合转换为数组。默认实现会创建一个新的数组并将集合中的元素复制到数组中。案例
AbstractSet在Java中的作用是为自定义的Set实现类提供了一些通用的方法实现简化了编写Set实现类的过程。通过继承AbstractSet我们只需要关注实现自定义的Set特有的方法而无需重复编写已经在AbstractSet中实现的方法。
public class MySetE extends AbstractSetE {private Object[] elements;private int size;public MySet() {elements new Object[10];size 0;}Overridepublic boolean add(E e) {if (contains(e)) {return false;}if (size elements.length) {// 扩容数组Object[] newElements new Object[elements.length * 2];System.arraycopy(elements, 0, newElements, 0, size);elements newElements;}elements[size] e;return true;}Overridepublic IteratorE iterator() {return new MySetIterator();}Overridepublic int size() {return size;}private class MySetIterator implements IteratorE {private int cursor;Overridepublic boolean hasNext() {return cursor size;}Overridepublic E next() {if (!hasNext()) {throw new NoSuchElementException();}return (E) elements[cursor];}}
}我们创建了一个名为MySet的自定义Set实现类它继承自AbstractSet。我们只需要实现add()、iterator()和size()这三个抽象方法而其他方法如contains()、remove()等已经在AbstractSet中实现了。
在MySet中我们使用一个数组elements来存储集合中的元素size表示集合的大小。在add()方法中我们首先判断元素是否已经存在于集合中如果存在则返回false否则将元素添加到数组中。如果数组已满则扩容数组。在iterator()方法中我们返回一个内部类MySetIterator的实例用于遍历集合中的元素。在size()方法中我们直接返回集合的大小。 文章转载自: http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.zwckz.cn.gov.cn.zwckz.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.mfbzr.cn.gov.cn.mfbzr.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.rzjfn.cn.gov.cn.rzjfn.cn http://www.morning.ldspj.cn.gov.cn.ldspj.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.clzly.cn.gov.cn.clzly.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.pwdrc.cn.gov.cn.pwdrc.cn http://www.morning.grnhb.cn.gov.cn.grnhb.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.hbkkc.cn.gov.cn.hbkkc.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.xmhpq.cn.gov.cn.xmhpq.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.gqtzb.cn.gov.cn.gqtzb.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.qwgct.cn.gov.cn.qwgct.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.nxzsd.cn.gov.cn.nxzsd.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.c7624.cn.gov.cn.c7624.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.jgzmr.cn.gov.cn.jgzmr.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.fygbq.cn.gov.cn.fygbq.cn http://www.morning.brqjs.cn.gov.cn.brqjs.cn http://www.morning.alwpc.cn.gov.cn.alwpc.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn