From b8161aca1a5a9b46931c11b1dabaa5ebb0a009af Mon Sep 17 00:00:00 2001 From: maverick Date: Mon, 10 Oct 2016 08:14:51 +0530 Subject: [PATCH] Java sorting. Adding required files. This is commit for starting from new fork due to conflicts in PR. --- .../org/baeldung/java/sorting/Employee.java | 58 +++++ .../baeldung/java/sorting/JavaSorting.java | 211 ++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/Employee.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/JavaSorting.java diff --git a/core-java/src/test/java/org/baeldung/java/sorting/Employee.java b/core-java/src/test/java/org/baeldung/java/sorting/Employee.java new file mode 100644 index 0000000000..c48cdd91a8 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/Employee.java @@ -0,0 +1,58 @@ +package org.baeldung.java.sorting; + +public class Employee implements Comparable { + private String name; + private int age; + private double salary; + + public Employee() { + } + + public Employee(String name, int age, double salary) { + super(); + this.name = name; + this.age = age; + this.salary = salary; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + this.salary = salary; + } + + @Override + public String toString() { + return "(" + name + "," + age + "," + salary + ")"; + + } + + @Override + public boolean equals(Object obj) { + return ((Employee) obj).getName().equals(getName()); + } + + @Override + public int compareTo(Object o) { + Employee e = (Employee) o; + return getName().compareTo(e.getName()); + } +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/JavaSorting.java b/core-java/src/test/java/org/baeldung/java/sorting/JavaSorting.java new file mode 100644 index 0000000000..54d58dc87c --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/JavaSorting.java @@ -0,0 +1,211 @@ +package org.baeldung.java.sorting; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.junit.Test; + +public class JavaSorting { + + @Test + public void givenIntArray_whenUsingSort_thenSortedArray() { + int [] numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }, + sortedInts = {1, 5, 7, 66, 88, 89, 123, 200, 255}; + + Arrays.sort(numbers); + + assertTrue(Arrays.equals(numbers, sortedInts)); + } + + @Test + public void givenIntegerArray_whenUsingSort_thenSortedArray() { + Integer[] integers = new Integer[] + { 5, 1, 89, 255, 7, 88, 200, 123, 66 }, + sortedIntegers = {1, 5, 7, 66, 88, 89, 123, 200, 255}; + + + Arrays.sort(integers, new Comparator() { + @Override + public int compare(Integer a, Integer b) { + return a - b; + } + }); + + assertTrue(Arrays.equals(integers, sortedIntegers)); + } + + @Test + public void givenArray_whenUsingSortWithLambdas_thenSortedArray() { + Integer[] integers = new Integer[] + { 5, 1, 89, 255, 7, 88, 200, 123, 66 }, + sortedIntegers = {1, 5, 7, 66, 88, 89, 123, 200, 255}; + + Arrays.sort(integers, (a, b) -> { + return a - b; + }); + + assertTrue(Arrays.equals(integers, sortedIntegers)); + } + + @Test + public void givenEmpArray_SortEmpArray_thenSortedArrayinNaturalOrder() { + Employee[] 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)}; + Employee[] 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)}; + + Arrays.sort(employees); + + assertTrue(Arrays.equals(employees, employeesSorted)); + } + + + @Test + public void givenIntArray_whenUsingRangeSort_thenRangeSortedArray() { + int [] numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }, + sortedRangeInts = {5, 1, 89, 7, 88, 200, 255, 123, 66}; + + Arrays.sort(numbers, 3, 7); + + assertTrue(Arrays.equals(numbers, sortedRangeInts)); + } + + @Test + public void givenIntArray_whenUsingParallelSort_thenParallelSortedArray() { + int [] numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }, + sortedInts = {1, 5, 7, 66, 88, 89, 123, 200, 255}; + + Arrays.parallelSort(numbers); + + assertTrue(Arrays.equals(numbers, sortedInts)); + } + + + + @Test + public void givenArrayObjects_whenUsingComparing_thenSortedArrayObjects() { + List employees = Arrays.asList(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) }); + Employee[] employeesSorted = 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.sort(Comparator.comparing(Employee::getAge));//.thenComparing(Employee::getName)); + + assertTrue(Arrays.equals(employees.toArray(), employeesSorted)); + } + + @Test + public void givenList_whenUsingSort_thenSortedList() { + List integers = Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }), + sortedIntegers = Arrays.asList(new Integer[] {1, 5, 7, 66, 88, 89, 123, 200, 255}); + + Collections.sort(integers); + + assertTrue(Arrays.equals(integers.toArray(), sortedIntegers.toArray())); + } + + @Test + public void givenMap_whenSortingByKeys_thenSortedMap() { + HashMap map = new HashMap<>(); + map.put(55, "John"); + map.put(22, "Apple"); + map.put(66, "Earl"); + map.put(77, "Pearl"); + map.put(12, "George"); + map.put(6, "Rocky"); + Integer[] sortedKeys = new Integer[] { 6, 12, 22, 55, 66, 77 }; + + List> entries = new ArrayList<>(map.entrySet()); + Collections.sort(entries, new Comparator>() { + @Override + public int compare(Entry o1, Entry o2) { + return o1.getKey().compareTo(o2.getKey()); + } + }); + HashMap sortedMap = new LinkedHashMap<>(); + for (Map.Entry entry : entries) { + sortedMap.put(entry.getKey(), entry.getValue()); + } + + assertTrue(Arrays.equals(sortedMap.keySet().toArray(), sortedKeys)); + } + + @Test + public void givenMap_whenSortingByValues_thenSortedMap() { + HashMap map = new HashMap<>(); + map.put(55, "John"); + map.put(22, "Apple"); + map.put(66, "Earl"); + map.put(77, "Pearl"); + map.put(12, "George"); + map.put(6, "Rocky"); + String[] sortedValues = new String[] + { "Apple", "Earl", "George", "John", "Pearl", "Rocky" }; + + List> entries = new ArrayList<>(map.entrySet()); + Collections.sort(entries, new Comparator>() { + @Override + public int compare(Entry o1, Entry o2) { + return o1.getValue().compareTo(o2.getValue()); + } + }); + HashMap sortedMap = new LinkedHashMap<>(); + for (Map.Entry entry : entries) { + sortedMap.put(entry.getKey(), entry.getValue()); + } + + assertTrue(Arrays.equals(sortedMap.values().toArray(), sortedValues)); + } + + + + + + + @Test + public void givenSet_whenUsingSort_thenSortedSet() { + HashSet integers = new LinkedHashSet<>(Arrays.asList(new Integer[] + { 5, 1, 89, 255, 7, 88, 200, 123, 66 })), + sortedIntegers = new LinkedHashSet<>(Arrays.asList(new Integer[] + {255, 200, 123, 89, 88, 66, 7, 5, 1})); + + ArrayList list = new ArrayList(integers); + Collections.sort(list, (i1, i2) -> { + return i2 - i1; + }); + integers = new LinkedHashSet<>(list); + + assertTrue(Arrays.equals(integers.toArray(), sortedIntegers.toArray())); + } + + + +}