From 0706cd84532d324f6556a19ad259fbae882ba919 Mon Sep 17 00:00:00 2001 From: maverick Date: Thu, 28 Jul 2016 16:38:17 +0530 Subject: [PATCH 1/7] First Commit for Sorting Examples. --- .../java/com/baeldung/beans/Employee.java | 49 +++++++++ .../org/baeldung/java/sorting/ArraySort.java | 88 +++++++++++++++ .../baeldung/java/sorting/ComparingTest.java | 38 +++++++ .../org/baeldung/java/sorting/Employee.java | 54 +++++++++ .../org/baeldung/java/sorting/ListSort.java | 59 ++++++++++ .../org/baeldung/java/sorting/MapSort.java | 104 ++++++++++++++++++ .../java/sorting/NaturalOrderExample.java | 34 ++++++ .../org/baeldung/java/sorting/SetSort.java | 54 +++++++++ 8 files changed, 480 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/beans/Employee.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java 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/ListSort.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/MapSort.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/SetSort.java diff --git a/core-java/src/main/java/com/baeldung/beans/Employee.java b/core-java/src/main/java/com/baeldung/beans/Employee.java new file mode 100644 index 0000000000..78d65e18fe --- /dev/null +++ b/core-java/src/main/java/com/baeldung/beans/Employee.java @@ -0,0 +1,49 @@ +package com.baeldung.beans; + +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 int compareTo(Object o) { + Employee e = (Employee) o; + return getAge() - e.getAge() ; + } +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java new file mode 100644 index 0000000000..3e55ab7f6a --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java @@ -0,0 +1,88 @@ +package org.baeldung.java.sorting; + +import static org.junit.Assert.*; + +import java.sql.Array; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class ArraySort { + + private int[] numbers; + private Integer[] integers; + private String[] names; + private Employee[] employees; + + @Before + public void initData() { + numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; + integers = new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; + + names = new String[] { "John", "Apple", "Steve", "Frank", "Earl", "Jessica", "Jake", "Pearl" }; + + 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) }; + + } + + @Test + public void naturalSortIntArray() { + + Arrays.sort(numbers); + + } + + @Test + public void comparatorSortIntArray() { + + Arrays.sort(integers, new Comparator() { + + @Override + public int compare(Integer a, Integer b) { + return a - b; + } + }); + + } + + @Test + public void comparatorSortIntArray_Java8Lambda() { + Arrays.sort(integers, (a, b) -> { + return a - b; + }); + + } + + @Test + public void comparableSortEmployeeArrayByAge_NaturalOrder() { + + Arrays.sort(employees); + + } + + @Test + public void comparatorSortEmployeeArrayByName() { + Arrays.sort(employees, new Comparator() { + + @Override + public int compare(Employee o1, Employee o2) { + + return o1.getName().compareTo(o2.getName()); + } + }); + } + + @Test + public void comparatorSortEmployeeArrayByName_Java8Lambda() { + Arrays.sort(employees, (o1, o2) -> { + return o1.getName().compareTo(o2.getName()); + + }); + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java b/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java new file mode 100644 index 0000000000..02cdede7db --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java @@ -0,0 +1,38 @@ +package org.baeldung.java.sorting; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class ComparingTest { + + private List employees; + + @Before + public void initData() { + + // 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) }); + + } + + @Test + public void testComparing() { + 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) }); + + employees.sort(Comparator.comparing(Employee::getAge).thenComparing(Employee::getName)); + + } + +} 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..f36e552daf --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/Employee.java @@ -0,0 +1,54 @@ +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 int compareTo(Object o) { + Employee e = (Employee) o; + return getAge() - e.getAge(); + } +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java b/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java new file mode 100644 index 0000000000..91e1c40607 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java @@ -0,0 +1,59 @@ +package org.baeldung.java.sorting; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class ListSort { + + private List integers; + private List employees; + + @Before + public void initData() { + integers = Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }); + + 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) }); + } + + @Test + public void naturalOrderIntegerListSort() { + + Collections.sort(integers); + + } + + @Test + public void comparableEmployeeSortByAge() { + + Collections.sort(employees); + + } + + @Test + public void comparatorEmployeeSortByName() { + + Collections.sort(employees, new Comparator() { + @Override + public int compare(Employee e1, Employee e2) { + return e1.getName().compareTo(e2.getName()); + } + }); + + } + + @Test + public void comparatorEmployeeSortByNameJava8() { + + Collections.sort(employees, (e1, e2) -> { + return e1.getName().compareTo(e1.getName()); + }); + + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java b/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java new file mode 100644 index 0000000000..1a9fd30437 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java @@ -0,0 +1,104 @@ +package org.baeldung.java.sorting; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import org.junit.Before; +import org.junit.Test; + +public class MapSort { + + HashMap map; + + @Before + public void initData() { + 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"); + + } + + @Test + public void sortMapByKeys() { + + + 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()); + } + + + } + + @Test + public void sortMapByValues() { + + //showMap(map); + + 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()); + } + +/// showMap(sortedMap); + } + + @Test + public void sortMapViaTreeMap() { + +// showMap(map); + Map treeMap = new TreeMap<>(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1 - o2; + } + }); + + treeMap.putAll(map); +// showMap(treeMap); + + } + + public void showMap(Map map1) { + for (Map.Entry entry : map1.entrySet()) { + System.out.println("[Key: " + entry.getKey() + " , " + "Value: " + entry.getValue() + "] "); + + } + + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java b/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java new file mode 100644 index 0000000000..bb35557561 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java @@ -0,0 +1,34 @@ +package org.baeldung.java.sorting; + +import java.util.Arrays; +import java.util.Comparator; + +import org.junit.Test; + +public class NaturalOrderExample { + + @Test + public void sortArray() { + int[] numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; + + Arrays.sort(numbers); + + } + + @Test + public void sortEmployees() { + 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) }; + + Arrays.sort(employees, new Comparator() { + + @Override + public int compare(Employee o1, Employee o2) { + return -(int) (o1.getSalary() - o2.getSalary()); + } + }); + + + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java b/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java new file mode 100644 index 0000000000..d892223862 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java @@ -0,0 +1,54 @@ +package org.baeldung.java.sorting; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.TreeSet; + +import org.junit.Before; +import org.junit.Test; + +public class SetSort { + + private HashSet integers; + private TreeSet employees; + + @Before + public void initData() { + + integers = new HashSet<>(); + integers.addAll(Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 })); + + employees = new TreeSet<>(); + + employees.addAll(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) })); + + } + + @Test + public void hashSetSortIntegers() { + + + + ArrayList list = new ArrayList(integers); + Collections.sort(list, (i1, i2) -> { + return i2 - i1; + }); + + + } + + @Test + public void treeSetEmployeeSorting() { + + ArrayList list = new ArrayList(employees); + Collections.sort(list, (e1, e2) -> { + return e2.getName().compareTo(e1.getName()); + }); + + } + +} From bf1c98fd1679b3d0b6f6ff6cd766aa98042f2b8f Mon Sep 17 00:00:00 2001 From: maverick Date: Thu, 28 Jul 2016 16:38:17 +0530 Subject: [PATCH 2/7] First Commit for Sorting Examples. --- .../java/com/baeldung/beans/Employee.java | 49 +++++++++ .../org/baeldung/java/sorting/ArraySort.java | 88 +++++++++++++++ .../baeldung/java/sorting/ComparingTest.java | 38 +++++++ .../org/baeldung/java/sorting/Employee.java | 54 +++++++++ .../org/baeldung/java/sorting/ListSort.java | 59 ++++++++++ .../org/baeldung/java/sorting/MapSort.java | 104 ++++++++++++++++++ .../java/sorting/NaturalOrderExample.java | 34 ++++++ .../org/baeldung/java/sorting/SetSort.java | 54 +++++++++ 8 files changed, 480 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/beans/Employee.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java 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/ListSort.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/MapSort.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/SetSort.java diff --git a/core-java/src/main/java/com/baeldung/beans/Employee.java b/core-java/src/main/java/com/baeldung/beans/Employee.java new file mode 100644 index 0000000000..78d65e18fe --- /dev/null +++ b/core-java/src/main/java/com/baeldung/beans/Employee.java @@ -0,0 +1,49 @@ +package com.baeldung.beans; + +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 int compareTo(Object o) { + Employee e = (Employee) o; + return getAge() - e.getAge() ; + } +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java new file mode 100644 index 0000000000..3e55ab7f6a --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java @@ -0,0 +1,88 @@ +package org.baeldung.java.sorting; + +import static org.junit.Assert.*; + +import java.sql.Array; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class ArraySort { + + private int[] numbers; + private Integer[] integers; + private String[] names; + private Employee[] employees; + + @Before + public void initData() { + numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; + integers = new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; + + names = new String[] { "John", "Apple", "Steve", "Frank", "Earl", "Jessica", "Jake", "Pearl" }; + + 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) }; + + } + + @Test + public void naturalSortIntArray() { + + Arrays.sort(numbers); + + } + + @Test + public void comparatorSortIntArray() { + + Arrays.sort(integers, new Comparator() { + + @Override + public int compare(Integer a, Integer b) { + return a - b; + } + }); + + } + + @Test + public void comparatorSortIntArray_Java8Lambda() { + Arrays.sort(integers, (a, b) -> { + return a - b; + }); + + } + + @Test + public void comparableSortEmployeeArrayByAge_NaturalOrder() { + + Arrays.sort(employees); + + } + + @Test + public void comparatorSortEmployeeArrayByName() { + Arrays.sort(employees, new Comparator() { + + @Override + public int compare(Employee o1, Employee o2) { + + return o1.getName().compareTo(o2.getName()); + } + }); + } + + @Test + public void comparatorSortEmployeeArrayByName_Java8Lambda() { + Arrays.sort(employees, (o1, o2) -> { + return o1.getName().compareTo(o2.getName()); + + }); + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java b/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java new file mode 100644 index 0000000000..02cdede7db --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java @@ -0,0 +1,38 @@ +package org.baeldung.java.sorting; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class ComparingTest { + + private List employees; + + @Before + public void initData() { + + // 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) }); + + } + + @Test + public void testComparing() { + 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) }); + + employees.sort(Comparator.comparing(Employee::getAge).thenComparing(Employee::getName)); + + } + +} 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..f36e552daf --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/Employee.java @@ -0,0 +1,54 @@ +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 int compareTo(Object o) { + Employee e = (Employee) o; + return getAge() - e.getAge(); + } +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java b/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java new file mode 100644 index 0000000000..91e1c40607 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java @@ -0,0 +1,59 @@ +package org.baeldung.java.sorting; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class ListSort { + + private List integers; + private List employees; + + @Before + public void initData() { + integers = Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }); + + 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) }); + } + + @Test + public void naturalOrderIntegerListSort() { + + Collections.sort(integers); + + } + + @Test + public void comparableEmployeeSortByAge() { + + Collections.sort(employees); + + } + + @Test + public void comparatorEmployeeSortByName() { + + Collections.sort(employees, new Comparator() { + @Override + public int compare(Employee e1, Employee e2) { + return e1.getName().compareTo(e2.getName()); + } + }); + + } + + @Test + public void comparatorEmployeeSortByNameJava8() { + + Collections.sort(employees, (e1, e2) -> { + return e1.getName().compareTo(e1.getName()); + }); + + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java b/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java new file mode 100644 index 0000000000..1a9fd30437 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java @@ -0,0 +1,104 @@ +package org.baeldung.java.sorting; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import org.junit.Before; +import org.junit.Test; + +public class MapSort { + + HashMap map; + + @Before + public void initData() { + 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"); + + } + + @Test + public void sortMapByKeys() { + + + 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()); + } + + + } + + @Test + public void sortMapByValues() { + + //showMap(map); + + 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()); + } + +/// showMap(sortedMap); + } + + @Test + public void sortMapViaTreeMap() { + +// showMap(map); + Map treeMap = new TreeMap<>(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1 - o2; + } + }); + + treeMap.putAll(map); +// showMap(treeMap); + + } + + public void showMap(Map map1) { + for (Map.Entry entry : map1.entrySet()) { + System.out.println("[Key: " + entry.getKey() + " , " + "Value: " + entry.getValue() + "] "); + + } + + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java b/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java new file mode 100644 index 0000000000..bb35557561 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java @@ -0,0 +1,34 @@ +package org.baeldung.java.sorting; + +import java.util.Arrays; +import java.util.Comparator; + +import org.junit.Test; + +public class NaturalOrderExample { + + @Test + public void sortArray() { + int[] numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; + + Arrays.sort(numbers); + + } + + @Test + public void sortEmployees() { + 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) }; + + Arrays.sort(employees, new Comparator() { + + @Override + public int compare(Employee o1, Employee o2) { + return -(int) (o1.getSalary() - o2.getSalary()); + } + }); + + + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java b/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java new file mode 100644 index 0000000000..d892223862 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java @@ -0,0 +1,54 @@ +package org.baeldung.java.sorting; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.TreeSet; + +import org.junit.Before; +import org.junit.Test; + +public class SetSort { + + private HashSet integers; + private TreeSet employees; + + @Before + public void initData() { + + integers = new HashSet<>(); + integers.addAll(Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 })); + + employees = new TreeSet<>(); + + employees.addAll(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) })); + + } + + @Test + public void hashSetSortIntegers() { + + + + ArrayList list = new ArrayList(integers); + Collections.sort(list, (i1, i2) -> { + return i2 - i1; + }); + + + } + + @Test + public void treeSetEmployeeSorting() { + + ArrayList list = new ArrayList(employees); + Collections.sort(list, (e1, e2) -> { + return e2.getName().compareTo(e1.getName()); + }); + + } + +} From 94f08d43af8890b2a53b76c26b9a468b8f00f1d3 Mon Sep 17 00:00:00 2001 From: maverick Date: Mon, 22 Aug 2016 13:21:24 +0530 Subject: [PATCH 3/7] sort variant and parallel sort added --- .../java/org/baeldung/java/sorting/ArraySort.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java index 3e55ab7f6a..cacbcdb727 100644 --- a/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java +++ b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java @@ -84,5 +84,20 @@ public class ArraySort { }); } + + @Test + public void arraySortVariant() { + System.out.println(Arrays.toString(numbers)); + Arrays.sort(numbers, 3, 7); + System.out.println(Arrays.toString(numbers)); + + } + + @Test + public void arrayParallelSortVariant() { + + Arrays.parallelSort(numbers); + + } } From d18e64037ed837ab89b49e4e17a859ce8bb2e71e Mon Sep 17 00:00:00 2001 From: maverick Date: Wed, 31 Aug 2016 19:29:25 +0530 Subject: [PATCH 4/7] Changes for naming conventions in test functions --- .../src/test/java/org/baeldung/java/sorting/ArraySort.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java index cacbcdb727..35e6c8b46d 100644 --- a/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java +++ b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java @@ -51,7 +51,7 @@ public class ArraySort { } @Test - public void comparatorSortIntArray_Java8Lambda() { + public void givenArray_whenUsingSortWithLambdas_thenSortedArray() { Arrays.sort(integers, (a, b) -> { return a - b; }); @@ -86,7 +86,7 @@ public class ArraySort { } @Test - public void arraySortVariant() { + public void givenIntArray_whenUsingRangeSort_thenRangeSortedArray() { System.out.println(Arrays.toString(numbers)); Arrays.sort(numbers, 3, 7); System.out.println(Arrays.toString(numbers)); @@ -94,7 +94,7 @@ public class ArraySort { } @Test - public void arrayParallelSortVariant() { + public void givenIntArray_whenUsingParallelSort_thenParallelSortedArray() { Arrays.parallelSort(numbers); From 739502694fd0192a24581762e999b1ad31ea3358 Mon Sep 17 00:00:00 2001 From: maverick Date: Wed, 7 Sep 2016 09:30:34 +0530 Subject: [PATCH 5/7] md5 changes --- core-java/pom.xml | 341 +++++++++--------- .../org/baeldung/java/md5/JavaMD5Test.java | 90 +++++ 2 files changed, 265 insertions(+), 166 deletions(-) create mode 100644 core-java/src/test/java/org/baeldung/java/md5/JavaMD5Test.java diff --git a/core-java/pom.xml b/core-java/pom.xml index bc533607e7..802436e606 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,205 +1,214 @@ - - 4.0.0 - com.baeldung - core-java - 0.1.0-SNAPSHOT + + 4.0.0 + com.baeldung + core-java + 0.1.0-SNAPSHOT - core-java + core-java - + - - - net.sourceforge.collections - collections-generic - 4.01 - - - com.google.guava - guava - ${guava.version} - + + + net.sourceforge.collections + collections-generic + 4.01 + + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-collections4 - 4.0 - + + org.apache.commons + commons-collections4 + 4.0 + - - commons-io - commons-io - 2.4 - + + commons-io + commons-io + 2.4 + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - - org.apache.commons - commons-math3 - 3.3 - + + org.apache.commons + commons-math3 + 3.3 + - + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + - + - - junit - junit - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.assertj - assertj-core - ${assertj.version} - test - + + org.assertj + assertj-core + ${assertj.version} + test + - - org.testng - testng - ${testng.version} - test - + + org.testng + testng + ${testng.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.mockito + mockito-core + ${mockito.version} + test + - + + + commons-codec + commons-codec + 1.10 + - - core-java - - - src/main/resources - true - - - + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + core-java + + + src/main/resources + true + + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*IntegrationTest.java - - - + - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + - - - 4.3.11.Final - 5.1.38 + - - 2.7.2 + - - 1.7.13 - 1.1.3 + + + 4.3.11.Final + 5.1.38 - - 5.1.3.Final + + 2.7.2 - - 19.0 - 3.4 + + 1.7.13 + 1.1.3 - - 1.3 - 4.12 - 1.10.19 - 6.8 - 3.5.1 + + 5.1.3.Final - 4.4.1 - 4.5 + + 19.0 + 3.4 - 2.9.0 + + 1.3 + 4.12 + 1.10.19 + 6.8 + 3.5.1 - - 3.5.1 - 2.6 - 2.19.1 - 2.7 - 1.4.18 + 4.4.1 + 4.5 - + 2.9.0 + + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 + + \ No newline at end of file diff --git a/core-java/src/test/java/org/baeldung/java/md5/JavaMD5Test.java b/core-java/src/test/java/org/baeldung/java/md5/JavaMD5Test.java new file mode 100644 index 0000000000..83f1fb33b6 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/md5/JavaMD5Test.java @@ -0,0 +1,90 @@ +package org.baeldung.java.md5; + +import static org.junit.Assert.*; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import javax.xml.bind.DatatypeConverter; + +import org.apache.commons.codec.digest.DigestUtils; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.hash.HashCode; +import com.google.common.hash.Hashing; + +import java.io.File; +import java.io.IOException; +import java.nio.*; +import static org.assertj.core.api.Assertions.assertThat; + + +public class JavaMD5Test { + + + String filename = "src/test/resources/test_md5.txt"; + String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3"; + + String hash = "35454B055CC325EA1AF2126E27707052"; + String password = "ILoveJava"; + + + + @Test + public void givenPassword_whenHashing_thenVerifying() throws NoSuchAlgorithmException { + String hash = "35454B055CC325EA1AF2126E27707052"; + String password = "ILoveJava"; + + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(password.getBytes()); + byte[] digest = md.digest(); + String myHash = DatatypeConverter.printHexBinary(digest).toUpperCase(); + + assertThat(myHash.equals(hash)).isTrue(); + } + + @Test + public void givenFile_generatingChecksum_thenVerifying() throws NoSuchAlgorithmException, IOException { + String filename = "src/test/resources/test_md5.txt"; + String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3"; + + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(Files.readAllBytes(Paths.get(filename))); + byte[] digest = md.digest(); + String myChecksum = DatatypeConverter + .printHexBinary(digest).toUpperCase(); + + assertThat(myChecksum.equals(checksum)).isTrue(); + } + + @Test + public void givenPassword_whenHashingUsingCommons_thenVerifying() { + String hash = "35454B055CC325EA1AF2126E27707052"; + String password = "ILoveJava"; + + String md5Hex = DigestUtils + .md5Hex(password).toUpperCase(); + + assertThat(md5Hex.equals(hash)).isTrue(); + } + + + @Test + public void givenFile_whenChecksumUsingGuava_thenVerifying() throws IOException { + String filename = "src/test/resources/test_md5.txt"; + String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3"; + + HashCode hash = com.google.common.io.Files + .hash(new File(filename), Hashing.md5()); + String myChecksum = hash.toString() + .toUpperCase(); + + assertThat(myChecksum.equals(checksum)).isTrue(); + } + + +} From e0d9cd41a2e0fae80d6be324767f295517ba8fac Mon Sep 17 00:00:00 2001 From: maverick Date: Thu, 29 Sep 2016 14:57:30 +0530 Subject: [PATCH 6/7] Changes in pom.xml as suggested to remove formating changes in pom.xml commit. --- core-java/pom.xml | 347 +++++++++++++++++++++++----------------------- 1 file changed, 172 insertions(+), 175 deletions(-) diff --git a/core-java/pom.xml b/core-java/pom.xml index 802436e606..bce97d1148 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,214 +1,211 @@ - - 4.0.0 - com.baeldung - core-java - 0.1.0-SNAPSHOT + + 4.0.0 + com.baeldung + core-java + 0.1.0-SNAPSHOT - core-java + core-java - + - - - net.sourceforge.collections - collections-generic - 4.01 - - - com.google.guava - guava - ${guava.version} - + + + net.sourceforge.collections + collections-generic + 4.01 + + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-collections4 - 4.0 - + + org.apache.commons + commons-collections4 + 4.0 + - - commons-io - commons-io - 2.4 - + + commons-io + commons-io + 2.4 + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - - org.apache.commons - commons-math3 - 3.3 - + + org.apache.commons + commons-math3 + 3.3 + - + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + - + - - junit - junit - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.assertj - assertj-core - ${assertj.version} - test - + + org.assertj + assertj-core + ${assertj.version} + test + - - org.testng - testng - ${testng.version} - test - + + org.testng + testng + ${testng.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.mockito + mockito-core + ${mockito.version} + test + + + + commons-codec + commons-codec + 1.10 + - - - commons-codec - commons-codec - 1.10 - + + + core-java + + + src/main/resources + true + + - + - - core-java - - - src/main/resources - true - - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*IntegrationTest.java - - - + - + + + 4.3.11.Final + 5.1.38 - + + 2.7.2 - - - 4.3.11.Final - 5.1.38 + + 1.7.13 + 1.1.3 - - 2.7.2 + + 5.1.3.Final - - 1.7.13 - 1.1.3 + + 19.0 + 3.4 - - 5.1.3.Final + + 1.3 + 4.12 + 1.10.19 + 6.8 + 3.5.1 - - 19.0 - 3.4 + 4.4.1 + 4.5 - - 1.3 - 4.12 - 1.10.19 - 6.8 - 3.5.1 + 2.9.0 - 4.4.1 - 4.5 + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 - 2.9.0 - - - 3.5.1 - 2.6 - 2.19.1 - 2.7 - 1.4.18 - - + \ No newline at end of file From 758c5f7319765ed5b1fb352dfd6eca7d67c57162 Mon Sep 17 00:00:00 2001 From: maverick Date: Thu, 29 Sep 2016 15:06:45 +0530 Subject: [PATCH 7/7] Removed files not useful in this branch for MD5 article. --- .../java/com/baeldung/beans/Employee.java | 49 --------- .../org/baeldung/java/sorting/ArraySort.java | 103 ----------------- .../baeldung/java/sorting/ComparingTest.java | 38 ------- .../org/baeldung/java/sorting/Employee.java | 54 --------- .../org/baeldung/java/sorting/ListSort.java | 59 ---------- .../org/baeldung/java/sorting/MapSort.java | 104 ------------------ .../java/sorting/NaturalOrderExample.java | 34 ------ .../org/baeldung/java/sorting/SetSort.java | 54 --------- 8 files changed, 495 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/beans/Employee.java delete mode 100644 core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java delete mode 100644 core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java delete mode 100644 core-java/src/test/java/org/baeldung/java/sorting/Employee.java delete mode 100644 core-java/src/test/java/org/baeldung/java/sorting/ListSort.java delete mode 100644 core-java/src/test/java/org/baeldung/java/sorting/MapSort.java delete mode 100644 core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java delete mode 100644 core-java/src/test/java/org/baeldung/java/sorting/SetSort.java diff --git a/core-java/src/main/java/com/baeldung/beans/Employee.java b/core-java/src/main/java/com/baeldung/beans/Employee.java deleted file mode 100644 index 78d65e18fe..0000000000 --- a/core-java/src/main/java/com/baeldung/beans/Employee.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.baeldung.beans; - -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 int compareTo(Object o) { - Employee e = (Employee) o; - return getAge() - e.getAge() ; - } -} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java b/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java deleted file mode 100644 index 35e6c8b46d..0000000000 --- a/core-java/src/test/java/org/baeldung/java/sorting/ArraySort.java +++ /dev/null @@ -1,103 +0,0 @@ -package org.baeldung.java.sorting; - -import static org.junit.Assert.*; - -import java.sql.Array; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import org.junit.Before; -import org.junit.Test; - -public class ArraySort { - - private int[] numbers; - private Integer[] integers; - private String[] names; - private Employee[] employees; - - @Before - public void initData() { - numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; - integers = new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; - - names = new String[] { "John", "Apple", "Steve", "Frank", "Earl", "Jessica", "Jake", "Pearl" }; - - 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) }; - - } - - @Test - public void naturalSortIntArray() { - - Arrays.sort(numbers); - - } - - @Test - public void comparatorSortIntArray() { - - Arrays.sort(integers, new Comparator() { - - @Override - public int compare(Integer a, Integer b) { - return a - b; - } - }); - - } - - @Test - public void givenArray_whenUsingSortWithLambdas_thenSortedArray() { - Arrays.sort(integers, (a, b) -> { - return a - b; - }); - - } - - @Test - public void comparableSortEmployeeArrayByAge_NaturalOrder() { - - Arrays.sort(employees); - - } - - @Test - public void comparatorSortEmployeeArrayByName() { - Arrays.sort(employees, new Comparator() { - - @Override - public int compare(Employee o1, Employee o2) { - - return o1.getName().compareTo(o2.getName()); - } - }); - } - - @Test - public void comparatorSortEmployeeArrayByName_Java8Lambda() { - Arrays.sort(employees, (o1, o2) -> { - return o1.getName().compareTo(o2.getName()); - - }); - } - - @Test - public void givenIntArray_whenUsingRangeSort_thenRangeSortedArray() { - System.out.println(Arrays.toString(numbers)); - Arrays.sort(numbers, 3, 7); - System.out.println(Arrays.toString(numbers)); - - } - - @Test - public void givenIntArray_whenUsingParallelSort_thenParallelSortedArray() { - - Arrays.parallelSort(numbers); - - } - -} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java b/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java deleted file mode 100644 index 02cdede7db..0000000000 --- a/core-java/src/test/java/org/baeldung/java/sorting/ComparingTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.baeldung.java.sorting; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import org.junit.Before; -import org.junit.Test; - -public class ComparingTest { - - private List employees; - - @Before - public void initData() { - - // 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) }); - - } - - @Test - public void testComparing() { - 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) }); - - employees.sort(Comparator.comparing(Employee::getAge).thenComparing(Employee::getName)); - - } - -} 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 deleted file mode 100644 index f36e552daf..0000000000 --- a/core-java/src/test/java/org/baeldung/java/sorting/Employee.java +++ /dev/null @@ -1,54 +0,0 @@ -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 int compareTo(Object o) { - Employee e = (Employee) o; - return getAge() - e.getAge(); - } -} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java b/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java deleted file mode 100644 index 91e1c40607..0000000000 --- a/core-java/src/test/java/org/baeldung/java/sorting/ListSort.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.baeldung.java.sorting; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import org.junit.Before; -import org.junit.Test; - -public class ListSort { - - private List integers; - private List employees; - - @Before - public void initData() { - integers = Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }); - - 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) }); - } - - @Test - public void naturalOrderIntegerListSort() { - - Collections.sort(integers); - - } - - @Test - public void comparableEmployeeSortByAge() { - - Collections.sort(employees); - - } - - @Test - public void comparatorEmployeeSortByName() { - - Collections.sort(employees, new Comparator() { - @Override - public int compare(Employee e1, Employee e2) { - return e1.getName().compareTo(e2.getName()); - } - }); - - } - - @Test - public void comparatorEmployeeSortByNameJava8() { - - Collections.sort(employees, (e1, e2) -> { - return e1.getName().compareTo(e1.getName()); - }); - - } - -} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java b/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java deleted file mode 100644 index 1a9fd30437..0000000000 --- a/core-java/src/test/java/org/baeldung/java/sorting/MapSort.java +++ /dev/null @@ -1,104 +0,0 @@ -package org.baeldung.java.sorting; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TreeMap; - -import org.junit.Before; -import org.junit.Test; - -public class MapSort { - - HashMap map; - - @Before - public void initData() { - 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"); - - } - - @Test - public void sortMapByKeys() { - - - 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()); - } - - - } - - @Test - public void sortMapByValues() { - - //showMap(map); - - 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()); - } - -/// showMap(sortedMap); - } - - @Test - public void sortMapViaTreeMap() { - -// showMap(map); - Map treeMap = new TreeMap<>(new Comparator() { - @Override - public int compare(Integer o1, Integer o2) { - return o1 - o2; - } - }); - - treeMap.putAll(map); -// showMap(treeMap); - - } - - public void showMap(Map map1) { - for (Map.Entry entry : map1.entrySet()) { - System.out.println("[Key: " + entry.getKey() + " , " + "Value: " + entry.getValue() + "] "); - - } - - } - -} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java b/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java deleted file mode 100644 index bb35557561..0000000000 --- a/core-java/src/test/java/org/baeldung/java/sorting/NaturalOrderExample.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.baeldung.java.sorting; - -import java.util.Arrays; -import java.util.Comparator; - -import org.junit.Test; - -public class NaturalOrderExample { - - @Test - public void sortArray() { - int[] numbers = new int[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 }; - - Arrays.sort(numbers); - - } - - @Test - public void sortEmployees() { - 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) }; - - Arrays.sort(employees, new Comparator() { - - @Override - public int compare(Employee o1, Employee o2) { - return -(int) (o1.getSalary() - o2.getSalary()); - } - }); - - - } - -} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java b/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java deleted file mode 100644 index d892223862..0000000000 --- a/core-java/src/test/java/org/baeldung/java/sorting/SetSort.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.baeldung.java.sorting; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.TreeSet; - -import org.junit.Before; -import org.junit.Test; - -public class SetSort { - - private HashSet integers; - private TreeSet employees; - - @Before - public void initData() { - - integers = new HashSet<>(); - integers.addAll(Arrays.asList(new Integer[] { 5, 1, 89, 255, 7, 88, 200, 123, 66 })); - - employees = new TreeSet<>(); - - employees.addAll(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) })); - - } - - @Test - public void hashSetSortIntegers() { - - - - ArrayList list = new ArrayList(integers); - Collections.sort(list, (i1, i2) -> { - return i2 - i1; - }); - - - } - - @Test - public void treeSetEmployeeSorting() { - - ArrayList list = new ArrayList(employees); - Collections.sort(list, (e1, e2) -> { - return e2.getName().compareTo(e1.getName()); - }); - - } - -}