示例代码
package sortandsearch;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* 此类用于排序和搜索算法
*/
public class SortAndSearch {
/**
* 对int[]进行排序
*
* @param array int[]
* @return int[]
*/
public static int[] sortByArrsys(int[] array) {
Arrays.sort(array);
return array;
}
/**
* 对int[] 进行查找
*
* @param array int[]
* @param num 查找第几个数
* @return int 第几个数
*/
public static int searchByArrsys(int[] array, int num) {
return Arrays.binarySearch(array, num);
}
/**
* 对List<Interger>进行排序
*
* @param list List
* @return List<Integer>
*/
public static List<Integer> sortByList(List<Integer> list) {
Collections.sort(list);
return list;
}
/**
* 按照Object某一参数进行排序
*
* @param students 某一对象
* @return 排序后的
*/
public static List<Student> sortByObject1(List<Student> students) {
// 按照年龄进行排序
students.sort((st1, st2) -> st1.getAge() - st2.getAge());
return students;
}
/**
* 按照Object多个参数进行排序
*
* @param students 某一对象
* @return 排序后的
*/
public static List<Student> sortByObject2(List<Student> students) {
// 按照年龄进行排序,如果年龄相同,按照工号
students.sort((st1, st2) -> {
if (st1.getAge() - st2.getAge() == 0) {
return st1.getId() - st2.getId();
}
return st1.getAge() - st2.getAge();
});
return students;
}
/**
* 按照Object的字段字符串进行排序
*
* @param students 某一对象
* @return 排序后的
*/
public static List<Student> sortByObject3(List<Student> students) {
// 按照姓名进行排序
students.sort((st1, st2) -> st1.getName().hashCode() - st2.getName().hashCode());
return students;
}
}
Student类
package sortandsearch;
public class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试代码
package sortandsearch;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class SortAndSearchTest {
@Test
public void sortByArrsys_Success() {
int[] array = {2, 5, -2, 6, -3, 8, 0, -7, -9, 4};
int[] actual = SortAndSearch.sortByArrsys(array);
// 排序打印
System.out.println("排序后的顺序为:");
for (int a : array) {
System.out.println(a);
}
int[] expected = {-9, -7, -3, -2, 0, 2, 4, 5, 6, 8};
Assert.assertArrayEquals(expected, actual);
}
@Test
public void searchByArrsys_Success() {
int[] array = {-9, -7, -3, -2, 0, 2, 4, 5, 6, 8};
int actual = SortAndSearch.searchByArrsys(array, 2);
//查找结果打印
System.out.println("元素 2 在第" + actual + "位置");
int expected = 5;
Assert.assertEquals(expected, actual);
}
@Test
public void searchByObject_Success() {
Student student1 = new Student(1, "name1", 18);
Student student2 = new Student(2, "name2", 19);
Student student3 = new Student(5, "name3", 17);
Student student4 = new Student(4, "name3", 17);
List<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
List<Student> actual = SortAndSearch.sortByObject1(students);
//查找结果打印
for (Student student : actual) {
System.out.println(student);
}
}
@Test
public void searchByObject2_Success() {
Student student1 = new Student(1, "name1", 18);
Student student2 = new Student(2, "name2", 19);
Student student3 = new Student(5, "name3", 17);
Student student4 = new Student(4, "name3", 17);
List<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
List<Student> actual = SortAndSearch.sortByObject2(students);
//查找结果打印
for (Student student : actual) {
System.out.println(student);
}
}
@Test
public void searchByObject3_Success() {
Student student1 = new Student(1, "name1", 18);
Student student2 = new Student(2, "name2", 19);
Student student3 = new Student(5, "name3", 17);
Student student4 = new Student(4, "aname3", 17);
List<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
List<Student> actual = SortAndSearch.sortByObject3(students);
//查找结果打印
for (Student student : actual) {
System.out.println(student);
}
}
}