Java programming skills: list sort

zhaozj2021-02-16  88

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, sorted a 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, just like this:

List list = ...; list copyoflist = new arraylist (list); collections.sort (CopyOflist);

Here, the list of rows 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? An implementation is like this to implement Comparator:

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 }}

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 default language, you need to pass a local language to getInstance () method, just like:

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);}}

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, distinguishes 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", "Woman", "Manana" "," Manana "," MA? ANA "," MA? ANA "," Mantra "," Mantra "," MANTEL "," MANTEL "}; // Create Frame To Display Sortingsjframe Frame = New JFrame (" sorting " Frame.setdefaultCloseOperatio n (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; // create initial list to sortlist list = new arraylist (arrays.aslist (word)); Out.println ("Original List:"); Out.println (List); OUT.Println (); // Perform Default SortCollections.Sort (List); Out.println ("Default Sorting:"); Out.println (List); out.println ();

// Reset list list = new ArrayList (Arrays.asList (words)); // Perform case insensitive sortComparator comp = new CaseInsensitiveComparator (); Collections.sort (list, comp); out.println ( "Case insensitive sorting:") Out.println (list); out.println (); // reset ListList = new arraylist (arrays.aslist (words)); // Perform collation sortcomp = new collatorcomparator (); Collectes.Sort (List, comp); Out.println ("Collator Sorting:"); Out.Println (List); Out.println (); // Fill Text Area and DisplaytextArea.Settext (Buffer.Tostring ()); frame.pack (); frame.show ();}} If your main problem is sequential, the list is not your good data structure selection. As long as your collection is not repeated, you can save your elements in the tree (providing or not providing comparator). In this way, the elements will always be sorted.

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

New Post(0)