Enhanced for Loop
After Java 1.5:
Used to iterate through collections and arrays (simplifying the operation of iterating through collections and arrays)
for (Element_type E:list_array) {
}
New loops are implemented in the form of iterators do not delete elements through a collection
for (String str:list) {
System.out.println (str);
List.remove (str);//error cannot be deleted
}
Package Day28;import Java.util.arraylist;import Java.util.collection;public class Demo01 {public static void main ( String[] args) {int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};for (int i:arr) {System.out.print (i + 1);} System.out.println (); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + 1);} System.out.println (); collection<integer> list = new arraylist<integer> (); List.add (1); List.add (2); List.add (3); List.add (4); List.add (5); List.add (6); List.add (7); List.add (8); List.add (9); for (int i:list) {System.out.print (i + 1);}}
The Sublist method for the List collection:
Gets a subset of the current collection
List sublist (int start,int end)
Modifications to subsets affect the original collection
Package Day28;import Java.util.arraylist;import Java.util.list;public class Demo02 {public static void main (string[] args) {list<integer> List = new arraylist<integer> (); for (int i = 0; i <; i++) {list.add (i);} SYSTEM.OUT.PRINTLN (list);//The base type cannot be used as a generic list<integer> sublist =list.sublist (3,9); System.out.println (sublist);//expands the elements of a subset by 10 times times for (int i = 0;i< sublist.size (); i++) {int sub = Sublist.get (i); sub = sub*10; Sublist.set (i,sub);} for (int i = 0;i<sublist.size (); i++) {Sublist.set (I,sublist.get (i) *10);} System.out.println (sublist); SYSTEM.OUT.PRINTLN (list);}}
Data:
It's all about saving data.
Queue queues are used to store a set of data, but there are certain requirements for the 糴 access method must be first in, out (FIFO) cannot be removed from the middle, so there is no query
Implementation of LinkedList in the common terms sub-class
Methods of the queue
Add (e) Add a meta-cable if the queue is full, throw a Iiiegaislabeepeplian exception
Boolean offer (E): Append an element append to the end of the queue successfully returns true if the queue is full, false
E poll () takes the element out of the first team (removes the element from the queue after it is removed) returns null if the queue is empty
E Peek () gets the first element of the team, but does not delete if the queue is empty, then NULL is returned
Remove removes and returns the element of the queue header if the queue is empty, the nosuchelementexception exception is thrown
Element returns the elements of the queue header if the queue is empty, a Nosuchelementexception exception is thrown
Put adds an element block if the queue is full
Take remove and return elements of the queue header if the queue is empty, it blocks
The traversal of a queue is a one-time
The second element in the queue can be obtained only if the element of the first team is removed
Package Day28;import Java.util.linkedlist;import Java.util.queue;public class Demo03 {public static void main (string[] args) {queue<string> Queue = new linkedlist<string> (), Queue.offer ("A"), Queue.offer ("B"), Queue.offer ("C" ); System.out.println (queue); System.out.println (Queue.peek ()); String str = null;while ((str = Queue.poll ()) = null) {System.out.print (str);}}
Stack
Deque is the sub-interface of the queue
Using a double-ended queue, only allow elements to go in one direction, stack structure, stack structure access elements to the advanced post-out (FILO) First Input last Output
Stack has a good history of traceability
LinkedList implements the Deque
Package Day28;import Java.util.deque;import Java.util.linkedlist;public class Demo04 {public static void main (string[] args) {deque<string> stack = new linkedlist<string> ();//Create Stack Stack.push ("A"),//Add Element Stack.push ("B") to the stack; Stack.push ("C"); Stack.push ("D"); SYSTEM.OUT.PRINTLN (stack);//Output Stack System.out.println (Stack.peek ());//Get stack first element, like Queue//system.out.println (Stack.pop ());//similar to queue while (stack.peek () = null) {//traversal stack to use PEEK to determine if there are elements in the top of the stack, call the Pop method to get the No side will have an exception System.out.print (Stack.pop ());}}
Collection sort
Comparable interface
When sorting an array or a collection element, first determine the comparison size, have the conclusion to be able to sort, how to compare size, need to implement this interface, and implement seven kinds of abstract methods to define the comparison rules
Arrays when an array is sorted, the CompareTo method of these elements is called when comparing each element size, and this method is the comparable interface-defined
Package day28;//which class implements this interface using this class as the generic public class point implements Comparable<point> {//implements this interface with a method private int x;private int y;public point (int x,int y) {this.x = X;this.y = y;} @Overridepublic String toString () {return x+ "," +Y;} /** * Return value does not care about the specific value, only the value range * >0 The current object is larger than the given object * <0 the current object is smaller than the given object * = 0 The current object and the given object are equal */public int compareTo (point P) {//define interface China A comparison rule between instances of a point of law this method equals the comparison rule between the/** * definition points: * points to the origin of the long distance of the large */int len = this.x * this.x + this.y*this.y;int Plen = p.x * p.x +p.y*p.y;return Len-plen;} /** * The return value of the CompareTo method should be consistent with the return value of the Equals method * when the Equals method of two objects returns True * The CompareTo method should return 0 */}
Collection is the parent interface of the collection
Tool class for collections collection
One way is to sort the collection
Many classes in Java implement the comparable interface
String integer and so on
They have defined the comparison rules themselves, but do not meet the requirements of sorting, you need to specify a comparison rule
Collections provides an overloaded sorting method
Sort (Collection C, Comparator com)
Comparator
Comparator interface
Comparators are used to define comparison rules
When the collections sort (Collection c,comparator com) method is used, the method does not apply the comparison rules of the elements themselves in the collection when sorting the collection, but instead uses the comparison rules in the comparator we provide to compare and then sort
package day28;import java.util.arraylist;import java.util.collections;import java.util.comparator;import java.util.list;public class demo06 {public static Void main (String[] args) {list<point> list = new arraylist<point> (); List.add (New point (3,4)); List.add (New point (7,8)); List.add (New point (9,0)); List.add (new Point (5,6)); List.add (New point); SYSTEM.OUT.PRINTLN (list); Collections.sort (list); SYSTEM.OUT.PRINTLN (list);//Sort Collection Java.util.collections list<string> strlist = new ArrayList<String> (); Strlist.add ("FA"); Strlist.add ("CD"); Strlist.add ("EF"); Strlist.add ("AB"); Strlist.add ("de"); Strlist.add ("BC"); System.out.println (strlist); Collections.sort (strlist); System.out.println (strlist); List<string> strlist2 = new arraylist<string> (); StrList2.add ("asdf"); Strlist2.add ("XCB"); Strlist2.add ("123 asfd "); StrLIst2.add ("r2r234"), Strlist2.add ("G&NBSP;CG"), Strlist2.add ("Asd sdf e"), Strlist2.add ("asdf "); System.out.println (STRLIST2); Collections.sort (Strlist2, new newcomparator ());//Use your own defined comparator to sort System.out.println (STRLIST2); List<string> strlist3 = new arraylist<string> (); StrList3.add ("asdf"); Strlist3.add ("XCB"), Strlist3.add ("123 asfd "), Strlist3.add ("r2r234"), Strlist3.add ("G&NBSP;CG"); Strlist3.add ("Asd sdf e"); Strlist3.add ("asdf "); Comparator<string> comparator = new comparator<string> () {//Use Anonymous inner class public Int compare (STRING&NBSP;S1,STRING&NBSP;S2) {return s1.length () - s2.length ();}}; System.out.println (STRLIST3); Collections.sort (Strlist3,comparator); System.out.println (STRLIST3);}} /** * usually creates a comparer temporarily with an anonymous inner class */class newcomparator implements comparator<string >{//Custom Comparator/** * comparison rule * when return value >0 s1 > s2 * when return value <0 s1 < s2 * when return value =0 s1 = s2 */public int compare (STRING&NBSP;S1,STRING&NBSP;S2) {return s1.length () - s2.length ();}}
Set Set
Do not repeat collections
Two elements not present in Set set equals True
The Reuse implementation class for set sets
HashSet: Set implemented using hash algorithm
Hashing algorithms, high-volume data
Hashcode ()
TreeSet: Set implemented with two-fork tree
Package Day28;import Java.util.arraylist;import Java.util.hashset;import java.util.list;import java.util.Set;public Class Demo07 {public static void main (string[] args) {list<string> List = new arraylist<string> (); set<string> set = new hashset<string> ();//generics are also supported, and lists are used to constrain the type of the collection String str = "abc"; for (int i = 0;i<5;i++) {List.add (str); Set.add (str);} SYSTEM.OUT.PRINTLN (list); System.out.println (List.size ()); SYSTEM.OUT.PRINTLN (set); System.out.println (Set.size ());//Not repeating collection}}
Package Day28;import Java.util.hashset;import Java.util.random;import Java.util.set;public class Demo08 {public static void Main (string[] args) {set<integer> Set = new hashset<integer> (); Random r = new random (), int count = 0;while (Set.size () <10) {Count++;set.add (R.nextint (10));} System.out.println (set+ "--" +count);}}
Set set is unordered so there is no get method
Package Day28;import Java.util.hashset;import Java.util.set;public class Demo9 {public static void main (string[] args) { set<string> set = new Hashset<string> (), Set.add ("1"), Set.add ("2"), Set.add ("3"), Set.add ("4"), Set.add ("5 "); SYSTEM.OUT.PRINTLN (set);//set collection is unordered}}
The set collection does not have a Get method
So
Elements can only be obtained by means of iterators
Package Day28;import Java.util.hashset;import Java.util.iterator;import Java.util.set;public class Demo09 {public static void Main (string[] args) {set<string> Set = new hashset<string> () Set.add ("1"); Set.add ("2"); Set.add ("3"); Set.add ("4"); Set.add ("5"); SYSTEM.OUT.PRINTLN (set);//set collection is unordered iterator<string> it = Set.iterator (); while (It.hasnext ()) {//Iterate set set String str = It.next (); System.out.print (str);} System.out.println (); for (String I:set) {//Iterate set set System.out.print (i);}}}
HashSet access elements need to rely on elements
The return value of the Hashcode () method is the method defined in object
Overriding method Note: Consistent with equals method = "When the Equals method of two objects is compared to true, the number returned by the Hashcode () method should be the same
Different objects hashcode () values to avoid the same = "rewrite hashcode () =" algorithm
Package Day28;import Java.util.hashset;import Java.util.set;public class Demo10 {public static void main (string[] args) { set<point> set = new hashset<point> (); Point P = new Point, Set.add (p); System.out.println (Set.size ()); Set.add (P); System.out.println (Set.size ());p. SetX (2);p. Sety (2); Set.add (P);//Modified hashcode is a System.out.println that can be placed two times with one object ( Set.size ());}}
This article from the "Romantic Smile" blog, reproduced please contact the author!
JAVA28: Collection Class 2