Java Learning Lesson 43rd-Collection Framework Tools class (I)

Source: Internet
Author: User
Tags comparable shuffle

Collections: A tool class for the collection framework

The methods are static.

Sorting Method Demo

Import Java.util.arraylist;import java.util.collections;import Java.util.comparator;import Java.util.List;class Comparabyleng implements Comparator<string>{public int Compare (string O1, string o2) {int t = o1.length ()-O2.lengt h (); return T==0?o1.compareto (O2): t; }}public class Main {public static void Main (string[] args) {Colletyions_demo ();}    public static void Colletyions_demo () {list<string> li = new arraylist<string> (); Li.add ("ASD");  Li.add ("D"); Li.add ("efasd"); Li.add ("EFASD"); Li.add ("DSSSFD"); Li.add ("Xxxxd");//system.out.println (list);//collections.sort (list);//Sort//system.out.println (list);//sort: public static <t extends Comparable<? Super t>> void Sort (list<t> list)//analog sort//mysort (list);//system.out.println (list); Mysort (Li,new Comparabyleng ()); System.out.println (LI); Collections.sort (Li,new Comparabyleng ()); System.out.println (LI);} /*public static <t extends Comparable<? Super t>> void Mysort (list<t> List) {for (int i = 0; I < List.size ()-1; i++) {for (int j = i+1, J < List.size (); j + +) {if (List.get (i). CompareTo (List.get (j)) >0) {Collections.swap (list, I, J) ;//Tool class Exchange}}}}*/public static <T> void Mysort (list<t> li,comparator<? Super t> com) {for (int i = 0; I & Lt Li.size ()-1; i++) {for (int j = i+1, J < Li.size (); j + +) {if (Com.compare (Li.get (i), Li.get (j)) > 0) {collections.swap (Li, I, J) ;}}}}}


Two-point lookup, the best value method demo

Import Java.util.arraylist;import java.util.collections;import Java.util.comparator;import java.util.List;public Class Main {public static void Main (string[] args) {Colletyions_demo ();} public static void Colletyions_demo () {list<string> li = new arraylist<string> (); Li.add ("ASD");    Li.add ("D"); Li.add ("efasd");  Li.add ("EFASD"); Li.add ("DSSSFD"); Li.add ("Xxxxd"); Collections.sort (LI); System.out.println (li); int index = Collections.binarysearch (LI, "D"); SYSTEM.OUT.PRINTLN (index);//negative indicates not found//gets maximum value string maxstrstring = Collections.max (LI); System.out.println ("max =" +maxstrstring);//Gets the minimum value string minstrstring = Collections.min (LI); System.out.println ("min =" +minstrstring);//Take the longest string maxlenstring = Collections.max (li,new Comparabyleng ()); System.out.println (maxlenstring);}}

reversal, replacement method demo

Import Java.util.arraylist;import java.util.collections;import Java.util.list;import Java.util.TreeSet;public class Main {public static void main (string[] args) {Colletyions_demo ();} public static void Colletyions_demo () {//principle//treeset<string> ts = new treeset<string> (New comparator< String> () {//public int compare (string O1, String O2) {////return o2.compareto (O1);//}//});//Package treeset<string > ts = new treeset<string> (Collections.reverseorder ()), Ts.add ("ad"), Ts.add ("Xad"); Ts.add ("FDAGRRGD"); Ts.add ("a"); Ts.add ("Gedsad"); SYSTEM.OUT.PRINTLN (TS);//If there is a comparator, reversal//length reversal treeset<string> td = New Treeset<string> ( Collections.reverseorder (New Comparabyleng ())), Td.add ("ad"), Td.add ("Xad"); Td.add ("FDAGRRGD"); Td.add ("a"); Td.add ("Gedsad"); SYSTEM.OUT.PRINTLN (TD);//replaceall/reverse method List<string> al = new arraylist<string> (); Al.add ("ads"); Al.add ("DS"); Al.add ("s"); Collections.reverse (AL); System.out.println (AL); Collections.replaceall (AL, "s", "Y");//actually-&GT Set (IndexOf ("s"), "Y") System.out.println (AL);}}


Other methods
1、fill(List<? super T> list, T obj)
Replaces all elements in the specified list with the specified element.

Collections.fill (list, "ASD");


2、shuffle(List<?> list)
To displace a specified list using the default random source

Collections.shuffle (list); random

More ways to look at API documentation ...


To convert a non-synchronous collection to a synchronous collection

static
<T> Collection<T>
synchronizedCollection(Collection<T> c)
Returns the synchronization (thread-safe) collection that the specified collection supports.
static
<T> List<T>
synchronizedList(List<T> list)
Returns a list of synchronized (thread-safe) support for the specified list.
static
<K,V> Map<K,V>
synchronizedMap(Map<K,V> m)
Returns the synchronized (thread-safe) mappings supported by the specified mappings.
stat IC
<T> set<t>
synchronizedset (set<t> s)
Returns the synchronous (thread-safe) set supported by the specified set.
stat IC
<K,V> sortedmap<k,v>
synchronizedsortedmap (sortedmap<k,v> m)
Returns a synchronous (thread-safe) ordered map supported by the specified ordered map.
static
<T> SortedSet<T>
synchronizedSortedSet(SortedSet<T> s)
Returns a synchronous (thread-safe) ordered set supported by the specified ordered set.


Conversion principle:

List List = new ArrayList (); List = Mycollections.synlist (list);//Returns a synchronized ListClass mycollections{public list synlist (List list) {return new MyList ( list);} Private class MyList implements List{private List list;private static Final Object lock = new Object ();p ublic MyList () {su per ();//TODO auto-generated constructor stub}public MyList (List list) {super (); this.list = List;} Public boolean Add (Object obj) {synchronized (lock) {return list.add (obj);}} public boolean remove (Object obj) {synchronized (lock) {return list.remove (obj);}}}





Java Learning Lesson 43rd-Collection Framework Tools class (I)

Related Article

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.