Java Collections Tool Class

Source: Internet
Author: User
Tags shuffle

1.reverse Inversion
2.shuffle Random Sorting
3.sort Natural Sort
4.sort specifying the Comparer sort
5.swap swap the elements of the subscript position for X and Y
6.max Maximum Value
7.min Minimum value
8.frequency number of calculated elements
9.copy Copy List
10.replaceAll Replace all elements

ImportJava.util.*;/*** Created by Meicai on 2017/11/6.*/ Public classtestcollections { Public Static  voidMain (string[] args) {test10 (); }    /*Reverse Reverse 123*/    Private Static voidtest1 () {List<Integer> numlist= arrays.aslist (123,22,85,44);        Collections.reverse (numlist);    Numlist.foreach (System.out::p rintln); }    /*** Shuffle random sort *22-123*/    Private Static voidtest2 () {List<Integer> numlist= arrays.aslist (123,22,85,44);        Collections.shuffle (numlist);    Numlist.foreach (System.out::p rintln); }    /**3.sort Natural Sort * 123*/    Private Static voidtest3 () {List<Integer> numlist= arrays.aslist (123,22,85,44);        Collections.sort (numlist);    Numlist.foreach (System.out::p rintln); }    /*** Sort Specifies the comparator sort, using anonymous inner class * 123*/    Private Static voidTest4_1 () {List<Integer> numlist= arrays.aslist (123,22,85,44); Collections.sort (Numlist,NewComparator<integer>() {@Override Public intCompare (integer O1, integer o2) {return-O1.compareto (O2);                }                }                );    Numlist.foreach (System.out::p rintln); }    /*** Sort Specifies the comparator sort, using the java8 lambda expression * 123*/    Private Static voidTest4_2 () {List<Integer> numlist= arrays.aslist (123,22,85,44); Collections.sort (Numlist, (T1,T2)->{return-T1.compareto (t2);});    Numlist.foreach (System.out::p rintln); }    /*** Swap swaps the elements with the subscript position x and Y * 123*/    Private Static voidTest5 () {List<Integer> numlist= arrays.aslist (123,22,85,44); Collections.swap (Numlist,1,3);    Numlist.foreach (System.out::p rintln); }    /*** Max * max:123*/    Private Static voidTest6 () {List<Integer> numlist= arrays.aslist (123,22,85,44); Integer Max=Collections.max (numlist); System.out.println ("Max:" +max); }    /*** Frequency Returns the number of each element in the collection *count:2*/    Private Static voidtest7 () {List<Integer> numlist= arrays.aslist (123,22,85,44,22); intCount= Collections.frequency (numlist,22); System.out.println ("Count:" +count); }    /*** Copy exception, error usage *exception in thread ' main ' Java.lang.IndexOutOfBoundsException:Source does not fit in dest */    Private Static voidTest8 () {List<Integer> numlist= arrays.aslist (123,22,85,44,22); List<Integer> numlistcopy=NewArraylist<>();        Collections.copy (numlistcopy,numlist);    Numlistcopy.foreach (System.out::p rintln); }    /**copy Correct use method * 123*/    Private Static voidTest9 () {List<Integer> numlist= arrays.aslist (123,22,85,44,22); List<Integer> Numlistcopy=arrays.aslist (Newinteger[numlist.size ()]);        Collections.copy (numlistcopy,numlist);    Numlistcopy.foreach (System.out::p rintln); }    /*** ReplaceAll Replace all elements * 123 222 222*/    Private Static voidtest10 () {List<Integer> numlist= arrays.aslist (123,22,85,44,22); Collections.replaceall (Numlist,22,222);    Numlist.foreach (System.out::p rintln); }}
Basic Way to use demo

11. Switch from a thread-unsafe list to a thread-safe list

Private Static void test11 () {        List<Integer> numlist= arrays.aslist (123,22,85,44);        List<Integer> synchronizedlist= collections.synchronizedlist (numlist);        Synchronizedlist.foreach (System.out::p rintln);    }
from a thread-unsafe list to a thread-safe list

Use of 12.Enumeration

  /**      * Enumeration Use     * A1     A2     A3     */    privatestatic void test12 () {        enumeration e=new stringtokenizer ("A1-a2-a3", "-");          while (E.hasmoreelements ()) {            System.out.println (e.nextelement ());        }    }
use of enumeration

Java Collections Tool Class

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.