Java中的Collection与Map

LinkedList:链表方式实现List
ArrayList:以数组的方式实现List
如果要查找一个特定的元素且不知道他的位置,你需要访问所有的元素直到找到相匹配的。如果集合中的元素很多,那将是费时的。如果你不关心元素的顺序,有种数据结构能让你更快地查找元素。缺点是这种数据结构不能控制元素出现的顺序。
The Java collections library supplies(提供) a HashSet class that implements a set based on
a hash table.
A tree set is a sorted collection.the sorting is accomplished by a tree data structure. (Red-black tree is a self-balancing binary search tree.红黑树是一个自平衡的二叉查找树。)Adding an element to a tree is slower than adding it to a hash table. But it is still much faster than checking for duplicates in an array or linked list.(但他仍比在数组中重复地核对要快得多)
A double-ended queue, or deque(双端队列), lets you efficiently add or remove elements at the head and tail(尾). Adding elements in the middle is not supported. Java SE(标准版) 6 introduced a Deque interface. It is implemented by the ArrayDeque and LinkedList classes, both of which provide deques whose size grows as needed.
The priority queue makes use of an elegant and efficient data structure called a heap.

A set is a collection that lets you quickly find an existing element.
However, to look up an element, you need to have an exact copy of the element to find.
That isn’t a very common lookup—usually, you have some key information, and you want to look up the associated element.
The map data structure serves that purpose.
A map stores key/value pairs. You can find a value if you provide the key.
A hash map hashes the keys, and a tree map uses an ordering on the keys to organize them in a search tree. Keys must be unique. You cannot store two values with the same key. If you call
the put method twice with the same key, the second value replaces the first one.
“counts.put(word, counts.getOrDefault(word, 0) + 1);”(避免第一次取不到值)
或用
counts.merge(word, 1, Integer::sum);
associates word with 1 if the key wasn’t previously present, and otherwise combines
the previous value and 1, using the Integer::sum function.


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值