I. Collections introduction in the application development of the collection, several interfaces and several subclasses of the set are the most commonly used, but a set of tool class--collections is provided in the JDK, which can be set up directly by this kind of convenient operation.common methods and constants of collections class
No. |
Method |
Type |
Describe |
1 |
public static final List empty_list |
Constant |
Returns an empty list collection |
2 |
public static final Set Empty_set |
Constant |
Returns an empty set collection |
3 |
public static final Map Empty_map |
Constant |
Returns an empty map collection |
4 |
public static <T> Boolean addall (collection<? super t> C, T ... a) |
Ordinary |
Add content to a collection |
5 |
public static <t extends Object & comparable<? Super t>> T Max (collection<? extends t> Coll) |
Ordinary |
Find the largest content, sort by comparer returns the largest element of a given set based on the natural order of the elements |
6 |
public static <t extends Object & comparable<? Super t>> T min (collection<? extends t> Coll) |
Ordinary |
Finds the smallest content in the collection, sorted by comparer returns the smallest element of a given set based on the natural order of the elements |
7 |
public static <T> Boolean replaceall (list<t> list,t oldval,t newval) |
Ordinary |
Replaces the specified contents of a collection with new content replaces all occurrences of a specified value in the collection with another value |
8 |
public static void reverse (list<?> List) |
Ordinary |
Reverses the order of the elements in the specified list collection. |
9 |
public static <T> int BinarySearch (list<? extends Comparable<? Super t>> List,t Key) |
Ordinary |
Finds the specified content in the collection Finds the specified list collection using the binary lookup algorithm to obtain a reference to the The index position of the fixed object. |
10 |
public static final <T> list<t> emptylist () |
Ordinary |
Returns an empty list collection |
11 |
public static final <K,V> map<k,v> Emptymap () |
Ordinary |
Returns an empty map collection |
12 |
public static final <T> set<t> Emptyset () |
Ordinary |
Returns an empty set collection |
13 |
public static <t extends Comparable<? Super t>> void Sort (list<t> List) |
Ordinary |
Set sort operation, sorted according to comparable interface The specified list collection is ascending according to the natural order of the elements The order is ordered. |
14 |
public static void Swap (list<?> list,int i,int J) |
Ordinary |
Swaps an element at a specified position |
Invert the order of elements in the specified list collection
PackageCom.pb.demo2;Importjava.util.Collections;Importjava.util.LinkedList;Importjava.util.List; Public classCollectionsTest1 { Public Static voidMain (string[] args) {//declaring a collection and adding elementslinkedlist<string> list =NewLinkedlist<string>(); List.add ("One"); List.add ("Both"); List.add ("Three"); System.out.println ("======= Normal traversal ============"); for(String s:list) {System.out.print (s+ "\ T"); } System.out.println (); System.out.println ("======= Execution Reverse method ============"); Collections.reverse (list); for(String s:list) {System.out.print (s+ "\ T"); } }}
The function of the sort method is to sort the specified list collection in ascending order
PackageCom.pb.demo2;Importjava.util.ArrayList;Importjava.util.Collections;Importjava.util.List; Public classCollectionsTest2 { Public Static voidMain (string[] args) {//declaring collections and adding setslist<integer> list =NewArraylist<integer>(); List.add (7); List.add (2); List.add (5); List.add (3); List.add (1); System.out.println ("======= Normal traversal ============"); for(Integer i:list) {System.out.print (i+ "\ T"); } System.out.println ("\n======= performs the Sort method ============"); Collections.sort (list); for(Integer i:list) {System.out.print (i+ "\ T"); } System.out.println ("\n======= Execution Reverse method ============"); Collections.reverse (list); for(Integer i:list) {System.out.print (i+ "\ T"); } }}
Results:
======= Normal traversal ============7 2 5 3 1 ======= Perform sort method ============1 2 3 5 7 ======= Execute Reverse method ============7 5 3 2 1
V. Methods for finding and replacing collections
PackageCom.pb.demo2;Importjava.util.ArrayList;Importjava.util.Collections;Importjava.util.List; Public classCollectionsTest3 { Public Static voidMain (string[] args) {//declaring collections and adding sets//Create a collectionlist<string> list =NewArraylist<string>(); //10 + different words addedList.add ("This"); List.add ("Is"); List.add ("Collection"); List.add ("Test"); List.add ("and"); List.add ("We"); List.add ("Can"); List.add ("Learn"); List.add ("How"); List.add ("To"); System.out.println ("======= Normal traversal ============"); for(String s:list) {System.out.print (s+ "\ T"); } //prints the largest and smallest elements in the output collectionSystem.out.println ("\n======== Maximum and minimum elements ========= in the print output collection"); String Max=Collections.max (list); String min=collections.min (list); System.out.println ("Maximum value in the list collection:" +max); System.out.println ("Minimum value in the list collection:" +min); System.out.println ("\n======= performs the Sort method ============"); Collections.sort (list); for(String s:list) {System.out.print (s+ "\ T"); } //BinarySearch is used if you want to sort before you can use theSystem.out.println ("\n=============binarysearch find index ================ based on content"); System.out.println ("We index in list is:" +collections.binarysearch (List, "we"))); System.out.println ("=============replaceall Method ================"); //ReplaceAllCollections.replaceall (List, "we", "our"); for(String s:list) {System.out.print (s+ "\ T"); } //SwapSystem.out.println ("======swap========"); Collections.swap (list,0, 9); for(String s:list) {System.out.print (s+ "\ T"); } }}
Results:
======= Normal traverse ============This is collection test and we can learn How to ======== the largest and smallest elements in the print output collection ========= The maximum value in the list collection: The minimum value in the Welist collection: and ======= Perform the sort method ============and can collection how are learn test this To we =============binarysearch search index based on content ================We index in list is:9=== ==========replaceall method ================and can collection how is learn test This To our ======swap======== We can collection how are Learn test This To and
Vi. Generating Collections using Arrays.aslist
PackageCom.pb.demo2;Importjava.util.Arrays;Importjava.util.List; Public classTest1 { Public Static voidMain (string[] args) {String str= "A,b,c,d,e,f"; List<String> list1=arrays.aslist (str); System.out.println ("Length of List1:" +list1.size ()); for(String s:list1) {System.out.print (s+ "\ T"); } System.out.println ("\ nthe collection is separated by commas"); List<String> list2=arrays.aslist (Str.split (",")); System.out.println ("Length of List2:" +list2.size ()); for(String s:list2) {System.out.print (s+ "\ T"); } }}
Results:
Length of List1: 1a,b,c,d,e,f The length of the collection List2 when separated by commas :6a b c d e f
Java learns from zero 24 points (Collection tool class collections)