基于对象内的String数组对List <object>进行排序

时间:2017-08-14 12:28:35

标签: java sorting

我有一个对象列表,需要根据里面的String变量进行排序。问题是这个字符串是csv样式,所以我需要拆分它然后对它进行排序。该字符串包含一周中的几天,并且整个列表需要排序,考虑到今天是第一个,然后是后续的几天。我已经实现了我粘贴在下面的代码并且它可以工作,但我不确定它是否是最有效的方法。非常感谢您的帮助。

public class Student {
    private String studentDays;
    private String name;
    private int sortPriority;

    public String getStudentDays() {
        return studentDays;
    }

    public void setStudentDays(String studentDays) {
        this.studentDays = studentDays;
    }

    public int getSortPriority() {
        return sortPriority;
    }

    public void setSortPriority(int sortPriority) {
        this.sortPriority = sortPriority;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

这就是我实施的那种

public class SortNew {
    private List<Student> students;
    private HashMap<String, Integer> dayPriorityMap = new HashMap<String, Integer>();

    public SortNew(List<Student> students) {
        this.students = students;
        generateDayMap();
    }

    public void sort() {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getStudentDays() != null) {
                List<String> studentBatchDays = Arrays.asList(students.get(i).getStudentDays().split(","));
                if (studentBatchDays.contains("Mon")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Mon"));
                } else if (studentBatchDays.contains("Tue")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Tue"));
                } else if (studentBatchDays.contains("Wed")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Wed"));
                } else if (studentBatchDays.contains("Thu")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Thu"));
                } else if (studentBatchDays.contains("Fri")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Fri"));
                } else if (studentBatchDays.contains("Sat")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Sat"));
                } else if (studentBatchDays.contains("Sun")) {
                    students.get(i).setSortPriority(dayPriorityMap.get("Sun"));
                }
            }
        }

        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return Integer.compare(o1.getSortPriority(), o2.getSortPriority());
            }

        });

        for (Student student : students) {
            System.out.println(student.getName());
        }
        System.out.println(dayPriorityMap.toString());
    }

    public void generateDayMap() {
        Calendar cal = Calendar.getInstance();
        for (int i = 1; i <= 7; i++) {
            if (i > 1) {
                cal.add(Calendar.DAY_OF_WEEK, 1);
            }
            int t = cal.get(Calendar.DAY_OF_WEEK);
            String day = getDay(t);
            dayPriorityMap.put(day, i);
        }
    }

    public String getDay(int day) {
        switch (day) {
        case Calendar.SUNDAY:
            return "Sun";
        case Calendar.MONDAY:
            return "Mon";
        case Calendar.TUESDAY:
            return "Tue";
        case Calendar.WEDNESDAY:
            return "Wed";
        case Calendar.THURSDAY:
            return "Thu";
        case Calendar.FRIDAY:
            return "Fri";
        case Calendar.SATURDAY:
            return "Sat";
        }
        return null;
    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();

        Student student = new Student();
        student.setName("One");
        student.setSortPriority(10);
        student.setStudentDays("Sun,Tue,Wed");
        students.add(student);

        Student student1 = new Student();
        student1.setName("Two");
        student1.setSortPriority(10);
        student1.setStudentDays("Sat,Thu,Sun");
        students.add(student1);

        Student student2 = new Student();
        student2.setName("Three");
        student2.setSortPriority(10);
        students.add(student2);

        Student student3 = new Student();
        student3.setName("Four");
        student3.setSortPriority(10);
        student3.setStudentDays("Fri,Sun,Sat");
        students.add(student3);

        SortNew s = new SortNew(students);
        s.sort();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Java 8功能样式功能,以避免重复编码模式:

if (studentBatchDays.contains("Mon")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Mon"));
} else if (studentBatchDays.contains("Tue")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Tue"));
} else if (studentBatchDays.contains("Wed")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Wed"));
} else if (studentBatchDays.contains("Thu")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Thu"));
} else if (studentBatchDays.contains("Fri")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Fri"));
} else if (studentBatchDays.contains("Sat")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Sat"));
} else if (studentBatchDays.contains("Sun")) {
    students.get(i).setSortPriority(dayPriorityMap.get("Sun"));
}

您可以将消费者而不是值放入 dayPriorityMap 。然后迭代工作日并在每个工作日使用消费者。

这些方面的东西:

import java.util.*;
import java.util.function.Consumer;

public class SortNew {
    private List<Student> students;
    private Map<String, Consumer<Integer>> dayPriorityMap = new HashMap<>();

    public SortNew(List<Student> students) {
        this.students = students;
        generateDayMap();
    }

    public void sort() {
        List<String> weekDays = Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getStudentDays() != null) {
                List<String> studentBatchDays = Arrays.asList(students.get(i).getStudentDays().split(","));
                final int studentIndex = i;
                weekDays.stream().filter(studentBatchDays::contains)
                        .findFirst()
                        .ifPresent(weekday -> dayPriorityMap.get(weekday).accept(studentIndex));
            }
        }

        students.sort(Comparator.comparingInt(Student::getSortPriority));

        for (Student student : students) {
            System.out.println(student.getName());
        }
    }

    public void generateDayMap() {
        Calendar cal = Calendar.getInstance();
        for (int i = 1; i <= 7; i++) {
            if (i > 1) {
                cal.add(Calendar.DAY_OF_WEEK, 1);
            }
            int t = cal.get(Calendar.DAY_OF_WEEK);
            String day = getDay(t);
            final int sortPrio = i;
            dayPriorityMap.put(day, (in) -> students.get(in).setSortPriority(sortPrio));
        }
    }

    private String getDay(int day) {
        switch (day) {
            case Calendar.SUNDAY:
                return "Sun";
            case Calendar.MONDAY:
                return "Mon";
            case Calendar.TUESDAY:
                return "Tue";
            case Calendar.WEDNESDAY:
                return "Wed";
            case Calendar.THURSDAY:
                return "Thu";
            case Calendar.FRIDAY:
                return "Fri";
            case Calendar.SATURDAY:
                return "Sat";
        }
        return null;
    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();

        Student student = new Student();
        student.setName("One");
        student.setSortPriority(10);
        student.setStudentDays("Sun,Tue,Wed");
        students.add(student);

        Student student1 = new Student();
        student1.setName("Two");
        student1.setSortPriority(10);
        student1.setStudentDays("Sat,Thu,Sun");
        students.add(student1);

        Student student2 = new Student();
        student2.setName("Three");
        student2.setSortPriority(10);
        students.add(student2);

        Student student3 = new Student();
        student3.setName("Four");
        student3.setSortPriority(10);
        student3.setStudentDays("Fri,Sun,Sat");
        students.add(student3);

        SortNew s = new SortNew(students);
        s.sort();
    }
}