Java Collection Class Diagram

Source: Internet
Author: User
Tags hash log4j



Ordered No

Allow elements to repeat no

Collection

Whether

Is

List

Is

Is

Set

Abstractset

Whether

Whether

HashSet

TreeSet

Yes (sorted by binary tree)

Map

Abstractmap

Whether

Use Key-value to map and store data, key must be unique, value can be duplicated

HashMap

TreeMap

Yes (sorted by binary tree)

4, Detailed introduction

Introduction to Common classes

C ollection: Parent interface;  
Set: Interface---Implementation class: HashSet, Linkedhashset
List: Interface---Implementation class: Linkedlist,vector,arraylist
SortedSet: Interface---Implementation class: TreeSet

Map Interface---Implementation classes: HashMap, Hashtable, Linkedhashmap, Properties

1, List:  
List: A sequence table that allows for duplicate elements to be stored;
Implementation class:
ArrayList: Array implementation, query fast, additions and deletions slow, thread unsafe, lightweight;
LinkedList: Linked list implementation, additions and deletions fast, query slow
Vector: Array implementation, thread safety, heavyweight

Cases:

Levit use Set distribution tables in applications:

collection type Number of applications
ArrayList 184 places
LinkedList 2 places
Vector 0

Analysis:

From the above analysis, ArrayList is the most used, vector is not used (there is a performance problem, not recommended).

The following analysis of the application Linkedli St code for the scenario:

Private list<long> Getgroupids (list<groupmemberdo> groupmemberdos) {

list<long> Group Ids = new linkedlist<long> ();
        if (Collectionutils.isempty (Groupmemberdos)) {return
            groupids;
        }
//Background task calls this method, this scene data volume is very large, the outside loop collection type is ArrayList (query fast), to query out of the data Save application LinkedList (add fast).
for (Groupmemberdo groupmemberdo:groupmemberdos) {

//here only to do the add operation, the above analysis LinkedList is based on the linked list He does add delete quickly.
            Groupids.add (Groupmemberdo.getgroupid ());
        }
        Return Distinctelementfilter.filterlist (Groupids);
    }

In the actual development according to the business scene select the appropriate collection class.
2.Set:
Unordered collection, no duplicate elements allowed;

The background of the HashSet has a hashmap; initialize the background capacity; only a hashset is generated, the system only provides access to the key, and if there are two key duplicates, then the previous one will be overwritten;

The implementation class Hashset:equals return True,hashcode return the same integer; the hash table; The stored data is unordered.

Implementation Class Linkedhashset: This implementation differs from HashSet in that it maintains a doubly linked list running on all entries. the stored data is in order.

Hash Table Detailed:

Http://www.bianceng.cn/Programming/sjjg/200705/1126.htm

Cases:

Levit use Set distribution tables in applications:

Collection type
Number of applications
HashSet 3 places
Linkedhashset
0

Analysis:

From the above analysis results are not applied to the Linkedhashset,hashset application 3 of the following code analysis business scene code:

set<string> identities = new hashset<string> ();
Identities.add (vaccountidentity.tp_enterprise);
Identities.add (vaccountidentity.free_pending);
Identities.add (Vaccountidentity.tp_buyer);
The above code does not allow elements to repeat.

You do not allow duplicate data in the collection to select HashSet.

Sub-Interface SortedSet, the set Sort implementation class:TreeSet: The element is sorted using the natural order of the elements, or sorted according to the Comparator provided when the set was created;

two definition of fork number:

Http://www.comp.nus.edu.sg/~xujia/mirror/algorithm.myrice.com/datastructure/basic/binary_tree/chapter1.htm

the Levit application uses the collection at present without a business scenario.

3, Map
HashMap: Key value pairs, key cannot repeat, but value can be repeated, key implementation is hashset;value corresponding to put, allow null key or value;
Hashtable: A thread-safe key or value that does not allow null;
Properties:: Key and value are string types that are used to read the configuration file;

TreeMap: A map of the key row good order; Key is TreeSet, value corresponds to each key; Key to implement the comparable interface or TREEMAP has its own constructor;

Linkedhashmap: The difference between this implementation and HASHMAP is that the latter maintains a doubly linked list running on all the entries. the stored data is in order.

Levit use Set distribution tables in applications:

Collection type
Number of applications
HashMap 142 Places
Hashtable 0
Properties 0
TreeMap 0
Linkedhashmap 3

Analysis:

From the above analysis, the results of the analysis with the list are roughly the same, hashmap the most application scenarios.

The following analysis Linkedhashmap The application scenario code:

Here to ensure the order of the parameters, using Linkedhashmap.

map<string, string> map = new linkedhashmap<string, string> ();


Judgment is from the "transfer, deposit, transfer out" page to the success of the page

map.put (Transferconstants.sc_success_u, transfertype);

Map.put ("id", b);


Actionresult.setredirecturl (Getrender (transferconstants.redirect_success, map));

Read the profile code using Properties in the WEBX framework:

 /** * Mounts and initializes services in WEBX. <p> This class can be invoked through a listener or servlet. </p> * Public class Webxloader implements Webxcontroller, webxconstant {private void configurelog4j (bootstrapres Ourceloaderservice Resourceloader, Properties props) {//Omit part of code ...////If the profile name is *.xml, use DOMC
        Onfigurator, otherwise use propertyconfigurator.

        String filename = Log4jconfigurationurl.getfile ();
        if (Filename.endswith (Log4j_configuration_xml)) {domconfigurator.configure (log4jconfigurationurl, props);

            else {props = new Properties (props); Try {////Critical part, reading the file into the "props" collection.---------------------------------------------------------------------------
                ------------Props.load (Log4jconfigurationurl.openstream ());
                Propertyconfigurator.configure (props);
            Log.info ("Configured log4j from" + log4jconfiguration); catch (IOException e) {//This time logThe ging system is not yet available and is logged in the servlet log loglog ("Could not open log4j configuration file" + Log4
            Jconfigurationurl.toexternalform (), E);
        }//Now you can start the log.
    Resourceloader.setloggerready (TRUE); }
}

4, two tools Arrays and collections
1. Arrays, this class contains various methods for manipulating arrays, such as sorting and searching.

2.collections, mainly provides the static method that operates on collection (Synchronous Collection class method).
Arrays Application Instance Code:

private void Setgoodslist (ActionResult actionresult, goodsdo[] goodsarray) {
        if (Goodsarray!= null) {
            // Converts an array into a collection class
            list<goodsdo> goodslist = arrays.aslist (Goodsarray);

            Omit code ...
            Actionresult.putincontext ("Goodslist", goodslist);
            Actionresult.putincontext ("GeV", GeV);
        }
    

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.