Java-collection class

Source: Internet
Author: User

I. Implementation of Common collection classes

 


1. ArrayDeque/shortlist: array and linked list of double-end queues


2. HashSet/Map: Hash


3. TreeSet/Map: red/black tree


In fact, TreeSet uses TreeMap internally. When a new element is added, an empty Object is put into the TreeMap as the value.


3.1 When the Comparable and Comparator compare methods are implemented, a positive number indicates that the object or parameter 1 is large.


Therefore, if the field value of the parameter 2 object is subtracted from the value of parameter 1, the reverse order is generated.
Suggestion: when compare () = 0, equals () = true


3.2 ClassCaseException is generated when a class that does not implement the Comparable interface is placed into the TreeSet.


3.3 If a custom Comparator is input when a TreeSet is constructed, the compare method of the Comparator is used for comparison.
For the source code, see the put Method of TreeMap.


[Java]
Public class TreeSetTest {
 
Public static void main (String [] args ){
 
TreeSet <Item> setById = new TreeSet <Item> ();

// 1. Exception if Item not implement Comparable interface
// Exception in thread "main" java. lang. ClassCastException: Item cannot be cast to java. lang. Comparable

SetById. add (new Item (1, "level 9 "));
SetById. add (new Item (2, "level 2 "));
SetById. add (new Item (3, "level 5 "));

System. out. println (setById );


// 2.Use comparator

TreeSet <Item> setByDesc = new TreeSet <Item> (
New Comparator <Item> (){
@ Override
Public int compare (Item o1, Item o2 ){
Return o1.desc. compareTo (o2.desc );
}
});
SetByDesc. addAll (setById );

System. out. println (setByDesc );
}
 
}
 
Class Item implements Comparable <Item> {

Int id;
String desc;

Item (int id, String desc ){
This. id = id;
This. desc = desc;
}
 
@ Override
Public int compareTo (Item o ){
Return id-o. id;
}
 
@ Override
Public String toString (){
Return "Item [id =" + id + ", desc =" + desc + "]";
}

}

Public class TreeSetTest {

Public static void main (String [] args ){

TreeSet <Item> setById = new TreeSet <Item> ();

// 1. Exception if Item not implement Comparable interface
// Exception in thread "main" java. lang. ClassCastException: Item cannot be cast to java. lang. Comparable

SetById. add (new Item (1, "level 9 "));
SetById. add (new Item (2, "level 2 "));
SetById. add (new Item (3, "level 5 "));

System. out. println (setById );


// 2.Use comparator

TreeSet <Item> setByDesc = new TreeSet <Item> (
New Comparator <Item> (){
@ Override
Public int compare (Item o1, Item o2 ){
Return o1.desc. compareTo (o2.desc );
}
});
SetByDesc. addAll (setById );

System. out. println (setByDesc );
}

}

Class Item implements Comparable <Item> {
 
Int id;
String desc;
 
Item (int id, String desc ){
This. id = id;
This. desc = desc;
}

@ Override
Public int compareTo (Item o ){
Return id-o. id;
}

@ Override
Public String toString (){
Return "Item [id =" + id + ", desc =" + desc + "]";
}
 
}

4. ProrityQueue: priority queue (HEAP)


5. LinkedHashSet/Map: Record insertion sequence (LRU)


[Java]
Public class LRUTest {
 
Public static void main (String [] args ){
Map <Item, Integer> lruMap = new LinkedHashMap <Item, Integer> (){
Private static final long serialVersionUID = 1L;
 
@ Override
Protected boolean removeEldestEntry (Map. Entry <Item, Integer> eldest ){
Return size ()> 3;
}
};
LruMap. put (new Item (1, "111"), 1 );
LruMap. put (new Item (2, "222"), 2 );
LruMap. put (new Item (3, "333"), 3 );

LruMap. put (new Item (4, "444"), 4 );
System. out. println (lruMap );

LruMap. put (new Item (5, "555"), 5 );
System. out. println (lruMap );
}
 
}

Public class LRUTest {

Public static void main (String [] args ){
Map <Item, Integer> lruMap = new LinkedHashMap <Item, Integer> (){
Private static final long serialVersionUID = 1L;

@ Override
Protected boolean removeEldestEntry (Map. Entry <Item, Integer> eldest ){
Return size ()> 3;
}
};
LruMap. put (new Item (1, "111"), 1 );
LruMap. put (new Item (2, "222"), 2 );
LruMap. put (new Item (3, "333"), 3 );

LruMap. put (new Item (4, "444"), 4 );
System. out. println (lruMap );

LruMap. put (new Item (5, "555"), 5 );
System. out. println (lruMap );
}

}

 

Ii. Common algorithm Encapsulation


1. Sorting Algorithm


When sorting a collection class, convert it into an array, and then sort it by means of the Arrays. sort method.
All the algorithms mentioned above know that quick sorting requires less extra space and faster execution. The reason why merge is selected here.
Sorting is because it is stable and does not break the relative order between the same elements.


The source code is as follows:

 

 


2. Search Algorithms


For the data structure that implements RandomAccess, that is, the elements can be located in O (1) Time,
Then, the search algorithm uses binary search for direct subscript positioning and finds the elements to be searched in log (N) time.
Otherwise, an iterator will be used to move forward and backward to locate the position after the binary, achieving an inefficient binary.
Search.


The source code is as follows:

 

 


IndexedBinarySearch and iteratorBinarySearch are two search algorithms. The
The only difference is the location of the positioning element. In the indexedBinarySearch method, you can directly obtain it through list. get,
IteratorBinarySearch is obtained by constantly moving the iterator on the list.


The inheritance level of the RandomAccess interface:

 

 


Therefore, for ArrayList, you can locate the element directly by subscript and perform binary search.
For the consumer list linked list, we can only locate it through the iterator. Obviously
It is impossible to locate any element in the linked list within O (1) time.


Note that for non-RandomAccess data structures whose size is less than BINARYSEARCH_THRESHOLD (5000,
The indexedBinarySearch policy is also used. That is to say, when the length is less than 5000, It is not through the iterator but every time
The efficiency of all elements from the beginning to the end is acceptable.

Author: dc_726
 

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.