Java set learning 1

Source: Internet
Author: User

Java set learning 1
1. The elements stored in the Set are unordered and should not be repeated.
2. The elements stored in the List are ordered and repeatable.
3. Rewrite the equals method. Generally, it is best to rewrite the hasCode method. When the object is used as the key and the index, the hasCode method will be used for search.
4. When the container object calls methods such as remove and contains, it needs to compare whether the objects are equal. This involves the equals method and
HashCode method. For custom types, you need to override the equals and hashCode methods to implement custom object equality rules. Equal objects
It should have equivalent hashCode.
5. The underlying layer of ArrayList is implemented by Dynamic Arrays for ease of searching and fast reading.
6. The underlying layer of the slow list is implemented using a two-way Linked list to facilitate insertion and deletion. Slow reading (Hash is between Array (slow reading and modification) and Linked (slow reading)
7. Set containers include HashSet and TreeSet.

8. Map interface implementation classes include HashMap (hash implementation) and TreeMap (binary tree implementation.

9. The following is the integrated Mind Map in java, which summarizes the relationships, differences, and features of integration in Java.

 

 

 

 

As shown in the figure, Java Collections are composed of three main parts: Map, Collection, Collections,

Key and value are stored in Map. Collection is the parent class of Set and List, and Collections is the operation class of Set, which can process the Set.

Collection is from List and Set.

The following first analyzes the source code of the Collection:

 

Public interface Collection
 
  
Extends Iterable
  
   
{/* Size () function returns the size of the set, that is, the number of elements in the set. The returned value is int type **/int size (); // determines whether the set is empty, if it is null, true is returned. If it is not null, falseboolean isEmpty () is returned. // you can determine whether the specified element is in the set. If so, true is returned, if it is no longer returned, falseboolean contains (Object o); // The implemented iterator interface converts the elements of the set into an Iterator and outputs The iterator.
   
    
Iterator (); // converts a set element to an Object array Object [] toArray (); // returns an array containing all elements in the collection; the runtime type of the returned array is the same as the runtime type of the specified array.
    
     
T [] toArray (T [] a); // stores Element e in the Set boolean add (E e ); // Delete the o boolean remove (Object o) element in the Collection; // whether the specified Collection element boolean containsAll (Collection
     C); // Add the set element c to the Collection boolean addAll (Collection
     C); // remove the Collection element c boolean removeAll (Collection
     C); // retain only the elements in the collection that are also included in the specified collection (optional ). Boolean retainAll (Collection
     C); // clear all the set element void clear (); // rewrite the equals method to determine whether the elements are equal boolean equals (Object o); // rewrite the hashCode method, used to determine whether the elements are equal. The return value of the collection hash code is int hashCode ();}
    
   
  
 

10. From this we can see that Collection inherits Iterable Interface, so let's look at Iterable Source code of the interface. This interface only has three functions:

 

 

Public interface Iterator
   
    
{// The pointer to the first iteration is directed to the first element. // if there are still elements that can be iterated, trueboolean hasNext () is returned. // The next element of the iteration is returned. E next (); // remove the last element returned by the iterator from the collection to which the iterator points (optional ). Void remove ();}
   

 

 

Based on the above basic knowledge, I believe that I should have some knowledge about the basic knowledge of java integration. To learn things, I must learn from the source to learn more solidly. If I have time, I should read the source code, learn the idea and code style of source code.

 

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.