Set, list, and map

Source: Internet
Author: User

Java collections generally refer to subclasses of Java. util. collection.

Java ing generally refers to the subclass of Java. util. Map.

 

We don't have to look at all the APIS, but the basic method should be known.

Methods contained in the collection:

  • Size ()
  • Isempty ()
  • Contains (object)
  • Iterator ()
  • Toarray ()
  • Toarray (T [])
  • Add (E)
  • Remove (object)
  • Containsall (collection <?>)
  • Addall (collection <? Extends E>)
  • Removeall (collection <?>)
  • Retainall (collection <?>)
  • Clear ()
  • Equals (object)
  • Hashcode ()

Method included in the ing:

  • Size ()
  • Isempty ()
  • Containskey (object)
  • Containsvalue (object)
  • Get (object)
  • Put (K, V)
  • Remove (object)
  • Putall (Map <? Extends K ,? Extends v>)
  • Clear ()
  • Keyset ()
  • Values ()
  • Entryset ()

Well-known collection sub-interfaces: List <E>, set <E>

Well-known collection implementation class: abstractcollection <E>

List features: elements are ordered and can be duplicated. null is allowed as an element.

List <E> well-known implementation class: abstractlist <E>

List <E> is a famous implementation class: arraylist <E>, vector <E>, struct list <E>

Arraylist: the most basic list implementation, non-thread-safe.

Vector: similar to arraylist, but it is thread-safe and slow.

Slow list: a relatively advanced list, which can quickly insert and remove an element, but it is slow to search for an element.

 

Set features: elements are unordered and cannot be duplicated. null is allowed as an element.

It should be understood as follows:

Unordered. When traversing a set, the order obtained is different from that obtained during insertion, which is called unordered;

It cannot be repeated. It does not mean that an error is returned when two identical elements are inserted, but when the set already contains the same element, the add method returns false.

Therefore, the first null can be inserted, and the second null cannot be inserted.

In addition, the benchmark for the set judgment is equals, rather than =.

Well-known implementation classes of set <E>: hashset <E>, javashashset <E>, treeset <E>

Hashset: the location where an element is stored is related to hashcode. If hashcode is the same (equals is also the same in general), this element is not inserted. Otherwise, it is inserted to some locations according to certain rules.

Linkedhashset: It is basically the same as hashset, but it can record the order of element insertion. It has a higher overhead than hashset when inserting elements.

Treeset: The inserted elements are arranged in ascending order during traversal, from small to large. But it is inserted into this set.The object must implement the compareable Interface.

 

From the above summary, we can see that list is very similar to set.

However, according to tests, set is more efficient than list.

 

Famous implementation class of MAP: hashmap <K, V>,Hashtable<K, V>, linkedhashmap <K, V>, treemap <K, V>

Hashmap: Non-thread-safe, relatively efficient. It allows null keys and null values.

Hashtable: thread-safe, relatively inefficient. The null key and null value are not allowed.

Linkedhashmap: Non-thread-safe. It is more advanced than hashmap and ensures that the order during traversal is the order during insertion. Others are the same. It is a subclass of hashmap.

Treemap: The inserted elements are arranged in ascending order of keys during traversal, from small to large. But the key inserted into this mapThe object must implement the compareable Interface.

The key in hashmap is unique. If the put key is the same as the existing key, the original key will be overwritten, which is similar to set.

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.