Stream集合流自定义排序 针对泛型小范围

Stream集合流自定义排序 针对泛型

最近项目要用到集合流排序,自己搞了个泛型的,栗子中只针对String 和 自定义类(范围比较小,以后有需要再做扩展),记录一下:

测试类如下:

package com.changgou.demo;

import com.changgou.pojo.TestUser;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/*******
 *@ClassName testSort
 *@Description
 *@Author LQQ
 *@Date 16:48 2020/5/24
 *@Version 2.1
 *******/
public class testSort {
    public static void main(String[] args) {
        List<TestUser> list = new ArrayList<>();
        list.add(new TestUser(1, "1_2", "7"));
        list.add(new TestUser(1, "2_9", "8"));
        list.add(new TestUser(1, "1", "9"));
        list.add(new TestUser(1, "2_9_3", "8"));
        list.add(new TestUser(1, "2_9", "9"));
        list = list.stream().sorted(new UndefinedComparator<>()).collect(Collectors.toList());
        System.out.println("排序后:" + list);

        List<String> list2 = new ArrayList<>();
        list2.add("2_3_1");
        list2.add("1_2");
        list2.add("2_3");
        list2.add("2");
        list2.add("2_6");
        list2 = list2.stream().sorted(new UndefinedComparator<>()).collect(Collectors.toList());
        System.out.println("2 排序后:" + list2);
    }
    public static class UndefinedComparator<T> implements Comparator<T> {
        @Override
        public int compare(T o1, T o2) {
            String str1;
            String str2;
            // 目前只针对 String 和 自定义类
            if (o1 instanceof TestUser && o2 instanceof TestUser) {
                str1 = ((TestUser) o1).getOrder();
                str2 = ((TestUser) o2).getOrder();
                if (str1.equals(str2)) {
                    str1 = ((TestUser) o1).getPart();
                    str2 = ((TestUser) o2).getPart();
                }
            } else if (o1 instanceof String && o2 instanceof String) {
                str1 = (String) o1;
                str2 = (String) o2;
            } else {
                throw new RuntimeException("排序出错");
            }

            String[] arr1 = str1.split("_", 2);
            String[] arr2 = str2.split("_", 2);
            int num1 = Integer.parseInt(arr1[0]);
            int num2 = Integer.parseInt(arr2[0]);
            if (num1 == num2) {
                str1 = arr1.length == 2 ? arr1[1] : "0";
                str2 = arr2.length == 2 ? arr2[1] : "0";
                return compare((T)str1, (T)str2);
            }
            return num1 - num2;
        }
    }
}

当然还得有自定义类

package com.changgou.pojo;

/*******
 *@ClassName TestUser
 *@Description
 *@Author LQQ
 *@Date 16:46 2020/5/24
 *@Version 2.1
 *******/
public class TestUser {
    private Integer id;
    private String order;
    private String part;

    @Override
    public String toString() {
        return "TestUser{" +
                "id=" + id +
                ", order='" + order + '\'' +
                ", part='" + part + '\'' +
                '}';
    }

    public TestUser(Integer id, String order, String part) {
        this.id = id;
        this.order = order;
        this.part = part;
    }
	//此处省略get/set方法
}

你可能感兴趣的:(笔记)