何时使用List和Set

04-03-30 banq

Order of elements placed into the collection matters. For example you want to get the items out in the same order they were put in.

Use a List

- see previous criteria

If you often access by index in the list

Use ArrayList

If you want to access things that implement the Comparable interface in sorted order according to method compareTo

Use TreeSet

If order is unimportant. You only need to know if elements are in the collection or need to iterate over elements in arbitrary order

Use a Set