Java Collection Class Learning summary

Source: Internet
Author: User

The collection class for Java is divided into two types, collection and map,collection are also set and list.

1. Whether the elements in the collection class are the same

HashSet uses equals and hashcode two functions together to determine

TreeSet using CompareTo in the comparable interface, or compare functions in comparator

ArrayList using the Equals function

The key in HashMap is the same as HashSet, and value uses the Equals function

The key in TreeMap is the same as TreeSet, and value uses the Equals function


2. Whether the collection can contain null, or duplicate elements

HashSet can contain null, cannot contain duplicate elements

TreeSet with HashSet, can contain a null, adding a second null throws an exception

ArrayList can contain null, or it can contain duplicate elements

The key in HashMap is the same as hashset, and value can contain more than one null

The key in TreeMap is the same as TreeSet, and value can contain more than one null


3. About the contains and remove functions

Hashset,treeset,arraylist can use the following function to determine whether to include an element, or to delete an element

Boolean contains (Object o)

Boolean remove (Object o)


Hashmap,treemap has the following functions:

Boolean ContainsKey (Object key)

Boolean containsvalue (Object key)

Boolean remove (Object key)


In the implementation of these functions, you need to call the Equals method, so is the Equals method that invokes the parameter (with the element in the collection as the equals function argument) or the Equals method of each element?

The answer is the Equals method that invokes the parameter. Take the implementation of the Remove method as an example

  1. Public boolean remove (Object o) {
  2.     if   (o ==  null )  {  
  3.              for   ( span class= "keyword" style= "Color:rgb (127,0,85); Font-weight:bold ">int  index = 0 ;  index < size; index++)   
  4. if (Elementdata[index] = = null) {
  5. Fastremove (index);
  6. return true;
  7. }
  8.     } else  {  
  9.         < span class= "keyword" style= "Color:rgb (127,0,85); Font-weight:bold ">for   (int  index = 0 ; index <  size; index++)   
  10. if (O.equals(Elementdata[index])) {
  11. Fastremove (index);
  12. return true;
  13. }
  14. }
  15. return false;
  16. }

4. collection classes and Arrays

The collection class collection has the object[] ToArray () function, which is used to convert the collection to an array.

The Arrays tool class provides a aslist (Object ... a) method for converting an array to ArrayList, noting that this is the inner class of the arrays class and also inherits from list, which is fixed in length and differs from previous ArrayList.

Java Collection Class Learning summary

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.