Java programming skills: sort List
In the Java Collection Framework defined in the List achieve Vector, ArrayList and LinkedList. These sets provide a target group of the index visit. They offer add and delete elements of support. However, they do not have built-in support elements to sort.
Java.util.Collections class you can use in the sort () method on the List elements to sort. You can send a method to the List object, and can also send a List and a Comparator. If all elements in the list is the same type of class and the class implements Comparable interface, you can simply call Collections.sort (). If this had not realized Comparator, you can send a method to sort Comparator (), the sort. If you do not want to use the default sort order of the classification, you can also send a Comparator method to sort () to sort. If the element is not in the list are the same types of categories, you sort of time on the course is not the case fortunate. Unless you prepare a special cross-category of Comparator.
Sort of like how order? If the element is the String object, but in the sort order is carried out in accordance with the character encoding, basically each character ASCII / Unicode values. If strict restrictions in dealing with the English, but in the sort order is usually enough, because it first row AZ, then lowercase letters az. But if you deal with non-English word, or you just want to use a different sort order, so Collections.sort () there have been changes in the second. For example, you want to use a string of anti-sort sequence. In order to achieve this function, you can type in Collections through reverseOrder () to obtain an anti-sequence Comparator. Then you will be transmitted to the anti-sequence Comparator sort () method. In other words, you are as follows:
List list = …;
Comparator comp = Collections.reverseOrder ();
Collections.sort (list, comp);
If the list contains items: Man, man, Woman, and the woman, sort the list will be a good Man, Woman, man, woman. Here there is no complicated. Need to pay attention to the very important point is Collections.sort () is carried out in situ sequencing. If you need to retain the original sequence, the need to replicate the original set, in order, like this:
List list = …;
List copyOfList = new ArrayList (list);
Collections.sort (copyOfList);
Here, a list of Pai good sequence is: Man, Woman, man, woman, but the original list (Man, man, Woman, woman) was retained.
So far, sorting is case-sensitive. How do you not carry out the sort of case? A way to achieve this is to achieve as 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 really do not need the manual to create this class. Instead, you can be there for the Comparator, CASE_INSENSIVTIVE_ORDER, it is in the String class definition.
Such are the ways that small problems. Sort () algorithm to provide a stable order, and to maintain the same elements of the original sequence. This means that consists of two elements of a "woman" and "Woman" list will be different sort, and this difference is based on two elements in the list in the order decision.
What would the different language? Java.text packet provided Collector and CollectionKey language category to distinguish between the sort. Here are examples:
Note that if your text is a local language, rather than the default language, you need to pass a local languages to getInstance () method, like:
Public static class CollatorComparator
Implements Comparator (
Collator collator = Collator.getInstance ();
Public int compare (Object element1,
Object element2) (
Collator.getCollationKey CollationKey key1 = (
Element1.toString ());
Collator.getCollationKey CollationKey key2 = (
Element2.toString ());
Return key1.compareTo (key2);
)
)
You are sort of set keyword, rather than the actual string. This will not only provide fixed not case-sensitive sort, and it is cross-lingual sort. In other words, if your Spanish and non-Spanish words mixed sort, the word ma? Ana (tomorrow) will be the mantra in the front row. If you do not use Collector, ma? Ana mantra will be in the back row.
Below this procedure on a list of different types of sort (the default to distinguish between upper and lower case, the distinction between languages):
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) (
Collator.getCollationKey CollationKey key1 = (
Element1.toString ());
Collator.getCollationKey CollationKey key2 = (
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" and "Woman" and "woman"
"Manana" and "manana", "ma? Ana", "Ma? Ana"
"Mantra", "mantra", "mantel", "Mantel"
);
/ / Create frame to display sortings
JFrame 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 output
StringWriter buffer = new StringWriter ();
PrintWriter out = new PrintWriter (buffer);
/ / Create initial list to sort
List list = new ArrayList (Arrays.asList (words));
Out.println ( "Original list:");
Out.println (list);
Out.println ();
/ / Perform default sort
Collections.sort (list);
Out.println ( "Default sorting:");
Out.println (list);
Out.println ();
/ / Reset list
List = new ArrayList (Arrays.asList (words));
/ / Perform case insensitive sort
Comparator comp = new CaseInsensitiveComparator ();
Collections.sort (list, comp);
Out.println ( "Case insensitive sorting:");
Out.println (list);
Out.println ();
/ / Reset list
List = new ArrayList (Arrays.asList (words));
/ / Perform collation sort
Comp = new CollatorComparator ();
Collections.sort (list, comp);
Out.println ( "Collator sorting:");
Out.println (list);
Out.println ();
/ / Fill text area and display
TextArea.setText (buffer.toString ());
Frame.pack ();
Frame.show ();
)
)
If you visit the main problem is the order may list is not your choice of good data structure. If you do not duplicate the set, you can tree (TreeSet) save your element (provided or not provided Comparator). This element will always be sort of form.








0 Comments to “Java programming skills: sort List”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.