Java Collection (Tool class collections)

Source: Internet
Author: User

/*
* * Collections.sort (List collection, comparator);
The *sort method not only allows the list to be sorted naturally, but can also be sorted according to the requirement definition comparer.
The *sort method cannot sort a set collection.
The *binarysearch method inserts the collection and ensures that the collection is ordered, if the element found does not exist
* Returns the inverse of the subscript plus one for the element that is larger than the element, and his principle is binary lookup.
The *fill method replaces all the elements in the list collection with the specified elements.
* Practice to replace some elements of the collection with the specified elements.
The *replaceall method replaces an element with a specified element.
The *reverse method reverses all the elements.
The *swap method replaces the two elements specified in the collection with a position.
The *shuffle method randomly arranges all the elements in the collection once.
*/
Import java.util.*;
public class Collectionsdemo {

public static void Main (string[] args) {
Sortdemo ();
Maxdemo ();
Binarysearchdemo ();
Filldemo ();
Replacealldemo ();
Shufledemo ();
}
public static <T> void sop (T t)
{
System.out.println (t);
}
Randomly arranges all the elements in a collection
public static void Shufledemo () {
List<string> list=new arraylist<string> ();
List.add ("ABCD");
List.add ("AAA");
List.add ("Z");
List.add ("KKKK");
List.add ("QQ");
List.add ("zz");
SOP (list);
Collections.shuffle (list);
SOP (list);
}
Replaces an element with the specified element.
public static void Replacealldemo ()
{
List<string> list=new arraylist<string> ();
List.add ("ABCD");
List.add ("AAA");
List.add ("Z");
List.add ("KKKK");
List.add ("QQ");
List.add ("zz");
SOP (list);
Collections.replaceall (list, "AAA", "pp");
Collections.reverse (list);
Collections.swap (list,1, 2);
SOP (list);
}
Replaces all elements in the list collection with the specified element.
public static void Filldemo ()
{
List<string> list=new arraylist<string> ();
List.add ("ABCD");
List.add ("AAA");
List.add ("Z");
List.add ("KKKK");
List.add ("QQ");
List.add ("zz");
SOP (list);
Collections.fill (List, "pp");
SOP (list);
}

The BinarySearch method inserts the collection and ensures that the collection is orderly.
public static void Binarysearchdemo ()
{
List<string> list=new arraylist<string> ();
List.add ("ABCD");
List.add ("AAA");
List.add ("Z");
List.add ("KKKK");
List.add ("QQ");
List.add ("zz");
Sort by using the Sort method
Collections.sort (list);
SOP (list);
int Index=collections.binarysearch (list, "oo");
SOP (index);
}
Take maximum value
public static void Maxdemo ()
{
List<string> list=new arraylist<string> ();
List.add ("ABCD");
List.add ("AAA");
List.add ("Z");
List.add ("KKKK");
List.add ("QQ");
List.add ("zz");
Sort by using the Sort method
Collections.sort (list);
SOP (list);
Use the Max method to take the maximum value.
String Max=collections.max (list);
SOP ("max=" +max);
}
Sort by using the sort method.
public static void Sortdemo ()
{
List<string> list=new arraylist<string> ();
List.add ("ABCD");
List.add ("AAA");
List.add ("Z");
List.add ("KKKK");
List.add ("QQ");
List.add ("Z");
SOP (list);
Collections.sort (List,new comlength ());
SOP (list);
}
}
Defines a comparer that compares the length of a string.
Class Comlength implements comparator<string>{


public int Compare (string O1, string O2) {
if (O1.length () >o2.length ())
return 1;
else if (O1.length () <o2.length ())
return-1;
else return O1.compareto (O2);
}

}


/*
* The Reverseorder () method passes an inverse comparer to the TreeSet collection, which deserializes all the elements.
* Reverseorder () also has an overloaded method Reverseorder (comparator<t> cmp)
Returns a comparer that forcibly reverses the order of the specified comparer.
*
*/
Import java.util.Collections;
Import Java.util.Comparator;
Import Java.util.Iterator;
Import Java.util.TreeSet;




public class CollectionsDemo2 {
public static void Main (String args[])
{
Orderdemo ();
}

public static void Orderdemo ()
{
Call the Reverseorder method with parameters, first sort the collection by its parameter comparer, and then force the collection elements back to order.
Treeset<string> ts=new treeset<string> (Collections.reverseorder (New StrComparator2 ()));
Ts.add ("Abvde");
Ts.add ("a");
Ts.add ("KK");
Ts.add ("CCC");
Iterator<string> It=ts.iterator ();
while (It.hasnext ())
{
SOP (It.next ());
}
}
public static <T> void sop (T t)
{
System.out.println (t);
}
}
Define a comparer
Class Strcomparator implements Comparator<string>
{
public int Compare (string O1, string O2) {
Return O2.compareto (O1);
}
}
Define a comparer
Class StrComparator2 implements Comparator<string>
{
public int Compare (string O1, string O2) {
if (O1.length () >o2.length ())
return 1;
if (O1.length () <o2.length ())
return-1;
Return O1.compareto (O2);
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Collection (Tool class collections)

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.