cleanup work

This commit is contained in:
eugenp 2016-11-30 09:05:06 +02:00
parent 5df4fa0181
commit 7194d8b84a
1 changed files with 47 additions and 82 deletions

View File

@ -17,63 +17,41 @@ import java.util.Map.Entry;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.manipulation.Sortable;
import com.google.common.primitives.Ints;
public class JavaSorting {
private int [] toSort;
private int [] sortedInts;
private int [] sortedRangeInts;
// private Integer [] integers;
// private Integer [] sortedIntegers;
// private List<Integer> integersList;
// private List<Integer> sortedIntegersList;
private int[] toSort;
private int[] sortedInts;
private int[] sortedRangeInts;
// private Integer [] integers;
// private Integer [] sortedIntegers;
// private List<Integer> integersList;
// private List<Integer> sortedIntegersList;
private Employee[] employees;
private Employee[] employeesSorted;
private Employee[] employeesSortedByAge;
private HashMap<Integer, String> map;
@Before
public void initVariables () {
public void initVariables() {
toSort = new int[]
{ 5, 1, 89, 255, 7, 88, 200, 123, 66 };
sortedInts = new int[]
{1, 5, 7, 66, 88, 89, 123, 200, 255};
sortedRangeInts = new int[]
{5, 1, 89, 7, 88, 200, 255, 123, 66};
toSort = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
sortedInts = new int[] { 1, 5, 7, 66, 88, 89, 123, 200, 255 };
sortedRangeInts = new int[] { 5, 1, 89, 7, 88, 200, 255, 123, 66 };
// integers = new Integer[]
// { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
// sortedIntegers = 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 });
// sortedIntegersList = Arrays.asList(new Integer[] {1, 5, 7, 66, 88, 89, 123, 200, 255});
// integers = new Integer[]
// { 5, 1, 89, 255, 7, 88, 200, 123, 66 };
// sortedIntegers = 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 });
// sortedIntegersList = Arrays.asList(new Integer[] {1, 5, 7, 66, 88, 89, 123, 200, 255});
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)};
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<>();
map.put(55, "John");
@ -94,7 +72,7 @@ public class JavaSorting {
@Test
public void givenIntegerArray_whenUsingSort_thenSortedArray() {
Integer [] integers = ArrayUtils.toObject(toSort);
Integer[] integers = ArrayUtils.toObject(toSort);
Arrays.sort(integers, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
@ -107,7 +85,7 @@ public class JavaSorting {
@Test
public void givenArray_whenUsingSortWithLambdas_thenSortedArray() {
Integer [] integersToSort = ArrayUtils.toObject(toSort);
Integer[] integersToSort = ArrayUtils.toObject(toSort);
Arrays.sort(integersToSort, (a, b) -> {
return a - b;
});
@ -122,7 +100,6 @@ public class JavaSorting {
assertTrue(Arrays.equals(employees, employeesSorted));
}
@Test
public void givenIntArray_whenUsingRangeSort_thenRangeSortedArray() {
Arrays.sort(toSort, 3, 7);
@ -131,19 +108,17 @@ public class JavaSorting {
}
@Test
public void givenIntArray_whenUsingParallelSort_thenParallelSortedArray() {
public void givenIntArray_whenUsingParallelSort_thenArraySorted() {
Arrays.parallelSort(toSort);
assertTrue(Arrays.equals(toSort, sortedInts));
}
@Test
public void givenArrayObjects_whenUsingComparing_thenSortedArrayObjects() {
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));
}
@ -153,8 +128,7 @@ public class JavaSorting {
List<Integer> toSortList = Ints.asList(toSort);
Collections.sort(toSortList);
assertTrue(Arrays.equals(toSortList.toArray(),
ArrayUtils.toObject(sortedInts)));
assertTrue(Arrays.equals(toSortList.toArray(), ArrayUtils.toObject(sortedInts)));
}
@Test
@ -178,8 +152,7 @@ public class JavaSorting {
@Test
public void givenMap_whenSortingByValues_thenSortedMap() {
String[] sortedValues = new String[]
{ "Apple", "Earl", "George", "John", "Pearl", "Rocky" };
String[] sortedValues = new String[] { "Apple", "Earl", "George", "John", "Pearl", "Rocky" };
List<Map.Entry<Integer, String>> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, new Comparator<Entry<Integer, String>>() {
@ -196,16 +169,10 @@ public class JavaSorting {
assertTrue(Arrays.equals(sortedMap.values().toArray(), sortedValues));
}
@Test
public void givenSet_whenUsingSort_thenSortedSet() {
HashSet<Integer> integersSet = new LinkedHashSet<>(Ints.asList(toSort));
HashSet<Integer> descSortedIntegersSet = new LinkedHashSet<>(Arrays.asList(new Integer[]
{255, 200, 123, 89, 88, 66, 7, 5, 1}));
HashSet<Integer> descSortedIntegersSet = new LinkedHashSet<>(Arrays.asList(new Integer[] { 255, 200, 123, 89, 88, 66, 7, 5, 1 }));
ArrayList<Integer> list = new ArrayList<Integer>(integersSet);
Collections.sort(list, (i1, i2) -> {
@ -216,6 +183,4 @@ public class JavaSorting {
assertTrue(Arrays.equals(integersSet.toArray(), descSortedIntegersSet.toArray()));
}
}