The Java collection is detailed

Source: Internet
Author: User

1.java Collection Class Diagram

1.1

1.2

In the above class diagram, the real line border is the implementation class , such as Arraylist,linkedlist,hashmap, and so on, the polyline border is an abstract class , such as Abstractcollection,abstractlist,abstractmap, and the point line border is the interface , such as Collection,iterator,list.

Discover a feature, all of the above collection classes, have implemented the iterator interface, which is an interface for traversing the elements in the collection, consisting mainly of Hashnext (), Next (), remove () three methods. One of its sub-interfaces Linkediterator added three methods on top of it, namely add (), previous (), hasprevious (). That is, if the first iterator interface, then in the collection of elements, you can only traverse backwards, the traversed element will not be traversed, usually the unordered collection is implemented by this interface, such as Hashset,hashmap, and those elements of the ordered collection, The implementation of the general is the Linkediterator interface, the implementation of this interface can be a collection of two-way traversal, either through next () access to the next element, but also through previous () access to the previous element, such as ArrayList.

Another feature is the use of abstract classes. If you want to implement a collection class yourself, implementing those abstract interfaces can be cumbersome and a lot of work. Abstract classes can be used at this time, and these abstract classes give us a lot of out-of-the-box implementations, and we just need to rewrite some methods to meet our needs or add some methods to implement the set class we need, and the workflow is greatly reduced.

1.3

2. Detailed 2.1HashSet

HashSet is a subclass of the set interface, and the main feature is that it cannot hold duplicate elements, and uses a hash of the storage method, so there is no order. There is no order here: the order in which the elements are inserted is inconsistent with the order of the outputs.

code example: HashsetdemoView Codecode example: SettestView Code

2.2ArrayList

ArrayList is a sub-class of list, it and HashSet idea, allow to store repeating elements, so ordered. The order in which the elements in the collection are accessed depends on the type of the collection. If ArrayList is accessed, the iterator will start at index 0, one iteration at a time, and an index value of 1. However, if you access the elements in HashSet, each element will appear in a random order. While it is possible to determine that all elements in the collection can be traversed during the iteration, it is not possible to predict the order in which the elements are accessed.

code example: ArraylistdemoView Code2.3 Listiterator

Listiterator is an ordered sequence that can efficiently insert and delete operations at any location.

code example: LinkedlisttestView Code2.4HashMap

Refer to a previous blog: HashMap implementation principle

2.5WeekHashMapDemoView Code

Output results

View Code

Question: Why value is not recycled.

3. Compare

    is ordered Whether elements are allowed to repeat
Collection Whether Is
List Is Is
Set Abstractset Whether Whether
  HashSet
  TreeSet Yes (with binary sort tree)
Map Abstractmap Whether Use Key-value to map and store data, key must be unique, value can be repeated
  HashMap
  TreeMap Yes (with binary sort tree)

The Java collection is detailed

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.