数组
type[];
可返回一个数组
java 提供了四种类型的“集合类”:Vector(矢量)、BitSet(位集)、Stack(堆栈)以及 Hashtable(散列表)
stack 实现了一个 LIFO(先入先出)序列,而 Hashtable 是一种 “关联数组”,允许我们将任何对象关联起来。除此以外,所有Java 集合类都能自动改变自身的大小
集合实际容纳的是类型 为Object 的一些对象的句柄。这种类型当然代表Java 中的所有对象,因为它是所有类的根。当然,也要注 意这并不包括基本数据类型(
利用象 Integer、Double 之类的“封装器”类 )
Vector
Vector中的对象为object,所以你可以放任何对象进去,即使对象类型不同。所以为了防止放入错误类型,可以生成特定vector(has a vector)
class Gopher { private int gopherNumber; Gopher(int i) { gopherNumber = i; } void print(String msg) { if(msg != null) System.out.println(msg); System.out.println( "Gopher number " + gopherNumber); } } class GopherTrap { static void caughtYa(Gopher g) { g.print("Caught one!"); } } class GopherVector { 216 private Vector v = new Vector(); public void addElement(Gopher m) { v.addElement(m); } public Gopher elementAt(int index) { return (Gopher)v.elementAt(index); } public int size() { return v.size(); } public static void main(String[] args) { GopherVector gophers = new GopherVector(); for(int i = 0; i < 3; i++) gophers.addElement(new Gopher(i)); for(int i = 0; i < gophers.size(); i++) GopherTrap.caughtYa(gophers.elementAt(i)); } } ///:~
java中的interator:
Enumeration。除下面这些外,不可再用它 做其他任何事情:
(1) 用一个名为 elements()的方法要求集合为我们提供一个 Enumeration。我们首次调用它的 nextElement()
时,这个Enumeration会返回序列中的第一个元素。
(2) 用nextElement()获得下一个对象。
(3) 用hasMoreElements()检查序列中是否还有更多的对象。
BitSet
实际是由“二进制位”构成的一个 Vector ,最小长度是一个长整数(Long)的长度:64 位
Stack
Stack stk = new Stack();
push(), pop()
Vector 操作亦可针对 Stack 对象进行。这可能是由继承的特质决定的——Stack“属于”一种 Vector。因 此,能对Vector进行的操作亦可针对Stack进行,例如elementAt()方法。
Hashtable
散列码可以获取对象中的信息,然后将其转换成那个对象“相对唯一”的整数(int)。所有对象都有 一个散列码,而hashCode()是根类Object的一个方法。Hashtable获取对象的hashCode(),然后用它快速 查找键。这样可使性能得到大幅度提升。
Hashtable ht = new Hashtable();
containsKey();get();put()
新集合
模仿c++ stl
(1) 集合(Collection):一组单独的元素,通常应用了某种规则。在这里,一个List(列表)必须按特定 的顺序容纳元素,而一个 Set(集)不可包含任何重复的元素。
(2) 映射(Map):一系列“键-值”对。Map 就可以返回自己键的一个Set、一个包含自己值的List 或者包含自己“键
-值”对的一个List。
和数组相似,Map 可方便扩充到多个“维”,需简单地在一 个Map里包含其他Map(后者又可以包含更多的Map,以此类推)。
虚线框代表“接口”,点线框代表“抽象”类,而实线框代表普通(实际)类。点线箭头表示一个特定的类 准备实现一个接口(在抽象类的情况下,则是“部分”实现一个接口)。
双线箭头表示一个类可生成箭头指 向的那个类的对象
List x = new LinkedList();
List x = new ArrayList();
Collection c = new ArrayList();
使用Collections
下面这张表格总结了用一个集合能做的所有事情(亦可对Set和List做同样的事情,尽管List还提供了一 些额外的功能)。Map 不是从 Collection 继承的,所以要单独对
Boolean add(Object) |
*Ensures that the Collection contains the argument. Returns false i it doesn’t add the argument. |
Boolean addAll(Collection) |
*Adds all the elements in the argument. Returns true if any element were added. |
void clear( ) |
*Removes all the elements in the Collection. |
Boolean contains(Object) |
True if the Collection contains the argument. |
Boolean |
True if the Collection contains all the elements in the argument. |
containsAll(Collection) |
|
Boolean isEmpty( ) |
True if the Collection has no elements. |
Iterator iterator( ) |
Returns an Iterator that you can use to move through the elements i the Collection. |
Boolean remove(Object) |
*If the argument is in the Collection, one instance of that elemen is removed. Returns true if a removal occurred. |
Boolean removeAll(Collection) |
*Removes all the elements that are contained in the argument. Returns true if any removals occurred. |
Boolean retainAll(Collection) |
*Retains only elements that are contained in the argument (an “intersection” from set theory). Returns true if any changes occurred. |
int size( ) |
Returns the number of elements in the Collection. |
Object[] toArray( ) |
Returns an array containing all the elements in the Collection. |
Object[] toArray(Object[] a) |
Returns an array containing all the elements in the Collection, whose type is that of the arraya rather than plainObject (you must cast the array to the right type). |
*This is an “optional” method, which means it might not be implemented by a particular Collection. If not, that method throw anUnsupportedOperationException.Exceptionswillbecoveredin Chapter 9. |
List(接口) 顺序是 List 最重要的特性;它可保证元素按照规定的顺序排列。List 为 Collection 添加了 大量方法,以便我们在 List 中部插入和删除元素(只推荐对LinkedList 这样做)。
List 也会生成一个 ListIterator(列表反复器),利用它可在一个列表里朝两个方向遍历,同时插入和删除位于列表中部的元 素(同样地,只建议对 LinkedList 这样做 )
ArrayList用于替换原先的Vector。快速访问元素,但在从列表中部插入和删除元素时,速度却嫌稍慢。
一般只应该用 ListIterator 对一 个 ArrayList 进行向前和向后遍历,不要用它删除和插入元素;
LinkedList :可以高效率地在列表中部进行插入和删除操作。进行随机访 问时,速度却相当慢。
也提供了 addFirst(),addLast(),getFirst(), getLast(),removeFirst() 以及 removeLast()(未在任何接口或基础类中定义),以便将其作为一个规格、 队列以及一个双向队列使用
Set(接口) 添加到Set的每个元素都必须是独一无二的。添加到Set里 的对象必须定义equals(),从而建立对象的唯一性。Set拥有与Collection完全相同的接口。
HashSet* 用于除非常小的以外的所有Set。对象也必须定义 hashCode()
ArraySet 由一个数组后推得到的Set。面向非常小的Set设计,特别是那些需要频繁创建和删除的。
TreeSet 由一个“红黑树”后推得到的顺序 Set。这样一来,我们就可以从一个Set 里提到一个 顺序集合
Map(接口) 维持“键-值”对应关系(对),以便通过一个键查找相应的值
HashMap* 基于一个散列表实现(用它代替Hashtable)。针对“键-值”对的插入和检索,这种形式具有
最稳定的性能。可通过构建器对这一性能进行调整,以便设置散列表的“能力”和“装载因子”
ArrayMap 由一个ArrayList后推得到的Map。对反复的顺序提供了精确的控制。面向非常小的Map设计,特
别是那些需要经常创建和删除的。对于非常小的 Map,创建和反复所付出的代价要比 HashMap 低得多。但在
Map 变大以后,性能也会相应地大幅度降低
TreeMap 在一个“红-黑”树的基础上实现。查看键或者“键-值”对时,它们会按固定的顺序排列(取决
于 Comparable 或 Comparator,稍后即会讲到)。TreeMap 最大的好处就是我们得到的是已排好序的结果。
TreeMap是含有subMap()方法的唯一一种Map,利用它可以返回树的一部分
Type |
Get |
Iteration |
Insert |
Remove |
ArrayList |
110 |
490 |
3790 |
8730 |
LinkedList |
1980 |
220 |
110 |
110 |
Type |
Test size |
Add |
Contains |
Iteration |
10 |
22.0 |
11.0 |
16.0 |
|
TreeSet |
100 |
22.5 |
13.2 |
12.1 |
1000 |
31.1 |
18.7 |
11.8 |
|
10 |
5.0 |
6.0 |
27.0 |
|
HashSet |
100 |
6.6 |
6.6 |
10.9 |
1000 |
7.4 |
6.6 |
9.5 |
Type |
Test size |
Put |
Get |
Iteration |
10 |
11.0 |
5.0 |
44.0 |
|
Hashtable |
100 |
7.7 |
7.7 |
16.5 |
1000 |
8.0 |
8.0 |
14.4 |
|
10 |
16.0 |
11.0 |
22.0 |
|
TreeMap |
100 |
25.8 |
15.4 |
13.2 |
1000 |
33.8 |
20.9 |
13.6 |
|
10 |
11.0 |
6.0 |
33.0 |
|
HashMap |
100 |
8.2 |
7.7 |
13.7 |
1000 |
8.0 |
7.8 |
11.9 |
Collections 类中含有其他大量有用的实用工具:
enumeration(Collection) |
Produces an old-style Enumeration for the argument. |
max(Collection) min(Collection) |
Produces the maximum or minimum element in the argument using th natural comparison method of the objects in the Collection. |
max(Collection, Comparator) min(Collection, Comparator) |
Produces the maximum or minimum element in theCollection using the Comparator. |
nCopies(int n, Object o) |
Returns an immutable L i s t of size n whose handles all point too. |
subList(List, int min, int max) |
Returns a new List backed by the specified argumentList that is a window into that argument with indexes starting atmin and stopping just before m a x . |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?