From bf1c98fd1679b3d0b6f6ff6cd766aa98042f2b8f Mon Sep 17 00:00:00 2001 From: maverick Date: Thu, 28 Jul 2016 16:38:17 +0530 Subject: [PATCH] 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()); + }); + + } + +}