Summary of use of collection classes in jdk5.0

Source: Internet
Author: User

In 5.0, the biggest change to collection is that you can specify its specific type:
List List = new List ;

Two basic interfaces:
Public interface Collection
{
Boolean add (E element );
Iterator Iterator ();
...
}

Public interface Iterator
{
E next ();
Boolean hasNext ();
Void remove ();
}

Before 5.0, common forms were:
Collection C = ...;
Iterator Iter = c. iterator ();
While (iter. hasNext ())
{
String element = iter. next ();
Do something with element
}
But add another loop method in 5.0, similar to for each:
For (String element: c)
{
Do something with element
}
This method applies to any class that implements the Iterable interface.

When using remove, note that you must call the next method once before calling remove, because next is like moving a pointer, remove deletes the pointer that just jumped. Even if you want to delete two adjacent items consecutively, you must call next before each deletion.

Sort and search collections
The sort method of the Collections class can sort any classes that implement the List interface. In the sorting process, these classes implement the Comparable interface by default. If you want to sort by other methods, you can provide a Comparator object when calling the sort method:
Comparator ItemComparator = new
Comparator ()
{
Public int compare (Item a, Item B)
{
Return a. partNumber-B. partNumber;
}
});
Reverse sorting:
Collections. sort (items, itemComparator );
Collections. sort (items, Collections. reverseOrder (itemComparator ));

Find an object:
I = Collections. binarySearch (c, element );
I = Collections. binarySearch (c, element, comparator );
However, these lists must be sorted. Note that this algorithm requires random collection access. If random access is not supported, the efficiency of this algorithm may be very low.

Several common collections:
ArrayList
An indexed sequence that grows and shrinks dynamically
It can be accessed randomly, but deleting an object from the middle will affect the efficiency, because some undeleted objects need to be adjusted accordingly. Non-thread security, but the efficiency is higher than that of Vector. If it is in a single thread, select it instead of Vector.

Shortlist
An ordered sequence that allows efficient insertions and removal at any location
It can only be accessed in order, and it is easy to add or delete. Although the get (n) method is provided, it is actually sequential access. If you find that this method is used in the shortlist, consider whether the List type is suitable.

HashSet
An unordered collection that rejects duplicates
Using hashcode as an index is suitable for finding an object without knowing its location. Repeatable

TreeSet
A sorted set
Similar to HashSet, but the stored objects are sorted

LinkedHashSet
A set that remembers the order in which elements were inserted


PriorityQueue
A collection that allows efficient removal of the smallest element
When a Queue is added, a priority is given, and the lowest priority is obtained from the queue.

HashMap
A data structure that stores key/value associations
Stores key/value pairs, which are non-thread-safe and more efficient than HashTable.

TreeMap
A map in which the keys are sorted
Sorted HashMap

LinkedHashMap
A map that remembers the order in which entries were added


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.