第十一章: Collections of Objects (2.2 容器类续)

来源:互联网 发布:c4d r17 mac 破解 编辑:程序博客网 时间:2024/06/04 18:29

9Map functionality

Map (interface)

Maintains key-value associations (pairs) so you can look up a value using a key.

HashMap*

Implementation based on a hash table. (Use this instead of Hashtable.) Provides constant-time performance for inserting and locating pairs. Performance can be adjusted via constructors that allow you to set the capacity and load factor of the hash table.

LinkedHashMap
(JDK 1.4)

Like a HashMap, but when you iterate through it, you get the pairs in insertion order, or in least-recently-used (LRU) order. Only slightly slower than a HashMap, except when iterating, where it is faster due to the linked list used to maintain the internal ordering.

TreeMap

Implementation based on a red-black tree. When you view the keys or the pairs, they will be in sorted order (determined by Comparable or Comparator, discussed later). The point of a TreeMap is that you get the results in sorted order. TreeMap is the only Map with the subMap( ) method, which allows you to return a portion of the tree.

WeakHashMap

A map of weak keys that allow objects referred to by the map to be released; designed to solve certain types of problems. If no references outside the map are held to a particular key, it may be garbage collected.

IdentityHashMap
(JDK 1.4)

A hash map that uses == instead of equals( ) to compare keys. Only for solving special types of problems; not for general use.

a)SortedMap--the keys are guaranteed to be in sorted order

Comparator comparator( ): Produces the comparator used for this Map, or null for natural ordering.

Object firstKey( ): Produces the lowest key.

Object lastKey( ): Produces the highest key.

SortedMap subMap(fromKey, toKey): Produces a view of this Map with keys from fromKey, inclusive, to toKey, exclusive.

SortedMap headMap(toKey): Produces a view of this Map with keys less than toKey.

SortedMap tailMap(fromKey): Produces a view of this Map with keys greater than or equal to fromKey.

b)HashMap—

HashMap performance factors:

Capacity: The number of buckets in the table.

Initial capacity: The number of buckets when the table is created. HashMap and HashSet have constructors that allow you to specify the initial capacity.

Size: The number of entries currently in the table.

Load factor: size/capacity.

10) Choosing an implementation

       a)Choosing between Lists

Type

Get

Iteration

Insert

Remove

array

172

516

na

na

ArrayList

281

1375

328

30484

LinkedList

5828

1047

109

16

Vector

422

1890

360

30781

       b)Choosing between Sets

Type

Test size

Add

Contains

Iteration

 

 

10

25.0

23.4

39.1

TreeSet

100

17.2

27.5

45.9

 

 

1000

26.0

30.2

9.0

 

 

10

18.7

17.2

64.1

HashSet

100

17.2

19.1

65.2

 

 

1000

8.8

16.6

12.8

 

 

10

20.3

18.7

64.1

LinkedHashSet

100

18.6

19.5

49.2

 

 

1000

10.0

16.3

10.0

       c)Choosing between Maps

Type

Test size

Put

Get

Iteration

 

 

10

26.6

20.3

43.7

TreeMap

100

34.1

27.2

45.8

 

 

1000

27.8

29.3

8.8

 

 

10

21.9

18.8

60.9

HashMap

100

21.9

18.6

63.3

 

 

1000

11.5

18.8

12.3

 

 

10

23.4

18.8

59.4

LinkedHashMap

100

24.2

19.5

47.8

 

 

1000

12.3

19.0

9.2

 

 

10

20.3

25.0

71.9

IdentityHashMap

100

19.7

25.9

56.7

 

 

1000

13.1

24.3

10.9

 

 

10

26.6

18.8

76.5

WeakHashMap

100

26.1

21.6

64.4

 

 

1000

14.7

19.2

12.4

 

 

10

18.8

18.7

65.7

Hashtable

100

19.4

20.9

55.3

 

 

1000

13.1

19.9

10.8

11) Utilities--useful utilities in the Collections class:

max(Collection)

min(Collection)

Produces the maximum or minimum element in the argument using the natural comparison method of the objects in the Collection.

max(Collection, Comparator)

min(Collection, Comparator)

Produces the maximum or minimum element in the Collection using the Comparator.

indexOfSubList(List source, List target)

Produces starting index of the first place where target appears inside source.

lastIndexOfSubList(List source, List target)

Produces starting index of the last place where target appears inside source.

replaceAll(List list,
Object oldVal, Object newVal)

Replace all oldVal with newVal.

reverse( )

Reverses all the elements in place.

rotate(List list, int distance)

Moves all elements forward by distance, taking the ones off the end and placing them at the beginning.

copy(List dest, List src)

Copies elements from src to dest.

swap(List list, int i, int j)

Swaps elements at locations i and j in list. Probably faster than what you’d write by hand.

fill(List list, Object o)

Replaces all the elements of list with o.

nCopies(int n, Object o)

Returns an immutable List of size n whose references all point to o.

enumeration(Collection)

Produces an old-style Enumeration for the argument.

list(Enumeration e)

Returns an ArrayList generated using the Enumeration. For converting from legacy code.