A Collection framwork Diagram
Two important interface class parsing
1 Collection (element unordered)
Arrays have good random access performance, but the elements in an array are of a single type, once they are declared with a fixed space size. Collection compensates for the non-expandable array space, which can be added to any type of object, and space can be freely allocated. The classic proverb "collection is like a basket, any type of object can be put in, and the elements can be repeated."
2 List (element ordered)
1 ArrayList and LinkedList
list is the classic sub-interface of collection, often used for storage operations, while ArrayList is most commonly used to implement the list interface. Elements are added to the end of the list in order, and ArrayList is useful for traversing elements frequently. LinkedList is also a list interface implementation class, which is similar to a linked list, and is suitable for large-scale element deletion operations
2 ArrayList and Vector differences
ArrayList for thread sync, vector synchronization for thread, multi-select ArrayList in non-multithreaded environment
3 Set (element not duplicated)
1 TreeSet
1 built-in integer and string types are sorted by natural order and dictionary order, and custom object type ordering requires implementation of the comparable interface
Packagecom.fjnu.test;ImportJava.util.Iterator;ImportJava.util.TreeSet; Public classTester { Public Static voidMain (string[] args) {//TODO auto-generated Method StubTreeSet treeset=NewTreeSet (); Treeset.add ("Hello World"); Treeset.add ("Welcom to Here"); Treeset.add ("I like Java"); Iterator Iterator=Treeset.iterator (); while(Iterator.hasnext ()) System.out.println (Iterator.next ()); }}
Output Result:
Hello World
I like Java
Welcom to here (now sorted in dictionary order)
2 TreeSet rely on treemap for internal sorting (see object comparisons and Sorting)
2 HashSet (preferred Set implementation class with performance better than other set implementations)
4Queue (support element FIFO principle to sort)
Packagecom.fjnu.test;ImportJava.util.Iterator;Importjava.util.LinkedList;ImportJava.util.Queue; Public classTester { Public Static voidMain (string[] args) {//TODO auto-generated Method StubQueue queue =NewLinkedList (); Queue.add ("I am a programmer!"); Queue.add ("Java is a computer programming language!"); Queue.add ("Java is easy to learn!"); Iterator Iterator=Queue.iterator (); while(Iterator.hasnext ()) System.out.println (Iterator.next ()); }}
Output Result:
I am a programmer!
Java is a computer programming language!
Java is easy to learn!
5Map (key/value form Storage)
1TreeMap (element ordered)
2HASHMAP (Common implementation class for map interfaces)
Map is where the key/value pairs are stored, the map does not contain duplicate keys, and each key maps at most one value
Note: Neither the key nor the value in the map can be a basic type
Comparison and sorting of three objects
Small case:
A display manufacturing plant specialized in the manufacture of various LCD display display class properties:
1. Screen Size (14#,15#,17#,19#,23#) (size)
2. Screen resolution (1024*768,1280*1024,1366*768,1920*1080) (resolution)
3. Whether qualified (qualified, unqualified) (qualified)
Randomly generates 20 LCD TVs (but not identical) (TreeSet satisfies this condition)
Output requirements:
1. According to the qualification rate row (qualified Platoon head, unqualified platoon rear)
2. Screen size (from large to small)
3. Screen resolution (from low resolution to high resolution)
Method one implements the comparable interface (overriding the CompareTo () method)
PackageEdu.fjnu.Collection.TreeSet.Domain;ImportJava.util.Random;//display entities for encapsulating properties such as screen size resolution Public classDisplayerImplementsComparable<displayer> { Private Final StaticString sizes[] = {"14#", "15#", "17#", "19#", "23#", "25#", "27#", "29#"};//Screen Size Private Final StaticString resolutions[] = {"1024x768", "1280*1024", "1366*768", "1920*1080", "2050*1080"};//Screen Resolution PrivateRandom r =NewRandom ();//whether qualified PrivateString size; PrivateBoolean qulified; PrivateString resolution; //randomly generated objects PublicDisplayer () { This. Size =Sizes[r.nextint (sizes.length)]; This. Resolution =Resolutions[r.nextint (resolutions.length)]; This. qulified =R.nextboolean (); } @Override PublicString toString () {return This. size + "\ T" + This. resolution + "\ T" + ( This. qulified? "Qualified": "Unqualified"); } PublicRandom Getr () {returnR; } Public voidSetr (Random r) { This. R =R; } PublicString GetSize () {returnsize; } Public voidsetSize (String size) { This. Size =size; } PublicBoolean getqulified () {returnqulified; } Public voidsetqulified (Boolean qulified) { This. qulified =qulified; } PublicString getresolution () {returnresolution; } Public voidSetresolution (String resolution) { This. Resolution =resolution; } //Customizing the Displayer properties PublicDisplayer (Random r, string size, Boolean qulified, string resolution) { This. R =R; This. Size =size; This. qulified =qulified; This. Resolution =resolution; } @Override Public intCompareTo (Displayer other) {if( ! (Other.getqulified (). Equals ( This. qulified)))//The passing rate is different, according to the qualified first shot return This. Qulified.compareto (Other.getqulified ()) *-1; Else if(! ( This. Size.equals (Other.getsize ())))//The same rate, by size from big to small row return This. Size.compareto (Other.getsize ()) *-1; Else if(! (Other.getresolution (). Equals ( This. Resolution)))//screen resolution varies by big to small row return This. Getresolution (). CompareTo (other.resolution); return0; }} Packageedu.fjnu.Collection.TreeSet.test;ImportJava.util.TreeSet;ImportEdu.fjnu.Collection.TreeSet.Domain.Displayer; Public classDisplayertester { Public Static voidMain (string[] args) {//TODO auto-generated Method StubTreeset<displayer> treeset=NewTreeset<displayer>(); intI=0; intCount=0; while(i<20) {Treeset.add (NewDisplayer ()); I++; } System.out.println ("Screen size \ t screen resolution \ T is qualified"); for(Displayer displayer:treeset) {System.out.println (displayer); } }}
Output results
Method two-Custom comparator comparator
Public Static classDisplayercompatorImplementscomparator<displayer2> {//Custom Comparators@Override Public intCompare (Displayer2 displayer1, Displayer2 displayer2) {//Customizing the way you take notes if(!displayer1.getqulified (). Equals (Displayer2.getqulified ()))returndisplayer1.getqulified (). CompareTo (Displayer2.getqulified ())*-1; Else if(!displayer1.getsize (). Equals (Displayer2.getsize ()))returnDisplayer1.getsize (). CompareTo (Displayer2.getsize ()) *-1; Else if(!displayer1.getresolution (). Equals (Displayer2.getresolution ()))returndisplayer1.getresolution (). CompareTo (Displayer2.getresolution ()); return0; } }
Packageedu.fjnu.Collection.TreeSet.test;ImportJava.util.Iterator;ImportJava.util.TreeSet;ImportEdu.fjnu.Collection.TreeSet.Domain.Displayer;ImportEdu.fjnu.Collection.TreeSet.Domain.Displayer2; Public classDisplayertester { Public Static voidMain (string[] args) {//TODO auto-generated Method StubTreeSet TreeSet =NewTreeSet (NewDisplayer2.displayercompator ());//To Pass in a custom comparer in a construction method inti = 0; while(I < 20) {Treeset.add (NewDisplayer2 ()); I++; } System.out.println ("Screen size \ t screen resolution \ T is qualified"); Iterator Iterator=Treeset.iterator (); while(Iterator.hasnext ())//Iterating through elements in TreeSet with iteratorsSystem.out.println (Iterator.next ()); }}
Output Result:
Output meets the requirements of the problem
Note:
The Java Collection Framework provides programmers with the convenience of manipulating storage objects by choosing the most appropriate type of storage, depending on the characteristics of the different collection types in the development process. Due to the technical details of the collection framework, this article only deals with the general usage of collection, and the details of the problem can be realized after practice.
A brief analysis of set frame