cleanup work
This commit is contained in:
parent
5df4fa0181
commit
7194d8b84a
|
@ -17,64 +17,42 @@ import java.util.Map.Entry;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.manipulation.Sortable;
|
|
||||||
|
|
||||||
import com.google.common.primitives.Ints;
|
import com.google.common.primitives.Ints;
|
||||||
|
|
||||||
public class JavaSorting {
|
public class JavaSorting {
|
||||||
|
|
||||||
private int [] toSort;
|
private int[] toSort;
|
||||||
private int [] sortedInts;
|
private int[] sortedInts;
|
||||||
private int [] sortedRangeInts;
|
private int[] sortedRangeInts;
|
||||||
// private Integer [] integers;
|
// private Integer [] integers;
|
||||||
// private Integer [] sortedIntegers;
|
// private Integer [] sortedIntegers;
|
||||||
// private List<Integer> integersList;
|
// private List<Integer> integersList;
|
||||||
// private List<Integer> sortedIntegersList;
|
// private List<Integer> sortedIntegersList;
|
||||||
private Employee[] employees;
|
private Employee[] employees;
|
||||||
private Employee[] employeesSorted;
|
private Employee[] employeesSorted;
|
||||||
private Employee[] employeesSortedByAge;
|
private Employee[] employeesSortedByAge;
|
||||||
private HashMap<Integer, String> map;
|
private HashMap<Integer, String> map;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initVariables () {
|
public void initVariables() {
|
||||||
|
|
||||||
toSort = new int[]
|
toSort = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
|
||||||
{ 5, 1, 89, 255, 7, 88, 200, 123, 66 };
|
sortedInts = new int[] { 1, 5, 7, 66, 88, 89, 123, 200, 255 };
|
||||||
sortedInts = new int[]
|
sortedRangeInts = new int[] { 5, 1, 89, 7, 88, 200, 255, 123, 66 };
|
||||||
{1, 5, 7, 66, 88, 89, 123, 200, 255};
|
|
||||||
sortedRangeInts = new int[]
|
// integers = new Integer[]
|
||||||
{5, 1, 89, 7, 88, 200, 255, 123, 66};
|
// { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
|
||||||
|
// sortedIntegers = new Integer[]
|
||||||
// integers = new Integer[]
|
// {1, 5, 7, 66, 88, 89, 123, 200, 255};
|
||||||
// { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
|
//
|
||||||
// sortedIntegers = new Integer[]
|
// integersList = Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 });
|
||||||
// {1, 5, 7, 66, 88, 89, 123, 200, 255};
|
// sortedIntegersList = Arrays.asList(new Integer[] {1, 5, 7, 66, 88, 89, 123, 200, 255});
|
||||||
//
|
|
||||||
// integersList = Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 });
|
employees = new Employee[] { new Employee("John", 23, 5000), new Employee("Steve", 26, 6000), new Employee("Frank", 33, 7000), new Employee("Earl", 43, 10000), new Employee("Jessica", 23, 4000), new Employee("Pearl", 33, 6000) };
|
||||||
// sortedIntegersList = Arrays.asList(new Integer[] {1, 5, 7, 66, 88, 89, 123, 200, 255});
|
employeesSorted = new Employee[] { new Employee("Earl", 43, 10000), new Employee("Frank", 33, 70000), new Employee("Jessica", 23, 4000), new Employee("John", 23, 5000), new Employee("Pearl", 33, 4000), new Employee("Steve", 26, 6000) };
|
||||||
|
employeesSortedByAge = new Employee[] { new Employee("John", 23, 5000), new Employee("Jessica", 23, 4000), new Employee("Steve", 26, 6000), new Employee("Frank", 33, 70000), new Employee("Pearl", 33, 4000), new Employee("Earl", 43, 10000) };
|
||||||
employees = new Employee[] {
|
|
||||||
new Employee("John", 23, 5000),
|
|
||||||
new Employee("Steve", 26, 6000),
|
|
||||||
new Employee("Frank", 33, 7000),
|
|
||||||
new Employee("Earl", 43, 10000),
|
|
||||||
new Employee("Jessica", 23, 4000),
|
|
||||||
new Employee("Pearl", 33, 6000)};
|
|
||||||
employeesSorted = new Employee[] {
|
|
||||||
new Employee("Earl", 43, 10000),
|
|
||||||
new Employee("Frank", 33, 70000),
|
|
||||||
new Employee("Jessica", 23, 4000),
|
|
||||||
new Employee("John", 23, 5000),
|
|
||||||
new Employee("Pearl", 33, 4000),
|
|
||||||
new Employee("Steve", 26, 6000)};
|
|
||||||
employeesSortedByAge = new Employee[] {
|
|
||||||
new Employee("John", 23, 5000),
|
|
||||||
new Employee("Jessica", 23, 4000),
|
|
||||||
new Employee("Steve", 26, 6000),
|
|
||||||
new Employee("Frank", 33, 70000),
|
|
||||||
new Employee("Pearl", 33, 4000),
|
|
||||||
new Employee("Earl", 43, 10000)};
|
|
||||||
|
|
||||||
HashMap<Integer, String> map = new HashMap<>();
|
HashMap<Integer, String> map = new HashMap<>();
|
||||||
map.put(55, "John");
|
map.put(55, "John");
|
||||||
map.put(22, "Apple");
|
map.put(22, "Apple");
|
||||||
|
@ -84,7 +62,7 @@ public class JavaSorting {
|
||||||
map.put(6, "Rocky");
|
map.put(6, "Rocky");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenIntArray_whenUsingSort_thenSortedArray() {
|
public void givenIntArray_whenUsingSort_thenSortedArray() {
|
||||||
Arrays.sort(toSort);
|
Arrays.sort(toSort);
|
||||||
|
@ -94,24 +72,24 @@ public class JavaSorting {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenIntegerArray_whenUsingSort_thenSortedArray() {
|
public void givenIntegerArray_whenUsingSort_thenSortedArray() {
|
||||||
Integer [] integers = ArrayUtils.toObject(toSort);
|
Integer[] integers = ArrayUtils.toObject(toSort);
|
||||||
Arrays.sort(integers, new Comparator<Integer>() {
|
Arrays.sort(integers, new Comparator<Integer>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(Integer a, Integer b) {
|
public int compare(Integer a, Integer b) {
|
||||||
return a - b;
|
return a - b;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assertTrue(Arrays.equals(integers, ArrayUtils.toObject(sortedInts)));
|
assertTrue(Arrays.equals(integers, ArrayUtils.toObject(sortedInts)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArray_whenUsingSortWithLambdas_thenSortedArray() {
|
public void givenArray_whenUsingSortWithLambdas_thenSortedArray() {
|
||||||
Integer [] integersToSort = ArrayUtils.toObject(toSort);
|
Integer[] integersToSort = ArrayUtils.toObject(toSort);
|
||||||
Arrays.sort(integersToSort, (a, b) -> {
|
Arrays.sort(integersToSort, (a, b) -> {
|
||||||
return a - b;
|
return a - b;
|
||||||
});
|
});
|
||||||
|
|
||||||
assertTrue(Arrays.equals(integersToSort, ArrayUtils.toObject(sortedInts)));
|
assertTrue(Arrays.equals(integersToSort, ArrayUtils.toObject(sortedInts)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,39 +100,35 @@ public class JavaSorting {
|
||||||
assertTrue(Arrays.equals(employees, employeesSorted));
|
assertTrue(Arrays.equals(employees, employeesSorted));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenIntArray_whenUsingRangeSort_thenRangeSortedArray() {
|
public void givenIntArray_whenUsingRangeSort_thenRangeSortedArray() {
|
||||||
Arrays.sort(toSort, 3, 7);
|
Arrays.sort(toSort, 3, 7);
|
||||||
|
|
||||||
assertTrue(Arrays.equals(toSort, sortedRangeInts));
|
assertTrue(Arrays.equals(toSort, sortedRangeInts));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenIntArray_whenUsingParallelSort_thenParallelSortedArray() {
|
public void givenIntArray_whenUsingParallelSort_thenArraySorted() {
|
||||||
Arrays.parallelSort(toSort);
|
Arrays.parallelSort(toSort);
|
||||||
|
|
||||||
assertTrue(Arrays.equals(toSort, sortedInts));
|
assertTrue(Arrays.equals(toSort, sortedInts));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayObjects_whenUsingComparing_thenSortedArrayObjects() {
|
public void givenArrayObjects_whenUsingComparing_thenSortedArrayObjects() {
|
||||||
List<Employee> employeesList = Arrays.asList(employees);
|
List<Employee> employeesList = Arrays.asList(employees);
|
||||||
|
|
||||||
employeesList.sort(Comparator.comparing(Employee::getAge));//.thenComparing(Employee::getName));
|
employeesList.sort(Comparator.comparing(Employee::getAge));// .thenComparing(Employee::getName));
|
||||||
|
|
||||||
assertTrue(Arrays.equals(employeesList.toArray(), employeesSortedByAge));
|
assertTrue(Arrays.equals(employeesList.toArray(), employeesSortedByAge));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenList_whenUsingSort_thenSortedList() {
|
public void givenList_whenUsingSort_thenSortedList() {
|
||||||
List<Integer> toSortList = Ints.asList(toSort);
|
List<Integer> toSortList = Ints.asList(toSort);
|
||||||
Collections.sort(toSortList);
|
Collections.sort(toSortList);
|
||||||
|
|
||||||
assertTrue(Arrays.equals(toSortList.toArray(),
|
assertTrue(Arrays.equals(toSortList.toArray(), ArrayUtils.toObject(sortedInts)));
|
||||||
ArrayUtils.toObject(sortedInts)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -172,14 +146,13 @@ public class JavaSorting {
|
||||||
for (Map.Entry<Integer, String> entry : entries) {
|
for (Map.Entry<Integer, String> entry : entries) {
|
||||||
sortedMap.put(entry.getKey(), entry.getValue());
|
sortedMap.put(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(Arrays.equals(sortedMap.keySet().toArray(), sortedKeys));
|
assertTrue(Arrays.equals(sortedMap.keySet().toArray(), sortedKeys));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMap_whenSortingByValues_thenSortedMap() {
|
public void givenMap_whenSortingByValues_thenSortedMap() {
|
||||||
String[] sortedValues = new String[]
|
String[] sortedValues = new String[] { "Apple", "Earl", "George", "John", "Pearl", "Rocky" };
|
||||||
{ "Apple", "Earl", "George", "John", "Pearl", "Rocky" };
|
|
||||||
|
|
||||||
List<Map.Entry<Integer, String>> entries = new ArrayList<>(map.entrySet());
|
List<Map.Entry<Integer, String>> entries = new ArrayList<>(map.entrySet());
|
||||||
Collections.sort(entries, new Comparator<Entry<Integer, String>>() {
|
Collections.sort(entries, new Comparator<Entry<Integer, String>>() {
|
||||||
|
@ -192,30 +165,22 @@ public class JavaSorting {
|
||||||
for (Map.Entry<Integer, String> entry : entries) {
|
for (Map.Entry<Integer, String> entry : entries) {
|
||||||
sortedMap.put(entry.getKey(), entry.getValue());
|
sortedMap.put(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(Arrays.equals(sortedMap.values().toArray(), sortedValues));
|
assertTrue(Arrays.equals(sortedMap.values().toArray(), sortedValues));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSet_whenUsingSort_thenSortedSet() {
|
public void givenSet_whenUsingSort_thenSortedSet() {
|
||||||
HashSet<Integer> integersSet = new LinkedHashSet<>(Ints.asList(toSort));
|
HashSet<Integer> integersSet = new LinkedHashSet<>(Ints.asList(toSort));
|
||||||
HashSet<Integer> descSortedIntegersSet = new LinkedHashSet<>(Arrays.asList(new Integer[]
|
HashSet<Integer> descSortedIntegersSet = new LinkedHashSet<>(Arrays.asList(new Integer[] { 255, 200, 123, 89, 88, 66, 7, 5, 1 }));
|
||||||
{255, 200, 123, 89, 88, 66, 7, 5, 1}));
|
|
||||||
|
|
||||||
ArrayList<Integer> list = new ArrayList<Integer>(integersSet);
|
ArrayList<Integer> list = new ArrayList<Integer>(integersSet);
|
||||||
Collections.sort(list, (i1, i2) -> {
|
Collections.sort(list, (i1, i2) -> {
|
||||||
return i2 - i1;
|
return i2 - i1;
|
||||||
});
|
});
|
||||||
integersSet = new LinkedHashSet<>(list);
|
integersSet = new LinkedHashSet<>(list);
|
||||||
|
|
||||||
assertTrue(Arrays.equals(integersSet.toArray(), descSortedIntegersSet.toArray()));
|
assertTrue(Arrays.equals(integersSet.toArray(), descSortedIntegersSet.toArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue