排序和去重
排序
以Java 8的stream API为例。
list String 排序
正序
List<String> list = new ArrayList<>();
list.add("bb");
list.add("aa");
list.add("cc");
List<String> collect = list.stream().sorted(Comparator.comparing(String::valueOf)).collect(Collectors.toList());
// [aa, bb, cc]
System.out.println(collect);
逆序
使用reversed()方法进行逆序排序
List<String> list = new ArrayList<>();
list.add("bb");
list.add("aa");
list.add("cc");
List<String> collect = list.stream().sorted(Comparator.comparing(String::valueOf).reversed()).collect(Collectors.toList());
// [cc, bb, aa]
System.out.println(collect);
list 对象 排序,单一属性
根据name排序
正序
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("zhangsan");
list.add(student1);
Student student2 = new Student();
student2.setName("lisi");
list.add(student2);
Student student3 = new Student();
student3.setName("wangwu");
list.add(student3);
List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getName)).collect(Collectors.toList());
System.out.println(collect);
逆序
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("zhangsan");
list.add(student1);
Student student2 = new Student();
student2.setName("lisi");
list.add(student2);
Student student3 = new Student();
student3.setName("wangwu");
list.add(student3);
List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getName).reversed()).collect(Collectors.toList());
System.out.println(collect);
扩展:中文字符串排序
工具类
@NoArgsConstructor
public final class StringUtil {
/**
* 获取字符串拼音的第一个字母
*
* @param chinese the chinese
* @return string
*/
public static String toFirstChar(String chinese) {
if (PinyinUtil.isChinese(chinese.charAt(0))) {
StringBuilder pinyinStr = new StringBuilder();
//转为单个字符
char[] newChar = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (char c : newChar) {
if (c > 128) {
try {
if (PinyinUtil.isChinese(c)) {
pinyinStr.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinStr.append(c);
}
}
return pinyinStr.toString();
} else {
return chinese;
}
}
/**
* 汉字转为拼音
*
* @param chinese the chinese
* @return string
*/
public static String toPinyin(String chinese) {
if (PinyinUtil.isChinese(chinese.charAt(0))) {
StringBuilder pinyinStr = new StringBuilder();
char[] newChar = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (char c : newChar) {
if (c > 128) {
try {
if (PinyinUtil.isChinese(c)) {
pinyinStr.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinStr.append(c);
}
}
return pinyinStr.toString();
} else {
return chinese;
}
}
}
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("张三");
list.add(student1);
Student student2 = new Student();
student2.setName("李四");
list.add(student2);
Student student3 = new Student();
student3.setName("王五");
list.add(student3);
List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getName, (x, y) -> {
// 将汉字首字母转为拼音
x = StringUtil.toFirstChar(x).toUpperCase();
y = StringUtil.toFirstChar(y).toUpperCase();
Collator clt = Collator.getInstance();
return clt.compare(x, y);
})).collect(Collectors.toList());
System.out.println(collect);
list 对象 排序,多属性
先根据名字排序,然后根据年龄排序
正序
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("zhangsan");
student1.setAge(20);
list.add(student1);
Student student2 = new Student();
student2.setName("lisi");
student2.setAge(30);
list.add(student2);
Student student3 = new Student();
student3.setName("wangwu");
student3.setAge(18);
list.add(student3);
Student student4 = new Student();
student4.setName("lisi");
student4.setAge(20);
list.add(student4);
List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getName).thenComparing(Student::getAge)).collect(Collectors.toList());
System.out.println(collect);
或者采用list的sort方法
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("zhangsan");
student1.setAge(20);
list.add(student1);
Student student2 = new Student();
student2.setName("lisi");
student2.setAge(30);
list.add(student2);
Student student3 = new Student();
student3.setName("wangwu");
student3.setAge(18);
list.add(student3);
Student student4 = new Student();
student4.setName("lisi");
student4.setAge(20);
list.add(student4);
list.sort((o1, o2) -> {
if (o1.getName().compareTo(o2.getName()) > 0) {
return 1;
} else if (o1.getName().compareTo(o2.getName()) == 0) {
if (o1.getAge().compareTo(o2.getAge()) > 0) {
return 1;
}
}
return -1;
});
System.out.println(list);
逆序
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("zhangsan");
student1.setAge(20);
list.add(student1);
Student student2 = new Student();
student2.setName("lisi");
student2.setAge(30);
list.add(student2);
Student student3 = new Student();
student3.setName("wangwu");
student3.setAge(18);
list.add(student3);
Student student4 = new Student();
student4.setName("lisi");
student4.setAge(20);
list.add(student4);
List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getName).thenComparing(Student::getAge).reversed()).collect(Collectors.toList());
System.out.println(collect);
或者采用list的sort方法
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("zhangsan");
student1.setAge(20);
list.add(student1);
Student student2 = new Student();
student2.setName("lisi");
student2.setAge(30);
list.add(student2);
Student student3 = new Student();
student3.setName("wangwu");
student3.setAge(18);
list.add(student3);
Student student4 = new Student();
student4.setName("lisi");
student4.setAge(20);
list.add(student4);
list.sort((o1, o2) -> {
if (o1.getName().compareTo(o2.getName()) < 0) {
return 1;
} else if (o1.getName().compareTo(o2.getName()) == 0) {
if (o1.getAge().compareTo(o2.getAge()) < 0) {
return 1;
}
}
return -1;
});
System.out.println(list);
去重
list String 去重
List<String> list = new ArrayList<>();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
list.add("zhangsan");
List<String> collect = list.stream().distinct().collect(Collectors.toList());
System.out.println(collect);
利用set去重
List<String> list = new ArrayList<>();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
list.add("zhangsan");
Set<String> set = new HashSet<>(list);
System.out.println(set);
list 对象 去重,单一属性
根据名字
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("zhangsan");
student1.setAge(20);
list.add(student1);
Student student2 = new Student();
student2.setName("lisi");
student2.setAge(30);
list.add(student2);
Student student3 = new Student();
student3.setName("wangwu");
student3.setAge(18);
list.add(student3);
Student student4 = new Student();
student4.setName("lisi");
student4.setAge(20);
list.add(student4);
List<Student> collection = list.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(Student::getName))), ArrayList::new));
System.out.println(collection);
list 对象 去重,多属性
根据名字和年龄
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setName("zhangsan");
student1.setAge(20);
list.add(student1);
Student student2 = new Student();
student2.setName("lisi");
student2.setAge(30);
list.add(student2);
Student student3 = new Student();
student3.setName("wangwu");
student3.setAge(18);
list.add(student3);
Student student4 = new Student();
student4.setName("lisi");
student4.setAge(20);
list.add(student4);
List<Student> collection = list.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(o -> o.getName() + ";" + o.getAge() + ";"))), ArrayList::new));
System.out.println(collection);