-----Collection of Java knowledge grooming

Source: Internet
Author: User

Iterator Interface: Iterates through the elements in the collection collection, called Iterators.

boolean hasnext ();

Object next ();

void Remove ();

When using iterator to iterate over a set, the concurrentmodificationexception exception occurs if the elements in the collection are also manipulated using the method of the collection object, so the element can only be manipulated using the iterator method at the time of iteration. However, the iterator method is limited, it can only judge the elements, remove and remove operations, if you want to do other operations, such as adding, modifying, etc. need to use its sub-interface: Listiterator.

|------listiterator: Add additional methods on iterator to support bidirectional output

void Set (E);

void Add (E);

boolean hasprevious (); Returns whether the iterator association collection has a previous element;

Object Previous (); Returns the previous element of the iterator;

Collection

|-----List: elements are ordered and elements can be duplicated because the set system has indexes that determine whether the elements are the same, based on the Equals method of the elements.

|------ArrayList: The underlying data structure is an array; Features: Fast query, adding and removing slightly slow; thread is out of sync

|------LinkedList: The underlying data structure is linked list; Features: quick and easy to delete, query slightly slow;

|------Vector: The bottom layer is the array data structure; thread synchronization is inefficient; replaced by ArrayList

ArrayList and vector classes are based on the array implementation of the list class, the vector is older, replaced by ArrayList, ArrayList is thread insecure, and vector is thread-safe, but it is recommended to use ArrayList;

Gets the thread-safe ArrayList object: Method in the Collections class: Static list Synchronizedlist, returns a thread-safe listing.

|-----Set: Elements are unordered (the order in which they are deposited and taken out is not necessarily consistent), elements cannot be duplicated

|------HashSet: The underlying data structure is a hash table, and threads are out of sync.

HashSet guarantees that the uniqueness of the element is done through the hashcode and the Equals method, if the element's Hashcode value is the same, it will determine if the equals is true, and if the element's Hashcode value is different, equals is not called For comparison.

|------TreeSet: The underlying data structure is a two-fork tree that allows you to sort the elements in the Set collection

1, the basis of ensuring the uniqueness of elements is the CompareTo method

2. Sort-----Natural Sort

To make the elements themselves comparable, elements need to implement the comparable interface, overriding the CompareTo method

3. Sort-----Comparator Sort

When an element is not comparable, or has a comparison that is not required, it is possible to define the comparer yourself, implement the comparator interface, override the Compare method, and then pass the comparer object as a parameter to the constructor of the TreeSet collection.

Map

|------Hashtable: The bottom layer is a hash table data structure, can not be stored null key null value, thread synchronization, inefficient

|------HASHMAP: The underlying is a hash table data structure that allows NULL keys and null values, threads out of sync, high efficiency

|------TreeMap: The bottom layer is a two-fork tree data structure, threads are out of sync and can be used to sort the keys of the map collection

There are two ways of traversing the map collection:

map<integer,string> m = new hashmap<integer,string> ();

M.put (1, "A");

M.put (2, "B");

M.put (3, "C");

The first way

Set s = m.entryset ();

Iterator it = S.iterator ();

while (It.hasnext ()) {

Map.entry en = (Entry) it.next ();

System.out.println (En.getkey () + ":" +en.getvalue ());

}

The second way

s = M.keyset ();

it = S.iterator ();

while (It.hasnext ()) {

int i = (int) it.next ();

System.out.println (i+ ":" +m.get (i));

}

-----Collection of Java knowledge grooming

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.