JAVA application sorting

Source: Internet
Author: User

Overview: 1. This article describes how to sort the List (Array input. 2. You can use TreeSet and TreeMap to sort Set and Map. For more information, see related articles in this blog (connections are provided below ). 3. There are two ways to sort a List (arrays also apply): Method 1: implement a Comparator. Method 2: implement the compareTo method of the Comparable interface for objects in the List. 4. After completing the preceding steps, the collection uses Collections. sort, And the array uses Arrays. sort for sorting. [Java] package com. cxy. collection; import java. util. arrayList; import java. util. arrays; import java. util. collections; import java. util. comparator; import java. util. hashSet; import java. util. list; import java. util. set;/*** @ author cxy */public class ListSortTest {public static void main (String [] args) {List <Student> l = new ArrayList (); student s1 = new Student ("James", 76); Student s2 = new Student (" ", 99); Student s3 = new Student (" ", 60); l. add (s1); l. add (s2); l. add (s3); System. out. print ("before List sorting:"); System. out. println (l); // The first method: A common object, then implement a comparator, and use the following method to sort Collections. sort (l, new StudentComparator (); System. out. print ("sorted by List:"); System. out. println (l); System. out. println ("============================= "); // messy array Student [] sa = {s1, s2, s3}; System. out. print ("Before sorting the array:"); System. out. println (sa [0]. toSt Ring () + sa [1] + sa [2]); Arrays. sort (sa, new StudentComparator (); System. out. print ("after sorting the array:"); System. out. println (sa [0]. toString () + sa [1] + sa [2]); System. out. println ("============= The following are all ComparableStudent demos ============== "); // The object itself has the sorting capability List <ComparableStudent> l1 = new ArrayList (); ComparableStudent s11 = new ComparableStudent ("strong James", 76 ); comparableStudent s22 = new ComparableStudent ("tough little black", 99); ComparableStuden T s33 = new ComparableStudent ("strong little white", 60); // PS: strong means can be understood as winning over o_o l1.add (s11); l1.add (s22 ); l1.add (s33); System. out. print ("before List sorting:"); System. out. println (l1); Collections. sort (l1); System. out. print ("sorted by List:"); System. out. println (l1); System. out. println ("============================= "); // ComparableStudent [] cs = {s11, s22, s33}; System. out. print ("Before sorting the array:"); System. out. println (cs [0]. toString () + cs [1] + Cs [2]); Arrays. sort (cs); System. out. print ("after sorting the array:"); System. out. println (cs [0]. toString () + cs [1] + cs [2]) ;}// a common student class, sort by student's learning score // here is just a "simple" class designed to quickly display the sorting, so do not pursue the unscientific design of this class, member variables should be private. Class Student {public String name; // name public int score; // score Student (String name, int score) {this. name = name; this. score = score ;}@ Override public String toString () {return "[" + name + ":" + score + "]" ;}}// a student's comparator, generics define the comparison scope as Student, or do not define generics, forcibly convert class StudentComparator implements Comparator <Student >{@ Override public int compare (Student s1, Student s2) {if (s1.score> s2.score) retu Rn 1; else if (s1.score <s2.score) return-1; else return 0 ;}}/*** a student class * compareTo that implements the Comparable interface: * x. compareTo (y) is used as an example. when x is smaller than y, a negative number is returned. If x is greater than y, a positive number is returned. 0 is returned when the values are equal. */class ComparableStudent implements Comparable <ComparableStudent> {public String name; // name public int score; // score ComparableStudent (String name, int score) {this. name = name; this. score = score ;}@ Override www.2cto.com public int compareTo (ComparableStudent s) {if (score> s. score) return 1; else if (score <s. score) return-1; else return 0 ;}@ Override public String toString () {return "[" + name + ":" + score + "]" ;}}

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.