Review Java Collections Book part 2

II/ The Collections Framework
1/ Special Collections Support.
2/ Array Algorithm Support.
3/ Custom Implements.
4/ Compatibility Issues.
5/ Advanced Usages.

III/ Alternative Collection Libraries
6/ JGL Libraries.
7/ util.concurrent.
8/ Colt.

1/ Special Collections Support.

EMPTY_LIST: Represents an empty immutable list.
EMPTY_MAP: Represents an empty immutable map.
EMPTY_SET: Represents an empty immutable set.
binarySearch(): Searches for element in list with binary search.
copy(): Copies elements between two lists.
enumeration(): Converts a collection to an enumeration.
fill(): Fills a list with a single element.
max(): Searches for maximum value within the collection.
min(): Searches for minimum value within the collection.
nCopies(): Creates an immutable list with multiple copies of an element
reverse(): Reverses elements within list.
reverseOrder(): Returns comparator for reversing order of comparable elements. (Đảo ngược thứ tự)
shuffle(): Randomly reorders elements in list.
singleton(): Returns an immutable set of one element.
singletonList(): Returns an immutable list of one element.
singletonMap(): Returns an immutable map of one element.
sort() Reorders the elements in a list.
synchronizedCollection() Creates a thread−safe collection.
synchronizedList() Creates a thread−safe list.
synchronizedMap() Creates a thread−safe map.
synchronizedSet() Creates a thread−safe set.
synchronizedSortedMap() Creates a thread−safe sorted map.
synchronizedSortedSet() Creates a thread−safe sorted set.
unmodifiableCollection() Creates a read−only collection.
unmodifiableList() Creates a read−only list.
unmodifiableMap() Creates a read−only map.
unmodifiableSet() Creates a read−only set.
unmodifiableSortedMap() Creates a read−only sorted map.
unmodifiableSortedSet() Creates a read−only sorted set.

Empty Collections


List emptyList = Collections.unmodifiableList(new LinkedList());
// 2 câu lệnh cùng logic, nhưng dùng EMPTY_LIST sẽ nhanh hơn
List emptyList = Collections.EMPTY_LIST;

SingletonList

String[] simple = {"One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three"};
List<String> list = new ArrayList<String>(Arrays.asList(simple));
List<String> oneList = new ArrayList<String>(1);
oneList.add("One");
list.removeAll(oneList);

System.out.println(list.toString());

output

[One, Two, Three, One, Two, Three, One, Two, Three]
oneList: [One]
[Two, Three, Two, Three, Two, Three]

synchronizedCollection

Map map = Collections.synchronizedMap(new HashMap(89));
Set set = map.entrySet();
synchronized(map) {
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}

Notes: If you can avoid it, don’t waste CPU cycles by converting an historical collection like a Vector into a thread−safe List. It’s already thread−safe. While the code will still work, it will require an extra level of indirection for all method calls to ensure thread safety.

binarySearch: nhớ sort trước khi dùng


String simpsons[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" };
// Convert to list
List<String> list = new ArrayList<String>(Arrays.asList(simpsons));
// Ensure list sorted
Collections.sort(list);
System.out.println("Sorted list: [length: " + list.size() + "]");
System.out.println(list);
// Search for element in list
int index = Collections.binarySearch(list, "Maggie");
System.out.println("Found Maggie @ " + index);
// Search for element not in list
index = Collections.binarySearch(list, "Jimbo Jones");
System.out.println("Didn't find 'Jimbo Jones' @ " + index);
// Insert
int newIndex = (-index) - 1;
System.out.println("Add 'Jimbo Jones' at index: " + newIndex);
list.add(newIndex, "Jimbo Jones");
System.out.println("With Jimbo Jones added: [length: " + list.size() + "]");
System.out.println(list);

Output:


Sorted list: [length: 7]
[Bart, Homer, Hugo, Lisa, Maggie, Marge, Roy]
Found Maggie @ 4
Didn't find 'Jimbo Jones' @ -4
Add 'Jimbo Jones' at index: 3
With Jimbo Jones added: [length: 8]
[Bart, Homer, Hugo, Jimbo Jones, Lisa, Maggie, Marge, Roy]

Nếu trong list có 2 phẩn tử trùng thì trả về LatestIndex
[Bart, Homer, Hugo, Lisa, Maggie, Maggie, Roy], ta tìm Maggie <—- index: 5, ko phải là 4

Min, Max, reverseOrder


String simpsons[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" };
List<String> list = Arrays.asList(simpsons);
System.out.println(list);
// Min should be Bart
System.out.println("min: " + Collections.min(list));
// Max should be Roy
System.out.println("max: " + Collections.max(list));
Comparator comp = Collections.reverseOrder();
// Reversed Min should be Roy
System.out.println("min: " + Collections.min(list, comp));
// Reversed Max should be Bart
System.out.println("max: " + Collections.max(list, comp));

Output:


minMaxReverseOrder...
[Bart, Hugo, Lisa, Marge, Homer, Maggie, Roy]
min: Bart
max: Roy
min: Roy
max: Bart

Note: If the collection is empty, NoSuchElementException is thrown when min() or max() are called.

Copy(dest, src) : cũng giống với System.arraycopy() có một vài điểm chú ý khi dùng là

+ destination list phải dc khởi tạo

+ destination list length phải >= src list nếu ko sẽ bị IndexOutOfBoundsException

Note for Copy: The copy() method copies references between lists. If the underlying object changes, the change would be reflected in what both lists reference.

 

 

bbbb

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s