JAVA语言中的类集之set
小标 2018-09-11 来源 : 阅读 1374 评论 0

摘要:本文主要向大家介绍了JAVA语言中的类集之set,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言中的类集之set,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

Set可以理解为集合,它与List和Queue最大的不同点在于,它里面存放的元素不能有重复。它有两个常用的实现类:HashSet和TreeSet。
集合主要用来存放数据,因此只提供了添加和删除数据的方法:add(obj),remove(Obj),这里的obj表示集合中的元素。如果添加或者删除成功时返回true否则返回false。因为集合中不能有重复的元素,所以有必要在添加元素时判断是否添加成功。添加进去的元素存放在哪个位置由集合来确定,因此不能确定被添加元素的具体位置。鉴于这个原因set不支持像链表一样按照索引添加/删除元素。
另外,clear()方法用来清空集合中所有的元素,清空后会变成空集合。与List和Queue接口相比,Set接口提供的方法不多,不过我们还是使用文字结合代码的方式来演示如何使用它们。下面是具体的代码,请参考:
import java.util.Arrays;import java.util.HashSet;import java.util.Set; public class setEx { public static void main(String args[]){  // init set  Set set = new HashSet<>();  for (int i = 0; i < 10; ++i) {// set.add(new Integer(i+1));set.add((i + 1));  }   // show size of set ,and content of set  System.out.println("size of set: " + set.size());  for (Integer i : set)System.out.print(i + " ");   System.out.println("\nset: " + set);   // delete the content of set,this is based on content of set  if(set.remove(9)) {System.out.println("after removing the content 9. set: " + set);  } else {System.out.println("removing the content 9 failed.");  }   // delete the same content of set,it is failed to remove  if(set.remove(9)) {System.out.println("after removing the content 9. set: " + set);  } else {System.out.println("removing the content 9 failed.");  }   // add the same content of set,it is failed to add   if(set.add(9)) {System.out.println("after adding the content 9. set: " + set);  } else {System.out.println("adding the content 9 failed.");  }   // add the content of set,this is based on content of set  if(set.add(9)) {System.out.println("after adding the content 9. set: " + set);  } else {System.out.println("adding the content 9 failed.");  }   // change set to array  Integer[] array = set.toArray(new Integer[] {});  System.out.println("change set to array: " + Arrays.toString(array));   // add the content of set,this is based on content of set  set.clear();  System.out.println("after clearing the set: " + set); }}
下面是程序的运行结果,请参考:
size of set: 101 2 3 4 5 6 7 8 9 10set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]after removing the content 9. set: [1, 2, 3, 4, 5, 6, 7, 8, 10]removing the content 9 failed.after adding the content 9. set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]adding the content 9 failed.change set to array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]after clearing the set: []
各位看官,关于Java中类集之Set的例子咱们就介绍到这里    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved