------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! ------
Collections
I. Overview
Collections is a tool class for the collection framework. The methods inside it are static and do not need to create objects. Unique data is not encapsulated.
Most of the methods in the Collections tool class are used to manipulate the list collection, such as comparisons, binary lookups, random sorting, and so on .
Second, common operation
1. Find
T Max (collection< extends t> coll); Gets the largest element in the Coll collection based on the natural order of the collection
T Max (collection< extends t> coll,comparator<? super t> comp); gets the largest element in the Coll collection based on the order of the specified comparator comp
int BinarySearch (LSIT<? extends Comparable<? super t>> List,tkey);//dichotomy search for the specified object in the list collection
2. Replace
void Fill (list< Super t> List, T obj);// Replace all elements in the list collection with the specified object, obj
Boolean ReplaceAll (list<t> lsit,t oldval,t newval);// replace newval values in the collection with Oldval
void Swap (Listlist,int i,int j);/swap elements at a specified position in the specified list
3 reviews sorted by
void Shuffle (list<?> list);// random ordering of elements in the list collection using default random sources
void sort (lsit<t> list);// sort elements in the list collection according to natural order
void sort (list<t> lsit,comparator<? super t> C);// sort the List collection according to the ordering of the specified comparer c
4. Reversal
Reverse (list<?> list);//reverses the order of the elements in the list collection
Comparator Reverseorder ();//Returns a comparer that forcibly reverses the natural order of the objects that implement the comparable interface
Comparatorreverseorder (comparator<t> CMP);//Returns a comparer that forcibly reverses the order of the specified comparer
PackageAtheima;ImportJava.util.*; Public classCollectionsDemo1 { Public Static voidMain (string[] args) {Orderdemo (); } Public Static voidOrderdemo () {//Collections.reverseorder () can forcibly reverse the length of the settreeset<string>list=NewTreeset<string> (Collections.reverseorder (NewStrlcomparator ()));//Comparison lengthList.add ("NN"); List.add ("NZ"); List.add ("Nnffd"); List.add ("Sdgnn"); List.add ("AVBNN"); SOP (list);//forced reversal of the length of the set [Sdgnn, NZ, nnffd, nn, AVBNN]---"Length reversal: [Sdgnn, Nnffd, AVBNN, NZ, nn] } Public Static voidsop (Object obj) {System.out.println (obj); }}//Comparison lengthclassStrlcomparatorImplementsComparator<string>{@Override Public intCompare (String O1, String O2) {if(O1.length () >o2.length ())return1; if(O1.length () <o2.length ())return-1; returnO1.compareto (O2); } }
5. Synchronized Collections
List<t>synchronizedlist (list<t> list);//Returns a supported synchronous (thread-safe)List collection
Map<k,v>synchronizedlist (map<k,v> m);//Returns a supported synchronous (thread-safe)Map collection
PackageAtheima;ImportJava.util.*; Public classCollectionsdemo { Public Static voidMain (string[] args) {//Sortdemo (); //Maxdemo (); //Binarysearchdemo ();Filldemo (); } Public Static voidFilldemo () {List<string>list=NewArraylist<string>(); List.add ("NN"); List.add ("NZ"); List.add ("Nnffd"); List.add ("Sdgnn"); List.add ("AVBNN"); SOP (list); Collections.fill (list,"NZ"); SOP (list);//[NZ, NZ, NZ, NZ, NZ] } //Binary Method Search Public Static voidBinarysearchdemo () {List<string>list=NewArraylist<string>(); List.add ("NN"); List.add ("NZ"); List.add ("Nnffd"); List.add ("Sdgnn"); List.add ("AVBNN"); Collections.sort (list);//Natural SortSOP (list); intIndex=collections.binarysearch (list, "NZ"); SOP ("index=" +index);//index=3 } //take the maximum element Public Static voidMaxdemo () {List<string>list=NewArraylist<string>(); List.add ("NN"); List.add ("NZ"); List.add ("Nnffd"); List.add ("Sdgnn"); List.add ("AVBNN"); SOP (list); Collections.sort (list);//Natural SortSOP (list); String Max=collections.max (list);//take the maximum elementSOP ("max=" +max); } Public Static voidSortdemo () {List<string>list=NewArraylist<string>(); List.add ("NN"); List.add ("NZ"); List.add ("Nnffd"); List.add ("Sdgnn"); List.add ("AVBNN"); SOP (list); //Collections.sort (list);//Sort AscendingCollections.sort (list,NewStrcomparator ());//Sort AscendingSOP (list);//[Avbnn, nn, nnffd, SDGNN] } Public Static voidsop (Object obj) {System.out.println (obj); }}classStrcomparatorImplementsComparator<string>{@Override Public intCompare (String O1, String O2) {if(O1.length () >o2.length ())return1; if(O1.length () <o2.length ())return-1; returnO1.compareto (O2); } }
Iii. The difference between collections and collection
Collection is a top-level interface in the collection framework, which defines the common method of a single-column collection.
It has two commonly used sub-interfaces:
List: There is a defined index on the element. Orderly. You can repeat elements.
Set: The element cannot be duplicated. Disordered
Collections is a tool class in the collection framework. The methods in this class are static. There are methods available that can sort the list collection, binary lookup, and so on
Commonly used collections are threads that are not secure. Because of the need to improve efficiency. If you are working with these collections in a multithreaded manner, you can convert the thread's unsafe collection to a security by using the synchronization method in the tool class.
Arrays
I. Overview
Arrays is a tool class for manipulating arrays. The method inside is also all static. You do not need to create an object.
The benefit of turning an array into a list collection: You can manipulate the elements in the array using the ideas and methods of the collection. such as:contains,get,indexOf,sublist and other methods.
Ii. Common methods
1.lsit<t> aslist (T ... a);//Convert an array to a collection
Attention:
A, convert the array to a collection, do not use the collection of add and subtract methods, because the length of the array is fixed. If additions and deletions are made, a compilation exception is generated for Unsupportedoperationexception (operation exception is not supported).
b, if the elements in the array are objects, then the elements in the array are converted directly to the elements in the collection when they become collections.
c, if the elements in the array are basic data types, the array will be present as an element in the collection.
2,BinarySearch (): Binary lookup method,fill (): Replacement method,sort (): Sorting method, etc.
Features: An array of elements can be manipulated to accept a variety of basic data types other than Boolean and an ordered list of reference types of parameters, but also the scope of the specified element, and can be based on the specified comparer for the corresponding operation.
such as:sort (t[]a,comparator<? super t> C)
Fill (Int[]a,int from,int to), etc.
3. String toString ();//You can receive various array type parameters and return the string representation of the contents of the specified array.
Dark Horse Programmer---java Foundation---Collection Framework tool classes: collections and Arrays