java集合类(四)About Set
接上篇:java集合类(三)About Iterator & Vector(Stack)
之前,在比较java常见集合类的时候,就了解到一点有关Set的特性、实现类及其要求等,读者可以去温习下“java集合类(一)”。Set与Collection接口完全一样('cause 它继承了Collection接口---接口间继承,可以此拓展接口功能,另外,java只能通过接口实现多继承,而抽象类则不行),只是与Collection的行为不一样(多态性表现)。Set保存的是不重复元素,但不保证元素次序,它常被用来测试归属性,即通过对象的值,判断元素是否在某个Set集合内,通常使用HashSet实现,因为HashSet专门对快速查找进行了优化(hashCode()实现)。另外2种常见的Set,TreeSet则实现Comparable接口,以一定的方式对元素进行排序,并用红黑树存储这些元素;LinkedHashSet也使用了hashCode(),但它使用链表来维护元素顺序---即元素的插入顺序。这三者的比较可见下文!下面介绍Set的使用:
- Talk about “Set”:
- All Superinterfaces:Collection<E>, Iterable<E>
- All Known Subinterfaces:NavigableSet<E>, SortedSet<E>
- All Known Implementing Classes:AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet
- 1.Set 的常规使用:
//demo from <Thinking in java>
import java.util.*; public void setdemo(){
Random r = new Random(30);
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i<10000; i++)
set.add(r.nextInt(20));
System.out.println(set);
}
/*output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18]
*/
上面实例只对add()进行,因为Set跟之前的Collection差不多,其他方法便不再一一演示。在上面代码中使用了Random进行对Set元素的添加,这里就顺便用Set实现“在特定范围内,确保每次产生不同的随机数”
//Random 5 different nums between 10~100 every time import java.util.*; public void randomdemo(){
HashSet<Integer> hs = new HashSet<Integer>();
while (true) {
int a = (int)(Math.random() * 100);
if(a >= 10 && a <= 100) {
hs.add(a);
}
if (hs.size() == 5) {
break;
}
}
System.out.println(hs);
}
//output:[23, 97, 82, 41, 30]
- 关于使用Set测试元素的归属性,主要采用的是contains()及其相关方法,用法跟其他前面提及的java集合类差不多,这里也不作实例演示。
- 三种常用Set实现类的比较:
1)HashSet: 为快速查找而设计的set,存入HashSet的元素必须定义hashCode(),编程中一般使用这种(对元素顺序无要求时)
2)TreeSet: 保持次序的Set,底层结构为红黑树,使用它可以从Set中提取有序序列,存入其中的元素必须实现Comparable接口
3)LinkedHashSet:具有HashSet的查询速度,使用链表维护元素的顺序(即插入顺序),元素必须定义hashCode()
注:关于hashCode(),以后有时间再跟读者详细讨论,现在只需知道:在这里,它是为快速查找而必须实现的方法!
2.About SortedSet:
- All Superinterfaces:Collection<E>, Iterable<E>, Set<E>
- All Known Subinterfaces:NavigableSet<E>
- All Known Implementing Classes:ConcurrentSkipListSet, TreeSet
- 有关SortedSet的方法:
1)Comparator comparator():Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements
2)Object first():Returns the first (lowest) element currently in this set
3)Object last():Returns the last (highest) element currently in this set
4)SortedSet subSet(fromelement,toelement):
Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive
5)SortedSet headSet(toelement):Returns a view of the portion of this set whose elements are strictly less than toElement
6)SortedSet tailSet(fromelement):Returns a view of the portion of this set whose elements are greater than or equal to fromElement
- SortedSet的示例:
import java.util.*; public void sortedsetdemo(){
SortedSet<String> sortedset = new TreeSet<String>();
Collections.addAll(sortedset, "1 2 3 4 5".split(" "));
System.out.println(sortedset);
System.out.println(sortedset.first());
System.out.println(sortedset.last());
System.out.println(sortedset.subSet("1","4"));
System.out.println(sortedset.headSet("3"));
System.out.println(sortedset.tailSet("2"));
}
输出:
[1, 2, 3, 4, 5]
1
5
[1, 2, 3]
[1, 2]
[2, 3, 4, 5]
下一节学习“java集合类(五)About Map”!
### 学习从来都是一个过程,对对错错对对...若文中有错误,还望读者批评指出 ###
java集合类(四)About Set的更多相关文章
- 做JavaWeb开发不知Java集合类不如归家种地
Java作为面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对象进行存储.但是使用数组存储对象方面具有一些弊端,而Java 集合就像一种容器,可以动态地把多个对象的引用放入容 ...
- 【转载】Java集合类Array、List、Map区别和联系
Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...
- Java集合类中的哈希总结
JAVA集合类中的哈希总结 目 录 1.哈希表 2.Hashtable.HashMap.ConcurrentHashMap.LinkedHashMap.TreeMap区别 3.Hashtable.Ha ...
- Java集合类: Set、List、Map、Queue使用场景梳理
本文主要关注Java编程中涉及到的各种集合类,以及它们的使用场景 相关学习资料 http://files.cnblogs.com/LittleHann/java%E9%9B%86%E5%90%88%E ...
- Java集合类: Set、List、Map、Queue使用
目录 1. Java集合类基本概念 2. Java集合类架构层次关系 3. Java集合类的应用场景代码 1. Java集合类基本概念 在编程中,常常需要集中存放多个数据.从传统意义上讲,数组是我们的 ...
- 基础知识《六》---Java集合类: Set、List、Map、Queue使用场景梳理
本文转载自LittleHann 相关学习资料 http://files.cnblogs.com/LittleHann/java%E9%9B%86%E5%90%88%E6%8E%92%E5%BA%8F% ...
- java集合类(五)About Map
接上篇“java集合类(四)About Set” 这次学完Map之后,就剩队列的知识,之后有关java集合类的学习就将告一段落,之后可能会有java连接数据库,I/O,多线程,网络编程或Android ...
- java集合类(三)About Iterator & Vector(Stack)
接上篇:java集合类学习(二) Talk about “Iterator”: 任何容器类,在插入元素后,还需要取回元素,因为这是容器的最基本工作.对于一般的容器,插入有add()相关方法(List, ...
- java中四种引用类型
java中四种引用类型 今天看代码,里面有一个类java.lang.ref.SoftReference把小弟弄神了,试想一下,接触java已经有3年了哇,连lang包下面的类都不了解,怎么混.后来在 ...
随机推荐
- Developers, do consider different user roles! - A bad experience with cron
The Story: Last week, I found one of our embedded arm linux device ran out of flash space( totally ...
- SQL备份(全)
====================================================================== SQL备份 ======================= ...
- id,class,name区别
id,class,name区别 id:标签唯一标识,好比我们身份证号码,具有唯一性.JS常用document,getGlementBy(id). class:标签的类别,可重复使用,CSS常用. na ...
- poj 3268 Silver Cow Party
S ...
- Standford CoreNLP
Stanford CoreNLP Stanford CoreNLP提供一组自然语言处理的工具.这些工具可以把原始英语文本作为输入,输出词的基本形式,词的词性标记,判断词是否是公司名.人名等,规格化日期 ...
- unity打包android游戏部分问题总结
一:虚拟导航栏挡到游戏按钮: 解决方案如下: 1.获取焦点的时候隐藏 虚拟导航条 Navigation bar 隐藏导航条 2.出现导航条的时候,改变游戏界面大小 Unity tidbits: cha ...
- 【风马一族_git_github】github项目建成网站
---------------------------------------- 1)点击 Settings 2)上一个红色矩形是访问的网址,下一个红色矩形是用来设置页面相关的信息 3)页面信息的修改 ...
- 【风马一族_Android】Android 前端内容
Android 前端内容 4.1 View 类概述 4.1.1 关于 View //类型说明 view(视图)指的是用户界面组件的基本构建基块.一个视图占据屏幕上的矩形区域,负责绘图和事件处理.视图是 ...
- 浅析 GRUB 如何加载 linux kernel
前言 对于 GRUB 的加载流程,网上绝大部分都是写对 menu.lst, grub.cfg 这些 GRUB 配置文件的编写流程,就像是写脚本语言一样,用些关键字就能让 PC机能正确启动桌面 Linu ...
- Android String format 通过value 下的string.xml 文件
<string name="format_coordinate" formatted="false">%s %d° %d\' %d\" % ...