Java中的Collection

The core interfaces are:

Collection

Set

SortedSet

List

Map

SortedMap

Queue

  

The core concrete implementation classes you need to know are the following 13:

Maps

Sets

Lists

Queues

Utilities

HashMap

HashSet

ArrayList

PriorityQueue

Collections

Hashtable

LinkedHashSet

Vector

 

Arrays

TreeMap

TreeSet

LinkedList

  

LinkedHashMap

    

List interface  A List care about the index.One thing that List has that non-lists don't have is a set of methods related to the index. Those key methods include things like get(int index), indexOf(Object o), add(int index, Object obj), and so on. All three List implementations are ordered by index position-a positon you determine either by setting a object at a specific index or by adding it without specifying position, in which case the object is added to the end.

 

ArrayList   Think of this as a growable arry. It gives you fast iteration and fast random access. It's a ordered collection by index, but not sorted. Choose this over a LinkedList when you need a fast iteration but aren't as likely to be doing a lot of insertion and deletion.

 

Vector   A Vector is basiclly the same as a ArrayList, but Vector methods are synchronized for thread safety. You'll normally want to use ArrayList instead of Vector because the synchronized methods add a performance hit you might not need. And if you do need thread safty, there are utility methods in class Collections that can help. Vector is the only class other than ArrayList to implement RandomAccess.

 

LinkedList  A LinkedList is ordered by index position, like ArrayLlist, except that elements are doubly-linked to one another. This linkage gives you new methods (beyond what you get from the List interface) for adding and removing from the beginning or end, which makes it an easy choice for implementing a stack or queue. Keep in mind that a LinkedList may iterate more slowly than an ArrayList, but it's a good choice when you need fast insertion and deletion.

 

Set interface  A Set cares about uniqueness-it doesn't allow duplicates.

HashSet  A HashSet is a unsorted, unordered Set. Use this class when you want a Collection with no duplicates and you don't care about order when you iterate throught it.

LinkedHashSet  A LinkedHashSet is ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class insead of HashSet when you care about the iteration order. When you iterate through a HashSet the order is unpredictable, while a linkedHashSet lets you iterate through the elements in the order in which they were inserted.

TreeSet  The TreeSet is one of two sorted Collections (the other being TreeMap). It using red-black tree structure, and guarantee that the elements wil be in ascending order, according to natural order. Optionally, you can construct a TreeSet with a constructor that lets you give the collection your own rules for what the order should be by using a Comparable or Comparator.

 

Map interface  A Map cares about a unique identifiers. You map a unique key to a specific value, where both the key and the value are, of course, objects.

HashMap  The HashMap gives you an unsorted, unordered Map. When you need a Map you don't care about the order(when you iterate through it), then HashMap is the way to go; the other maps add a little more overhead.  HashMap allows one null key and multiple null values in a colleciton.

Hashtable like Vector, has existed from prehistoric Java times. Hashtable is a synchronized counterpart to HashMap. HashMap lets you have null values as well as one null key, a Hashtable doesn't let you have anything that's null.

LinkedHashMap like its Set counterpart, LinkedHashSet, the LinkedHashMap collection maintains insertion order(or, optionally, access order). Although it will be somewhat slower than HashMap for adding and removing elements, you can expect faster iteration with a LinkedHashMap.

TreeMap  is a sorted Map.

 

Queue interface Although other orders are possible, Queue are typically thought of as FIFO( First in, First out). Queues support all of the standard Collection methods and they also add methods to add or subtract elements and review queue elements.

PriorityQueue   This class is new with Java 5. Since the LinkedList has been enhanced to implement the Queue interface, basic queue can be handled with a LinkedList. The purpose of a PriorityQueue is to create a "Priority in, Priority out" queue as opposed to a FIFO queue. A PriorityQueue's elements are ordered either by natural ording or according to a Comparator. In either case, the elements' ordering represents their relative priority.

Collection Interface Concrete Implementation Classes

Class

Map

Set

List

Ordered

Sorted

HashMap

x

  

No

No

HashTable

x

  

No

No

TreeMap

x

  

Sorted

By natural order or custom comparison rules

LinkedHashMap

x

  

By insertion order or last access order

No

HashSet

 

x

 

No

No

TreeSet

 

x

 

Sorted

By natural order or custom comparison rules

LinkedHashSet

 

x

 

By insertion order

No

ArrayList

  

x

By index

No

Vector

  

x

By index

No

LinkedList

  

x

By index

No

PriorityQueue

   

Sorted

By to-do order

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值