Java Set Analysis Idea (i)

Source: Internet
Author: User

introduction of Collection Class

Arrays are a very common type of data structure that we can use to satisfy a lot of functions, but sometimes we encounter the following problems:

1, we need the length of the container is not certain.

2, we need it to be able to automatically sort.

3, we need to store data in the form of key-value pairs.

If the above situation is encountered, array is difficult to meet the requirements, the next chapter will introduce another similar to the array of data structures-collection classes, collection classes in Java has a very important significance, save temporary data, management objects, generics, web framework, and so on, many are used in a large number of collection classes.

Common collection classes have these kinds:

Implements the collection interface: Set, List, and their implementation classes.

Implementation of the Map interface: HashMap and its implementation class, we often have a map and its implementation class hashmap,hashtable,list, set and its implementation class ArrayList, HashSet, because the collection class is a large piece of content, We are not convenient to write all of its content, can only slowly increase, I hope that readers have their own ideas, enthusiastically to me, we work together to create exquisite blog for the vast number of programming enthusiasts to learn, below me we through a picture to the overall description:


This picture can't be shown very clearly, so I uploaded the original picture to my resources: http://download.csdn.net/detail/zhangerqing/4711389. If you want to see clearly, go down.

The table below may be more direct in their differences and relationships:

Interface

Briefly

Realize

Operational characteristics

Member Requirements

Set

Members cannot repeat

HashSet

External unordered traversal of members

A member can be an object of any object subclass, but if you override the Equals method, pay attention to modifying the Hashcode method.

TreeSet

An externally orderly traversal of members; An operation that implements the order of SortedSet, support subsets, etc.

Members require the implementation of the Caparable interface, or the use of comparator constructs TreeSet. Members are generally of the same type.

Linkedhashset

External traversal of members by member's Insertion Order

The members are similar to HashSet members

List

Provides index-based random access to members

ArrayList

Provides fast, indexed member access and better support for tail-member additions and deletions

A member can be an object of any object subclass

LinkedList

Better support for adding and removing members from any location in the list, but poor performance on indexed member access support

A member can be an object of any object subclass

Map

Saves key value pairs, sorts keys based on key search operations, CompareTo or Compare methods

HashMap

To meet the user's common needs for map

A key member can be an object of any object subclass, but if you override the Equals method, pay attention to modifying the Hashcode method.

TreeMap

To support the orderly traversal of the key, it is recommended to use HashMap to add and remove members, and finally to generate TreeMap from HashMap, additional implementation of the SortedMap interface, supporting the order of the sub-map and other requirements of the operation

The key member requires the implementation of the Caparable interface or the use of comparator constructs treemap. Key members are generally of the same type.

Linkedhashmap

Preserve the insertion order of keys and check the equality of keys and values with the Equals method

A member can be an object of any object subclass, but if you override the Equals method, pay attention to modifying the Hashcode method.

Identityhashmap

Use = = To check the equality of keys and values.

The members are using strictly equal

Weakhashmap

Its behavior relies on garbage collection threads, with no absolute justification for less

(above figure comes from the summary of the Netizen, already do not know which is original, do not post the address, if the original author see please contact me, will post a link.) )

The class that implements the collection interface, such as set and list, they are single value elements (in fact, the set is also used in the map to achieve, just like the key value, from the surface understanding, is a single value), unlike the implementation of the Map interface class, which contains Key-value (key-value pairs) form of data. This makes them a lot of different points, such as traversal, the former can only use iteration or loop to take out the value, but the latter can use the key to obtain the value.

Ii. Basic methods and use

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

implementation of the map interface

HashMap

The implementation class of the set is HashSet, and the underlying or the map interface is invoked, so here I will say some of the methods of the map interface and its implementation class. The original methods in the map interface are:

public abstract int size ();
Public abstract Boolean isempty ();
Public abstract Boolean ContainsKey (Object paramobject);
Public abstract Boolean Containsvalue (Object paramobject);
Public abstract V Get (Object paramobject);
Public abstract V-put (K Paramk, v PARAMV);
Public abstract V-Remove (Object paramobject);
public abstract void Putall (map< extends K,? extends v> Parammap);
public abstract void Clear ();
Public abstract set<k> keyset ();
Public abstract collection<v> values ();
Public abstract set<entry<k, v>> entryset ();
Public abstract Boolean equals (Object paramobject);
public abstract int hashcode ();

The attentive reader here will see that each method is preceded by an abstract keyword, which means that each method in the interface is abstract, and sometimes we write without the abstract keyword, but the JVM will add it in the process of compiling, so be careful. The abstract method means that it has no method implementation body and must be overridden in the implementation class, and then we analyze how their implementation class HashMap is done in turn.

HashMap's design is complex and ingenious, the use of a wide range, it is worth us to delve into, because HashMap is a very important point of knowledge, and here we focus on the collection class, so please see this article, about "in-depth interpretation of HASHMAP internal structure" article.

Key Method Introduction:

The first is the construction method, in most cases we take the parameterless constructor to construct the hash table,

[Java] view plain copy public HashMap () {this.entryset = null;     This.loadfactor = 0.75F;     This.threshold = 12;     this.table = new ENTRY[16];   Init (); Here are three important variables: Loadfactor, Threshold, table,loadfactor is a load factor, threshold is a critical value, and table shows the bottom of the hash table, which is actually a number of groups. Take a look at the way they are declared:

Transient entry[] table;
int threshold;
Final float Loadfactor;

Careful readers seem to have found a new problem, why the table is preceded by the transient keyword, then we cannot to study the meaning of the declaration as transient keyword: If the variable is declared as transient type, then in the serialization, ignore its value, This means that the table here, if it is going to be persisted, is not going to handle the value of the table, just ignore it, and why does this table work here? Because the HASHMAP storage structure, in fact, is an array + multiple linked lists, the array of the address of the object, the linked list to store data, so the address of the persistence is meaningless. On this piece of knowledge, I will be in another article "in-depth interpretation of the internal structure of HashMap," the focus of introduction.

HashMap initial capacity of 0, each add a pair of values, capacity has been 1, this good understanding, we pass a small example, to see the basic use of hashmap.

[Java] view plain copy package com.xtfggef.map.test;   Import Java.util.HashMap;   Import Java.util.Map;      Import Java.util.Set; /** * HashMap use * @author erqing

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.