A collection of Java program performance optimizations
Collection
Common data structures such as the list Map set used in combination
Let's take a look at the list interface related classes have ArrayList Vector LinkedList they all implement the list interface and extend from abstractlist
ArrayList and vector internal are implemented by the array and LinkedList is implemented with a doubly linked list, so if the query operation of the data is more frequent use of ArrayList if the data are deleted insert operations more frequently, The LinkedList is used. As for vector is a thread-safe collection, but in fact ArrayList and vector are functionally similar.
Next look at the map interface
The map is basically divided into two major factions
One is to implement the map interface and extend the HashMap <--linkedhashmap from Abstractmap TreeMap
One is to implement the map interface and extend self-dictonnary with hashtableßpropertities
About HashMap is a thread-insecure combination, which roughly maps the hash algorithm directly to the memory fetch value
The basic operations are as follows:
ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.LinkedHashMap;ImportJava.util.Map;ImportJava.util.Map.Entry; Public classASDF { Public Static voidMain (string[] args) {//Map am = new HashMap ();Map am=NewLinkedhashmap (16,0.75f,true);//These parameters represent the turn on Access order function, then in the order of insertion, then in the sequence of accessAm.put (2, "2"); Am.put (1, "1"); Am.put (4, "4"); Am.put (3, "3"); Am.get (3); Am.get (2); //Am.get (1);Am.get (4); /*Iterator Iterator=am.entryset (). Iterator (); while (Iterator.hasnext ()) {map.entry entry= (map.entry) iterator.next (); System.out.println (Entry.getkey () + "--" +entry.getvalue ()); }*/ /*Iterator Iterator=am.keyset (). Iterator (); while (Iterator.hasnext ()) {Object key=iterator.next (); String Name=key; String value= (String) am.get (key); System.out.println (key+ "-to" +value); }*/ for(Iterator iterator=Am.keyset (). iterator (); Iterator.hasnext ();) { //It is worth noting here that you cannot use the Remove () put () get () operation, which will change the order of the dataObject in=Iterator.next (); System.out.println (in); } }}
Can a single example of map implementation do both lazy loading and synchronization at the same time
It doesn't seem.
Linkedhashmap adds a list of links on hashmap basis to maintain the order of data additions and accesses.
TreeMap is a different map implementation, TREEMAP has more powerful features than HashMap, but TreeMap has less performance than HashMap. TreeMap implements the SortedMap interface, which enables sorting. This sort is not the same as Linkedhashmap, TreeMap is based on the number of tuples, and Linkedhashmap is sorted based on the order in which data is added and accessed.
Let's take a look at the specific code used by TreeMap
Public classStudentImplementsComparable<student>{ PrivateString name; Private intscore; PublicStudent (String name,intscore) { This. name=name; This. score=score; } @Override Public intcompareTo (Student o) {//TODO auto-generated Method Stub if(o.score< This. Score) { return1; }Else if(o.score> This. Score) { return-1; } return0; } PublicString toString () {StringBuffer sb=NewStringBuffer (); Sb.append ("Name:"); Sb.append (name); Sb.append (" "); Sb.append ("Score"); Sb.append (score); returnsb.tostring (); } PublicString GetName () {returnname; } } ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.TreeMap; Public classStudentdetailinfo {Student s; PublicStudentdetailinfo (Student s) { This. s=s; } PublicString toString () {returnS.getname () + "' s detail information"; } Public Static voidMain (string[] args) {map map=NewTreeMap (); Student S1=NewStudent ("Li1", 45); Student S2=NewStudent ("Li2", 55); Student S3=NewStudent ("Li3", 65); Student S4=NewStudent ("Li4", 75); Studentdetailinfo SF1=NewStudentdetailinfo (S1); Studentdetailinfo SF2=Newstudentdetailinfo (S2); Studentdetailinfo SF3=NewStudentdetailinfo (S3); Studentdetailinfo SF4=NewStudentdetailinfo (S4); Map.put (S1, SF1); Map.put (S2,SF2); Map.put (S3,SF3); Map.put (S4,SF4); Map Map1= ((TreeMap) map). SubMap (S1,S4);//[S1,S4]Map map2= ((TreeMap) map). Headmap (S4);//<S4Map map3= ((TreeMap) map). Tailmap (S3);//>=S3 for(Iterator it =Map3.keyset (). iterator (); It.hasnext ();) {Student Student=(Student) it.next (); System.out.println (Map3.get (student)); } }}
Use Treemapde SubMap Headmap Tailmap to efficiently implement sort queries
Actually, Treehap is based on the internal implementation of the red-black tree, so treemap's efficiency is still very high.
In the actual development encountered the sort can consider treemap use
Implementing a set interface is actually a class that implements the set interface for the class that implements the map interface and does not add additional operations on top of the Colloection interface
HashSet and HashMap correspondence
Linkedhashset and Linkedhashmap Correspondence
TreeSet and TreeMap Correspondence
The corresponding output is also roughly the same
A collection of Java performance optimizations