Removed files not useful in this branch for MD5 article.
This commit is contained in:
parent
e0d9cd41a2
commit
758c5f7319
|
@ -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() ;
|
||||
}
|
||||
}
|
|
@ -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<Integer>() {
|
||||
|
||||
@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<Employee>() {
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Employee> 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));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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<Integer> integers;
|
||||
private List<Employee> 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<Employee>() {
|
||||
@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());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer, String> 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<Map.Entry<Integer, String>> entries = new ArrayList<>(map.entrySet());
|
||||
|
||||
Collections.sort(entries, new Comparator<Entry<Integer, String>>() {
|
||||
|
||||
@Override
|
||||
public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2) {
|
||||
|
||||
return o1.getKey().compareTo(o2.getKey());
|
||||
}
|
||||
});
|
||||
|
||||
HashMap<Integer, String> sortedMap = new LinkedHashMap<>();
|
||||
|
||||
for (Map.Entry<Integer, String> entry : entries) {
|
||||
sortedMap.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortMapByValues() {
|
||||
|
||||
//showMap(map);
|
||||
|
||||
List<Map.Entry<Integer, String>> entries = new ArrayList<>(map.entrySet());
|
||||
|
||||
Collections.sort(entries, new Comparator<Entry<Integer, String>>() {
|
||||
|
||||
@Override
|
||||
public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2) {
|
||||
return o1.getValue().compareTo(o2.getValue());
|
||||
}
|
||||
});
|
||||
|
||||
HashMap<Integer, String> sortedMap = new LinkedHashMap<>();
|
||||
for (Map.Entry<Integer, String> entry : entries) {
|
||||
sortedMap.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
/// showMap(sortedMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortMapViaTreeMap() {
|
||||
|
||||
// showMap(map);
|
||||
Map<Integer, String> treeMap = new TreeMap<>(new Comparator<Integer>() {
|
||||
@Override
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return o1 - o2;
|
||||
}
|
||||
});
|
||||
|
||||
treeMap.putAll(map);
|
||||
// showMap(treeMap);
|
||||
|
||||
}
|
||||
|
||||
public void showMap(Map<Integer, String> map1) {
|
||||
for (Map.Entry<Integer, String> entry : map1.entrySet()) {
|
||||
System.out.println("[Key: " + entry.getKey() + " , " + "Value: " + entry.getValue() + "] ");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Employee>() {
|
||||
|
||||
@Override
|
||||
public int compare(Employee o1, Employee o2) {
|
||||
return -(int) (o1.getSalary() - o2.getSalary());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer> integers;
|
||||
private TreeSet<Employee> 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<Integer> list = new ArrayList<Integer>(integers);
|
||||
Collections.sort(list, (i1, i2) -> {
|
||||
return i2 - i1;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void treeSetEmployeeSorting() {
|
||||
|
||||
ArrayList<Employee> list = new ArrayList<Employee>(employees);
|
||||
Collections.sort(list, (e1, e2) -> {
|
||||
return e2.getName().compareTo(e1.getName());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue