This article includes:
List advanced
SubList Method
Queue interface and Queue List Implementation
Deque interface and consumer List Implementation
Stack operations
List Algorithm
Comparable Interface
Collections. sort Method
Comparator callback
I. List advanced
1. subList Method
The subList method of List is used to obtain the subList.
Note: The List obtained by subList occupies the same storage space as the original List. operations on the subList will affect the original List.
List <E> subList (int fromIndex, int toIndex); // included in front of [...}, not included in the end.
Example:
List <Integer> list = new Array <Integer> (); for (int I = 0; I <10; I ++) {list. add (I);} System. out. println (list); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] List <Integer> subList = list. subList (3, 8); System. out. println (subList); // [3, 4, 5, 6, 7] for (int I = 0; I <subList. size (); I ++) {subList. set (I, subList. get (I) * 10); // The Sub-List is increased by ten times.} // the original List is also increased by ten times. System. out. println (list); // [, 60, 70,] System. out. println (subList); // [30, 40, 50, 60, 70]
2. Queue interface and Queue List Implementation
Queue (Queue) is a common data structure. It can be considered as a special linear table. The Queue limits the access to the linear table: only the offline element can be added from one end of the linear table, extract the poll element from the other end.
The queue follows the first-in-first-out principle.
JDK provides the Queue interface. In the same way, the Queue List implements the function of selecting the Queue list to implement the Queue, because the Queue usually needs to be inserted and deleted, while the Queue list is more efficient in this regard ).
The main methods in the 6th and Queue interfaces are:
Boolean offer (E e): adds an object to the end of the team. If the addition is successful, true is returned.
E poll (): deletes an element from the beginning of the queue and returns an element.
E peek (): returns the first element but does not delete it)
Example:
// Create a queue
Queue <String> queue = new Queue list <String> ();
// Append an element to the end of the team
Queue. offer ("");
Queue. offer ("B ");
Queue. offer ("C ");
// Obtain the element from the beginning of the team
String str = queue. poll (); // gets the first element of the team.
// Traverse
While (str = queue. poll ())! = Null ){
System. out. println (str );
}
// Note: after obtaining the information, it will be deleted from the queue.
// Because the length is unknown, when the queue has no element, the flag is: poll method returns null.
3. Deque interface and consumer List Implementation
Deque is a sub-interface of Queue. It defines the so-called "dual-end Queue", that is, the two ends of the Queue can respectively enter the offline Queue) and the outbound poll), and the sort List implements this interface.
If you limit Deque to only one end, you can implement the data structure of "stack" stack). For stacks, the data structure is push and the data structure is pop.
Stack follows the principle of advanced and backward release.
4. Basic stack operations
Method:
Push (): push to stack
Pop (): gets the value from the stack)
// Create a stack
Deque <Character> stack = new Character List <Character> ();
String sr = "ABCDEFGHI ";
For (int I = 0; I <sr. length (); I ++ ){
Stack. push (sr. charAt (I); // inbound stack
}
System. out. println (stack); // [I, H, G, F, E, D, C, B, A]
/**
* Use the pop () method to return to the first element of the stack. If no element exists, the pop () method will throw NoSuchElementException.
* Stack also supports the peek () method to obtain the first element. If the return value is null, no element exists.
*/
// Traverse the stack
While (stack. peek ()! = Null ){
Char c = stack. pop ();
System. out. print (c + "");
}
Ii. List Algorithm
1. Comparable Interface
An abstract method is defined to define comparison rules between objects.
Therefore, its subclasses are comparable and can be sorted in the set.
Class Point1 implements Comparable <Point1> {
Int x;
Int y;
Public Point1 (int x, int y ){
Super ();
This. x = x;
This. y = y;
}
Public int compareTo (Point1 o ){
// Compare the two vertices.
Int myLen = x * x + y * y;
Int otherLen = o. x * o. x + o. y * o. y;
/**
* The value range of the returned value determines the size.
* If return> 0 => this> o
* If return <0 => this <o
* If return = 0 => this = o
*/
Return myLen-otherLen;
}
}
2. Collections. sort Method
The static method provides some practical operations on the set. The most used method is the sort method that sorts the List.
Collections's sort method requires objects in the set to implement the Comparable interface, so that the compareTo method can be called to determine the object size. Otherwise, the sort method cannot determine which object is large and which object is small.
Public class Demo3_Comparable {public static void main (String [] args) {List <Point1> list = new ArrayList <Point1> (); list. add (new Point1 (1, 28); list. add (new Point1 (1, 4); list. add (new Point1 (7,2); System. out. println (list);/*** collection tool Collections ** sort compares the elements in the Set, and only compares the returned results of the compareTo method of the element. * Therefore, the element should be a subclass of Comparable */Collections. sort (list); // you cannot use the attributes of the generic type to compare them, because the types of the generic type are uncertain and the comparison rules are different. System. out. println (list) ;}// Comparable also supports generic class Point1 implements Comparable <Point1> {int x; int y; public Point1 (int x, int y) {super (); this. x = x; this. y = y;} public int compareTo (Point1 o) {// compare the larger int myLen = x * x + y * y; int otherLen = o. x * o. x + o. y * o. y; /*** the value range of the returned value determines the size. * if return> 0 => this> o * if return <0 => this <o * if return = 0 => this = o */return myLen-otherLen ;} public String toString () {return "(" + this. x + "," + this. y + ")";}}
3. Comparator callback
Once the Java class implements Comparable, its comparison logic is determined. If you want to temporarily "specify comparison rules" in the sorting operation, you can use the Comparator interface callback method.
Collections has a natural sorting overload method.
Sort (Collection c, Comparator com); // Anonymous class
Comparator is an interface that is used to define comparison Rules separately.
Example:
List <String> list = new ArrayList <String> (); list. add ("Java"); list. add ("PHP"); list. add ("Net"); list. add ("C ++"); System. out. println (list); // [Java, PHP, Net, C ++]/*** the String class has implemented the compareTo method of the Comparable interface by default, * It is also stipulated that letters are used for comparison. */Collections. sort (list); System. out. println (list); // [C ++, Java, Net, PHP]/*** however, we now require that this comparison method not meet our needs, * The Comparator interface callback method is used. Comparison rules are defined separately. */Collections. sort (list, new Comparator <String> () {@ Override public int compare (String o1, String o2) {return o1.length ()-o2.length (); // compare by character length}); System. out. println (list); // [C ++, Net, PHP, Java]
This article from the "beautiful life needs to carefully record" blog, please be sure to keep this http://huing.blog.51cto.com/1669631/1295015