Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of java Custom and Natural sorting

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "sample analysis of java customization and natural sorting", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of java customization and natural sorting".

Two sorting methods are provided in the utility class Collections of the java collection, which are:

Collections.sort (List list) Collections.sort (List list,Comparator c)

The first is called natural sorting. The objects participating in the sorting need to implement the comparable interface and override their compareTo () method. The comparison size rules of the objects are implemented in the method body, as shown in the following example:

Entity class: (basic attributes, getter/setter method, parameter-free construction method, toString method)

Package test;public class Emp implements Comparable {private String name; private int age; public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public Emp () {super ();} public Emp (String name, int age) {super (); this.name = name; this.age = age } @ Override public String toString () {return "Emp [name=" + name + ", age=" + age + "]";} @ Override public int compareTo (Object o) {if (o instanceof Emp) {Emp emp = (Emp) if / return this.age-emp.getAge (); / / sort return this.name.compareTo (emp.getName ()) by age ascending order; / / sort by name ascending order} throw new ClassCastException ("objects that cannot be converted to Emp type...") }}

The second kind is called custom sorting, or custom sorting. You need to write an anonymous inner class, first new a comparator object c of the Comparator interface, and at the same time implement compare () its method.

Then pass the comparator object c to the parameter list of the Collections.sort () method to achieve the sorting function

Description: the first method is not flexible enough, after the entity class implements the comparable interface, it will increase the coupling. If you need to call the sorting method according to different attributes in different locations in the project, you need to modify the comparison rules repeatedly (by name or by age), the two can only choose one, and there will be conflicts. The second kind solves this problem very well. Where needed, create an instance of the inner class and override its comparison method.

The jUnit4 unit test class code is as follows:

Package test;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import org.junit.BeforeClass;import org.junit.Test;public class TestSort {static List list = new ArrayList (); / / @ BeforeClass annotated methods are executed before other test methods are executed, / / and only once. @ Before annotated methods are executed before each test method / / it only takes once to initialize the collection here, so use @ BeforeClass. @ BeforeClass public static void init () {list.add (new Emp ("tom", 18)); list.add (new Emp ("jack", 20)); list.add (new Emp ("rose", 15)); list.add (new Emp ("jerry", 17)); System.out.println ("before sorting:"); for (Object o: list) {System.out.println (o) }} / * * sort by age ascending order * / / @ Test// public void testSortAge () {/ / Collections.sort (list); / / System.out.println ("Natural sort by age:"); / / for (Object o: list) {/ / System.out.println (o); / /} / /} / / * * sort by name ascending order * / @ Test public void testSortName () {Collections.sort (list) System.out.println ("Natural sorting in ascending name:"); for (Object o: list) {System.out.println (o);}} / * * sort by age ascending order using Comparator comparator * / @ Test public void testComparatorSortAge () {Collections.sort (list,new Comparator () {@ Override public int compare (Object o1, Object O2) {if (o1 instanceof Emp & & O2 instanceof Emp) {Emp e1 = (Emp) o1; Emp e2 = (Emp) o2 Return e1.getAge ()-e2.getAge ();} throw new ClassCastException ("cannot be converted to Emp type");}}); System.out.println ("after sorting by age ascending order using Comparator comparator:"); for (Object o: list) {System.out.println (o) }} / * * sort by name ascending order using Comparator comparator * / @ Test public void testComparatorSortName () {Collections.sort (list,new Comparator () {@ Override public int compare (Object o1, Object O2) {if (o1 instanceof Emp & & O2 instanceof Emp) {Emp e1 = (Emp) o1; Emp e2 = (Emp) O2; return e1.getName (). CompareTo (e2.getName ());} throw new ClassCastException ("cannot be converted to Emp type");}}) System.out.println ("after sorting by name ascending order using Comparator comparator:"); for (Object o: list) {System.out.println (o);}

Right-click the blank space-> Run As-> JUnit Test->

The running results are as follows:

Before sorting: Emp [name=tom, age=18] Emp [name=jack, age=20] Emp [name=rose, age=15] Emp [name=jerry, age=17] Natural sort according to name ascending order: Emp [name=jack, age=20] Emp [name=jerry, age=17] Emp [name=rose, age=15] Emp [name=tom, age=18] sort in age Ascending order using Comparator comparator: Emp [name=rose, age=15] Emp [name=jerry, age=17] Emp [name=tom, age=18] age=18 [Emp, Emp] after sorting in the ascending order of Emp using the Emp comparator: name=jack [name=jack, name=jack] age=20 [age=20,] Age=15] Emp [name=tom, age=18]

The above is all the content of the article "sample Analysis of java Custom and Natural sorting". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report