I understand the use of parts of the Java Collection Framework (collection and MAP)

Source: Internet
Author: User
Tags dashed line

 The so-called set is a set of data that is similar to an array. Java provides classes and interfaces for working with collection data for our use .

Because the length of the array is fixed, it is troublesome to handle an indefinite amount of data, so there is a collection.

The following is a Java collection Framework (short dashed line indicates an interface, a long dashed line represents an abstract class, a solid lines represents a class, an arrow represents an implementation interface or Inheritance) (the diagram found on the network, do not know the original author, please contact me to Delete) (in short, The relationship is very complex, so don't remember this diagram, just to scare ).

 The following is a concise version of the collection (set) and map (map) of personal understanding? For the moment, Let's just say that the mind map is good to use, the following is two mind map is to facilitate understanding of their own simplified diagram, does not represent the real inheritance and implementation of the relationship .

1) Collection

Collection (set)

1) list (list) order, repeatable. (order: The elements are sorted by the order you insert) (repeatable: there can be elements of the same content, i.e. a.equals (b) ==ture);

2) set (set) is not sequential and cannot be duplicated. (no order: elements do not necessarily sort in the order of insertion, they have their own sort within the Class) (non-repeatable: a.equals (b) ==false);

1.1) list is divided into arraylist,vector,linkedlist and so on

     ArrayList: an inherently variable-length array (in fact, The system automatically helps you change a large array), the bottom layer is implemented by the array, not Thread-safe.

vector : Similar to ArrayList and Variable-length arrays, but thread-safe, so slower than Arraylist.

/* thread safety: When multithreaded access, the lock mechanism is used, and when a thread accesses the data, other threads cannot access the Data. */

    Linkedlist: the underlying is a linked list structure. Insert and delete operations are faster relative to the collection of array Types.

/* the list structure takes less time to insert the deleted data, and the array structure spends less time on random access */

    

1 ImportJava.util.*;2 3ArrayList Al =NewArrayList ();//the default array size is 10, and you can pass in Parameters4arraylist<int> al =Newarraylist<int> ();//use generics to specify the data types that can only be passed in5 6 //common part methods are as follows7 8Al.add (parameter);//inserting elements9Al.get (subscript);//gets the specified subscript elementTenAl.clear ();//empty all elements inside the container oneAl.isempty ();//determines whether the empty aAl.remove (the value of subscript or data);//Delete Element -Al.size ()//get the number of container (array) elements -Al.set (subscript, Value)//change a value of an underlying theAl.iterator ()//It is important that collection almost always have a method that returns an iterator, followed by a usage -  - //vector and LinkedList methods are almost the Same. - //It is puzzling that vector (vector) has the method capacity () can get the container size, other classes do not
ArrayList Example

2.2) set is divided into Hashset,treeset,linkedhashset and so on

Set is different from list, it is no order and cannot be duplicated. Users who do not care about the order of the data, only care about whether it exists in the collection, can choose the set Type.

    HashSet: sorts according to the hash value of the data element.

    TreeSet: the bottom layer is a two-fork tree structure. The default minimized tree sort (from small to large).

    Linkedhashset: list structure, sorted by insert Order.

The common method of set is the same as the list class, which are deleted and Modified.

It is important to iterate through the elements using iterators, and to access the elements in the set and list in the following ways

// assume that there is a set type or a list type Object a; Iterator I=  a.iterator ();  while (i.hasnext ()) {          // If there is an element       System.out.println (i.next ());   // seems to be the movement of the pointer }// This method is common to a collection containing iterator (), such as set and list, etc.

2) Map

The map differs from the collection collection by pure Data. a unit structure for a map is a key-value pair <key,value>, which is equivalent to a map. (lenovo understanding: Map maps, key addresses, value LOCATIONS)

(an address (key) can only point to a single location, but the location (value) is not necessarily represented by an address (key). )

Maps are not necessarily sequential and cannot be duplicated.

A key corresponds to a value, and a value can have more than one key corresponding to it. (similar to a Function)

In the map, you can find the corresponding value by finding the key (just like the Array's subscript access, The difference is that the key is not necessarily the value of the int type, it can be of various types).

    HashMap: not in order to establish <key,value>, sort by the hash value of the element (it does not seem to follow the size of the hash value).

    TreeMap: the bottom is a two-fork tree, minimizing the Tree.

    Linkedhashmap: linked list structure, According to the order of establishing <key,value>, it seems that the list structure and data structure are linear structure, so they are sorted in order of Insertion.

1 ImportJava.util.*;2 3Map m =NewHashMap ();//or the following is more recommended4map<integer,string> m =NewHashmap<integer,string> ();//uses generics, which specifies the type of key-value pairs <key,value>. 5 6 //Suppose a second construction method has been used7M.put (2, "ii");//Unlike the collection type, Add,map is added to the element with put8M.put (1, "one");//because the key value pairs are <Integer,String> types, only the parameters of the specified type can be Passed. 9M.put (4, "four");TenM.put (2, "ii");//already exists and will no longer join one  aM.size ()//returns the number of elements -M.get (key)//get the value of the corresponding key
HashMap Example

Map can also be understood as a collection of Forms.

1) Key Collection.

2) Values Collection.

3) key-values Map Collection.

1 ImportJava.util.*;2 3map<string,string> m =NewHashmap<string,string> ();//Mapping of <String,String>4 5M.put ("dong", "tsing Lung"));6M.put ("west", "white Tiger"));7M.put ("south", "rose Finch"));8M.put ("north", "xuanwu");9 TenSystem.out.println (m.keyset ());//KeySet () Returns a collection of key values, emphasizing that the map is not sequential, so it is not necessarily output in the order of insertion, where the output sequence is one //south, north, east, West a  -System.out.println (m.values ());//returns the collection of value values here is the Rose finch, xuanwu, tsing lung, White Tiger -  theSystem.out.println (m.entryset ());//returns the collection of Key-value pairs <key,value> here is [south = finch, north = xuanwu, East = tsing lung, West = white tiger] -    
View Code

The preliminary understanding is such superficial, if there is anything wrong or add welcome to the News.

End

Write Time: 2017-07-24 22:21:52

Modification Time:

I understand the use of parts of the Java Collection Framework (collection and MAP)

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.