Java Collection-collection Chapter

Source: Internet
Author: User
Tags set set

     When it comes to the collection collection, we have to mention arrays, and the arrays in Java provide great convenience for us to store large amounts of data, but
is an array also has many shortcomings, not flexible enough
     1. The length of the array defined is fixed, but the length of the data in the actual development is often not fixed.
     2. Only data of the same type can be stored in an array, but it is not possible to store data of multiple data types in an array
     In order to solve the problem of the array, the collection is answered
Introduction of Collection Collection system
     Collections are used to hold multiple elements, and because of the underlying data structure, Java provides a variety of collection classes, which have a common
Sexual things, and then constantly upward extraction of these public, then formed a collection system
     The collection collection can only be used to hold objects; The length is not fixed; it is possible to store multiple types of objects (typically one only)
     Framework
     Collection
|--list
|--arraylist
|--vector
|--linkedlist
|--set
|--hashset
|--treeset
Second, detailed
     Iterator is an iterator used specifically to iterate over the collection, see the Java API
     Collection
Super class, a public method that stores and changes data and additions
Iterator usage:
Create collection Object C by polymorphism and point to subclass arraylist//<string> This is generic, which is equivalent to a label that specifies that this object holds a String type collection<string> c = New Arraylist<string> (); C.add ("a"), C.add ("B"), C.add ("C");//iterators that create object C by polymorphic iterator<string> it = C.iterator ();//traverse each element in C and output while (It.hasnext ()) {System.out.println (It.next ());}

Collection other methods in the search API can be
    1.List
The list collection is ordered and the elements can be not unique
The iterator iterator has an iterator that also has a specialized iteration list--->listiterator (that is, the iterator subinterface)
Concurrency modification exceptions sometimes occur when using iterator and can be solved with listiterator.
      1>arraylist
   Data structure: Bottom is array
   Advantages: Fast query, adding and deleting slow
         Threads are not synchronous, so they are not secure, but they are highly efficient
      2>vector (rarely used now)
   Data structure: Bottom is array structure
   Advantages: Fast query, adding and deleting slow
Thread synchronization, so it's more secure, but inefficient
      3>linkedlist
   Data structure: Bottom is linked list (end-to-end)
   Advantages: Slow query, adding and deleting fast
Thread is out of sync, so it's unsafe, but high efficiency
    2.Set
The set set is unordered (different order of storage and fetch), but the element is unique
      1>hashset
   The underlying is a hash table (the hash value is used to indicate the identity of the element, but is not guaranteed to be unique)
        Characteristics:
   A. There is no guarantee that the iteration order of the data will be constant (the order of each iteration may be different)
   B. Uniqueness:
Element uniqueness is guaranteed by the hashcode () value and Equals () (two methods are overridden)
Hashcode () value is the same
    Different: Take the Equals () method to determine whether the element is the same as the specific content
  True: Elements are duplicated and not added to the collection
  False: element is not duplicated and added to the collection
    Same: element repeats not added
      2>treeset
  The bottom layer is a two-fork tree, which is stored and retrieved through the tree nodes.
  The elements can be naturally sorted according to the construction method or the comparator is sorted, and the uniqueness can be ensured at the same time.
  A. Natural sorting
A parameterless construct that requires the class to which the object belongs implements the comparable interface (that is, the CompareTo method)
  B. Comparator sequencing
With parameter construction, the parameter is an object that already implements the comparator interface (involving the use of anonymous internal classes)
The Collections Tool class provides a series of static methods for manipulating the collection collection class.
Third, summary
      Through a blog summarizing a stage of learning, you can well comb the knowledge points, will have their own new understanding appears
      There are a lot of things about the set that need constant knocking code to reach proficiency in using

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Collection-collection Chapter

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.