Java Collection Class Diagram

Source: Internet
Author: User
Tags sorts log4j




Ordered No

Allow elements to repeat no

Collection

Whether

Is

List

Is

Is

Set

Abstractset

Whether

Whether

HashSet

TreeSet

Yes (sort by binary tree)

Map

Abstractmap

Whether

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

HashMap

TreeMap

Yes (sort by binary tree)


4. Detailed Introduction

General Introduction to 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 list that allows repeating elements to be stored;  
Implementation class:  
ArrayList: Array implementation, query fast, adding and deleting slow, thread insecure, lightweight;
LinkedList: Linked list implementation, delete fast, query slow  
Vector: Array implementation, thread-safe, heavyweight  

Cases:

Use the Set distribution table in the Levit application:

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

Analysis:

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

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

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

list<long> groupids = new linkedlist<long> (); if (Collectionutils.isempty (Groupmemberdos)) {return groupids;
}

The background task calls this method, this scene data volume is very large, the outside loop collection type is ArrayList (query fast), to the query out the data saves the application LinkedList (increases quickly). for (Groupmemberdo Groupmemberdo:groupmemberdos) {

Here only to do the increase operation, the above analysis LinkedList is based on the linked list he does to increase the deletion soon. Groupids.add (Groupmemberdo.getgroupid ());
} return Distinctelementfilter.filterlist (Groupids);
}

Select the appropriate collection class based on the business scenario in the actual development.
2.Set:
unordered collection, not allowed to hold duplicate elements; allow null elements to be used

HashSet's backstage has a hashmap ; initialize the backend capacity; just generate a hashset, the system provide access to key only; If there are two key duplicates, then the previous one will be overwritten;  

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

Implementing Class Linkedhashset: This implementation differs from HashSet in that it maintains a double-link list that runs on all items. The stored data is ordered.

Hash Table Detailed:

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

Cases:

Use the Set distribution table in the Levit application:

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 below the Code Analysis business Scenario 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 be duplicated.

It is not allowed to have duplicate data in the collection to select HashSet.

Sub-Interface SortedSet , the implementation class for the set sort: TreeSet : Sorts the elements using their natural order, or sorts them according to the Comparator provided when the set is created;  

Definition of binary number:

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

The Levit application does not currently have a business scenario for that collection.

3. Map
HashMap : Key value pair, key cannot be duplicated, but value can be repeated; key implementation is hashset;value corresponding to put; Allow a null key or value;
Hashtable : Thread-Safe , does not allow a null key or value;
Properties :: Both key and value are string types and are used to read configuration files;

TreeMap : A well-ordered map of key, key is TreeSet, value corresponds to each key; key to implement the comparable interface or TREEMAP has its own constructor;  

Linkedhashmap: This implementation differs from HASHMAP in that the latter maintains a double-link list that runs on all items. The stored data is ordered.

Use the Set distribution table in the Levit application:

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

Analysis:

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

The following analysis Linkedhashmap the code for the scenario:

The order of the parameters is ensured here, using Linkedhashmap. map<string, string> map = new linkedhashmap<string, string> (); Judgment is from the "transfer, deposit, transfer" 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));

The WEBX framework uses Properties to read the configuration file code:

 /** *  load and initialize services in WEBX. <p> This class can be called by listener or servlet. </p> */public class webxloader implements webxcontroller, webxconstant  { private void configurelog4j (bootstrapresourceloaderservice resourceloader,  Properties props)  {         //omit part of the code ..... .....         //  If the configuration file is named *.xml, Domconfigurator is used, Otherwise, use Propertyconfigurator.         string filename = log4jconfigurationurl.getfile ();        if  (Filename.endswith (log4j_configuration_xml))  {             domconfigurator.configure ( Log4jconfigurationurl, props);        } else {            &Nbsp;props = new properties (props);             try {                 ////key section, read file to "props" Collection.---------------------------------------------------------------------------------------                 props.load ( Log4jconfigurationurl.openstream ());                 propertyconfigurator.configure (props);                 log.info ("configured log4j from "  +  Log4jconfiguration);            } catch  (ioexception e)  {                 //  logging system is not available at this time, recorded in the Servlet log                  loglog ("could not open log4j configuration  file  "                        + log4jconfigurationurl.toexternalform (),  e);             }        }         //  now it's time to start playing log.         resourceloader.setloggerready (true);     }}


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

2.Collections, provides a static method (Synchronous collection class method) that operates on collection .
Arrays Application Example code:

private void Setgoodslist (ActionResult actionresult, goodsdo[] goodsarray) {if (Goodsarray! = null) {/            /convert Array to collection class list<goodsdo> goodslist = Arrays.aslist (Goodsarray);            Omit code ... actionresult.putincontext ("Goodslist", goodslist);        Actionresult.putincontext ("GeV", GeV); }    }


Java Collection Class Diagram

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.