This article describes the array derepeatment and sorting operations implemented by Java. Share it for your reference, as follows:
Here is a demonstration of Java implementing array deduplication and sorting operations
The example source code written in the article is based on Jdk1.6+, junit4.8.2
java.util.Arrays.sort()
Support sorting int[] , long[] , short[] , char[] , byte[] , float[] , double[] , Object[]
The reference sample code snippet is as follows
// Declare the int array and initialize int[] intArry = {5,4,7,8,2,0,1,9,3,6,10};// Sort the int array Arrays.sort(intArry);Junit test class source code:
package com.gjnote.test.array;import java.util.Arrays;import org.junit.Test;public class TestArraysSort {// Declare the int array and initialize the int[] intArry = {5,4,7,8,2,0,1,9,3,6,10};@Testpublic void test() {// Sort the int array Arrays.sort(intArry);for (int i = 0; i < intArry.length; i++) {System.out.println(intArry[i]);}System.out.println(Arrays.toString(intArry));}}Console output
0
1
2
3
4
5
6
7
8
9
10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
java.util.Collections.sort()
Comparison of objects is realized by implementing the internal compare method
The sample code snippet is as follows
/*** Use Collections.sort(list, Comparator(){});* Recommended method for sorting List arrays*/public void collectionsSortElement1(List list) {Collections.sort(list, new Comparator() {@Overridepublic int compare(String o1, String o2) {// Adjust the order of compareTo objects according to actual sorting needs return (o2).compareTo(o1);}});}Java implements list deduplication
Method 1: Use for loop traversal to remove duplicate elements in List
The code snippet is as follows
List tempList = new ArrayList();// Remove duplicate elements from the original List for (String string : originalList) {if (!tempList.contains(string)) {tempList.add(string);}}Method 2: Use Set to deduplicate
The code snippet is as follows
// Set uses the uniqueness of the Set element and deduplicate Set set = new HashSet(originalList);List tempList = new ArrayList(set);
Method 3: Use TreeSet to remove duplicate elements
TreeSet treeSet = new TreeSet(originalList);ListtempList = new ArrayList();tempList.addAll(treeSet);// treeSet The default sorting is ascending order, add whether the inverse order is required according to the actual situation. Collections.reverse(tempList);
Java implements derepeatment of List after sorting
Junit Test List Deduplication and Sorting Source Code
package com.gjnote.test.array;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.TreeSet;import org.junit.Before;import org.junit.Test;/*** Test Class*List Array De-heavy Element Sort** @version 1.0* @author www.gjnote.com**/public class TestListArraySort {private ListoriginalList = null;@Beforepublic void setUp() throws Exception {originalList = new ArrayList();for (int i = 10000; i > 0; i--) {originalList.add("element" + i);// add repeat elementif(i % 2 == 0) {originalList.add("element" + i);}}}/*** Output List element* @param list*/private void outputList(List list) {for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}}/*** Use Collections.sort(list, Comparator(){});* Recommended sorting method*/private void collectionsSortElement(List list) {long start = System.currentTimeMillis();Collections.sort(list, new Comparator() {@Overridepublic int compare(String o1, String o2) {// Adjust the order of compareTo objects according to actual sorting needs to be returned o2.compareTo(o1);}});//outputList(tempList);System.out.println("Collections.sort:"+ (System.currentTimeMillis() - start) + "ms");}/*** Test to use for loop traversal to remove duplicate elements* Collections.sort sort*/@Testpublic void testForLoopRemoveRepeatElement() {System.out.println("testForLoopRemoveRepeatElement"); long start = System.currentTimeMillis();List tempList = new ArrayList();// Remove duplicate elements for (String string : originalList) {if (!tempList.contains(string)) {tempList.add(string);}}// Sort collectionsSortElement(tempList);//outputList(tempList);System.out.println("Use a for loop to traverse List and remove duplicate elements: "+ (System.currentTimeMillis() - start) + "ms");}/*** Test to use Set to deduplicate; * Use Collections.sort(list, Comparator(){}); Sort**/@Testpublic void testSetRemoveRepeatElement() {System.out.println("testSetRemoveRepeatElement"); long start = System.currentTimeMillis();// Sort first (theoretical value: sorting first and then deduplication will be more efficient than sorting later) collectionsSortElement(originalList);// Set uses the uniqueness of Set elements, deduplication Set set = new HashSet(originalList);List tempList = new ArrayList(set);// After sorting, you can comment first sorting, and then turn on sorting try running time //collectionsSortElement(tempList);//outputList(tempList);System.out.println("Collections.sort sort, use Set to deduplicate: "+ (System.currentTimeMillis() - start) + "ms");}/*** Test Use TreeSet to remove duplicate elements* Default sort or Collections.reverse flip sort*/@Testpublic void testTreeSetRemoveRepeatElement() {System.out.println("testTreeSetRemoveRepeatElement"); long start = System.currentTimeMillis();TreeSettreeSet = new TreeSet(originalList);ListtempList = new ArrayList();tempList.addAll(treeSet);// treeSet The default sort is ascending order, add whether you need to reverse sort according to the actual situation. Collections.reverse(tempList);//outputList(tempList);System.out.println("Sort with TreeSet, remove duplicate elements: "+ (System.currentTimeMillis() - start) + "ms");}@Testpublic void testMethods() {//outputList(originalList);// List Recommended method for deduplication testSetRemoveRepeatElement();// 14mstestTreeSetRemoveRepeatElement();// 20ms//testForLoopRemoveRepeatElement();// 2525ms}}Run testSetRemoveRepeatElement() console to output the result
testSetRemoveRepeatElement
Collections.sort: 8ms
Collections.sort sort, use Set to deduplicate: 14ms
Run testTreeSetRemoveRepeatElement() console to output the result
testTreeSetRemoveRepeatElement
Sort with TreeSet, remove duplicate elements: 20ms
Run testForLoopRemoveRepeatElement() console to output the result
testForLoopRemoveRepeatElement
Collections.sort: 7ms
Use a for loop to traverse List, remove duplicate elements: 2525ms
PS: Here are a few related tools for your reference:
Online Removal Tool:
http://tools.VeVB.COM/code/quchong
Online text repetition tool:
http://tools.VeVB.COM/aideddesign/txt_quchong
Online animation demonstration insert/select/bubble/merge/hill/quick sorting algorithm process tool:
http://tools.VeVB.COM/aideddesign/paixu_ys
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.