Summary of the Java Collection framework

Source: Internet
Author: User

1) Hierarchical structure of the Java Collection framework

2) operations on collections and linear tables using common methods defined by the collection interface

3) traversing the collection using the iterator interface

4) Use the JDK's enhanced for loop instead of iteration iterator for collection traversal

5) familiar with the set interface to understand when and how to use Hashset,linkedhashset or Treehashset to store elements

6) using the comparator interface to compare elements

7) Familiarity with the list interface to learn when and how to use ArrayList or LinkedList to store elements
8) distinguish between vectors and ArrayList, and learn how to use vectors and stacks

9) Simplify program design by using the general type of JDK1.5

10) Understand the difference between collection and map, know when and how to use Hashmap,linkedhashmap,treehashmap to store

11) using static methods in the Collections class
12) using static methods in the Arrays class


The above is the content of the Java collection Framework, if you are not familiar with the article, it is necessary to take a good look.

--------------------------------------------------------------------------------------------------------------- ------------------------------------

The Java Collection schema supports 3 types of collections: Rule set (set), Linear table (list), and diagram (map), respectively, defined in Set,list,map. A set instance stores a set of distinct elements (collections), a list instance stores a set of sequential elements (tables), a map that stores a set of objects---a mapping of key values


The overall architecture is as follows, very important, including the inheritance relationship, implementation of the classification, at a glance:

Collection interface:

Set interface:

HashSet Concrete Class

Linkedhashset Concrete Class

TreeSet Concrete Class

List interface:
ArrayList Concrete Class

LinkedList Concrete Class

Vector class vectors specific classes

Stack Concrete Class


Map interface:
HashMap class

Linkedhashmap class

TreeMap class
    
--------------------------------------------------------------------------------------------------------------- ------------------------------------



1) First of all, collection interface, which is the root interface of processing object collection, provides some common methods, size,iterator,add,remove something

2) Both the set and the list interface are extended from Collection,set is a set of high school mathematics, not allowed to repeat, disorderly. List is like a table, can be repeated, elements in the table is placed in order.

3) Then for the 3 implementations of the set interface:

The HashSet object must implement the Hashcode method, Javaapi Most classes implement the Hashcode method.
Linkedhashset implements an extension to the HashSet, supports the ordering of elements within the ruleset, has no order in the HashSet, and in Linkedhashset, it can be extracted in the order in which the elements are inserted into the collection
TreeSet guarantees that the elements of the set are orderly, there are 2 ways to achieve comparability between objects: 1, objects added to TreeSet implement the comparable interface, 2, specify a comparer for the elements of the ruleset (Comparator)

Tips for use:

If you want to extract the elements in the order in which they are inserted into the collection, with Linkedhashset, its elements are stored in the order in which they are added

If there is no above demand, should use hashset, its efficiency is higher than linkedhashset

Linkedhashset just keep order in the order in which they are added, and to add order attributes to the collection elements, you need to use TreeSet (the collection element has a sort relationship).


4) Again, several implementations of the list

The most important of course is ArrayList (unsynchronized) and LinkedList, a list of dynamically expanding capacity using an array implementation, and a chain-implemented list.

There is also the vector (synchronous) class, which, in addition to the synchronization method that includes access and modification vectors, is the same as ArrayList.

The last is the Stack class, it inherits from the Vector class, but generally only as a function of the stack to use, do not use the vector inside the function


5) Map

Map is a mapping, which is fundamentally different from the previous set and list.

Hash chart HashMap, chain hash chart linkedhashmap, tree diagram Treehashmap is a mapping of 3 implementations, from the name, with the above set of 3 implementations of the analysis, this is similar.

HASHMAP: High Efficiency
Likedhashmap: stored in order of addition, can be removed in order of addition
Treehashmap: Sort Sex

--------------------------------------------------------------------------------------------------------------- ------------------------------------


Collections class and Arrays class:

Collections Class (Note not collection): Provides a number of static methods to manage collections, linear tables (most of which are to manipulate linear tables, such as for linear table copying, sorting, etc., see API)

Arrays class: Provides a variety of static methods for sorting, locating, comparing, and populating elements of an array.

--------------------------------------------------------------------------------------------------------------- -------------------------------------


General types of Use:

refers to using generics in the Java collection to specify the type of element to add:

hashmap<k,v> map = new hashmap<k,v> () where K,v is a two class type, indicating that only objects of the K,v type can be populated here

In addition, only objects can be added to the collection, and the basic data types are automatically transformed into corresponding wrapper classes.

--------------------------------------------------------------------------------------------------------------- -----------------------------------

Summary of the Java Collection framework

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.