Java Tips: List Sort

zhaozj2021-02-16  128

The list implemented in Java Collection Framework has vector, ArrayList, and LinkedList. These collections provide an index access to the object group. They provide elements to add and delete support. However, they don't sort the built-in elements. You can use the sort () method in the java.util.collections class to sort the List element. You can pass a List object to the method or pass a list and a Comparetor. If the elements in the list are class of the same type, and this class implements a Comparable interface, you can simply call Collectes.Sort (). If this class does not implement Compare, you can also pass a Comparator to Method Sort () to sort. If you don't want to use the default classification order, you can also pass a Comparator to method sort () to sort. If the elements in the list are not all the same type of class, you are not so lucky when you are sorting. Unless you write a dedicated cross-class Comparator. How is the order of sorting? If the element is a String object, the order of the provincial sorting is based on character encoding, which is basically the ASCII / Unicode value of each character. If strictly restrictions are handled in English, the order of sorting is usually sufficient because it first rows A-Z, then lowercase letters A-Z. However, if you handle non-English words, or you just want to use different sort order, such collections.sort () has a second change. For example, you want to sort using a string. To implement this feature, you can get a reverse CoMPARATOR in the Collectes class. Then, you will pass the reverse CoMPARATOR to the Sort () method. In other words, you work as follows: list list = ...; comparator comp = collections.reverseOrder (); Collectes.Sort (List, comp); if the list contains items: Man, Man, Woman, and Woman, sort The list will be Man, Woman, Man, Woman. There is nothing complicated here. A very important point to pay attention is that collections.sort () is in situ sorting. If you need to keep the original sequence, you need to copy the original collection, in sorting, like this: List list = ...; list copyoflist = new arrayList (list); collections.sort (copyoflist); Here, racing The list is: Man, Woman, Man, Woman, but the original list (Man, Man, Woman, Woman) is preserved. So far, sorting is case sensitive. How do you do not sort in size? Such an implementation is achieved as Comparator:. Public static class CaseInsensitiveComparator implements Comparator {public int compare (Object element1, Object element2) {String lower1 = element1.toString () toLowerCase (); String lower2 = element2.toString (). }}}}}}}}}}} You don't need to create this class manually.

Instead, you can be used to exist, CASE_INSENSIVTIVE_ORDER, which is defined in the String class. This implementation has a little problem. The sort () algorithm provides stable sorting and maintains the same elements as the original sequence. This means that a list containing two elements "Woman" and "Woman" will have different sorting, and this difference is determined by the order of the two elements in the list. What will the language difference? The Java.Text package provides the Collector and the CollectionKey class to distinguish between language. Here is an example: Note that if your text is a local language, instead of the default language, you need to pass a local language to getInstance () method, just like: public static class collatorcomparator imports comparator {collator collator = collator.getInstance () ; public int compare (Object element1, Object element2) {CollationKey key1 = collator.getCollationKey (element1.toString ()); CollationKey key2 = collator.getCollationKey (element2.toString ()); return key1.compareTo (key2);}} You are sorting the collected keywords instead of actual strings. This not only provides a fixed unique sorting, but it is the sort of cross-language. In other words, if you sort Spanish and non-Spanish mixed words, the word Ma? Ana (Tomorrow) will be ranked in front of Mantra. If you don't use Collector, Ma? ANA will be ranked behind Mantra.

Below this program performs a list of different types of sorting (default, case-sensitive, differentiated language): import java.awt.borderLayout; import java.awt.container; import java.io. *; Import java. text *;. import java.util *;. import javax.swing *;. public class SortIt {public static class CollatorComparator implements Comparator {Collator collator = Collator.getInstance (); public int compare (Object element1, Object element2) {CollationKey key1 = collator.getCollationKey (element1.toString ()); CollationKey key2 = collator.getCollationKey (element2.toString ()); return key1.compareTo (key2);}} public static class CaseInsensitiveComparator implements Comparator {public int compare (Object element1 , Object element2) {String lower1 = element1.toString () toLowerCase ();. String lower2 = element2.toString () toLowerCase ();. return lower1.compareTo (lower2);}} public static void main (String args [] ) {String Words [] = {"MAN", "MAN", "Woman", "MANANA", "MANANA", "MA? ANA", "MA? ANA", "Mantra", "Mantra "," mantel "," mantel "}; // create frame to display sortingsjframe frame = new jframe (" Sorting "); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane (); JTextArea textArea = new JTextArea (); JScrollPane pane = new JScrollPane (textArea); contentPane.add (pane, BorderLayout.CENTER); // Create buffer for outputStringWriter buffer = new StringWriter (); PrintWriter out = new PrintWriter (buffer); // Create initial list to sortList list = new ArrayList (Arrays.asList (words)); out.println ( "Original list: "); out.println (list); out.println (); // perform default sortcollections.sort (list); out.println ("

转载请注明原文地址:https://www.9cbs.com/read-7946.html

New Post(0)