如何按Java中给定范围的SortedMap值按值分组?

时间:2019-06-09 10:59:31

标签: java treemap sortedmap

我有一个SortedMap,它具有一组学生ID,其通过结果为带有以下数据的布尔值:

1000 FALSE
1001 FALSE
1002 FALSE
1003 TRUE
1004 TRUE
1005 TRUE
1006 FALSE
1007 TRUE
1008 FALSE
1009 FALSE

我有一个带有以下变量的pojo(StudentInfo.java)

String startRange, endRange; Boolean isPass

及其各自的吸气剂和吸气剂。

我需要遍历TreeMap,并根据以下格式的布尔值将pootuID和布尔结果存储在pojo中。

startRange: 1000
endRange: 1002
isPass: FALSE

startRange: 1003
endRange: 1005
isPass: TRUE

startRange: 1006
endRange: 1006
isPass: FALSE

startRange: 1007
endRange: 1007
isPass: TRUE

startRange: 1008
sendRange: 1009
isPass: FALSE

为此,我为上述5个范围初始化了一个StudentInfo类型的新对象,将其存储为ArrayList<StudentInfo>。我已经尝试了以下逻辑,但我似乎还是不明白。

private void createStudentInfo(SortedMap<String, Boolean> studentDetails, ArrayList<StudentInfo> list) {
        StudentInfo studentInfo;
        List<String> pass = new ArrayList<String>();
        List<String> fail = new ArrayList<String>();

        for(Map.Entry<String, Boolean> entry : studentDetails.entrySet()) {
            studentInfo = new StudentInfo();

            if(!entry.getValue()) {
                fail.add(entry.getKey());
                if(pass.size() > 0) {
                    studentInfo.setStartRange(pass.get(0));
                    studentInfo.setEndRange(pass.get(pass.size()-1));
                    studentInfo.setPass(true);

                    list.add(studentInfo);

                    pass.clear();
                }
            } else {
                pass.add(entry.getKey());

                if(fail.size() > 0) {
                    studentInfo.setStartRange(fail.get(0));
                    studentInfo.setEndRange(fail.get(fail.size()-1));
                    studentInfo.setPass(false);

                    list.add(studentInfo);

                    fail.clear();
                }
            }
        }
    }

我该如何迭代并形成上述范围?请帮忙。

1 个答案:

答案 0 :(得分:1)

我测试了您发布的代码,它几乎可以正常工作。除了没有添加最后一组。

这是我测试过的改进的代码,可以正常工作:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class StudentInfo {

    private String startRange;
    private String endRange;
    private boolean isPass;

    public static void main(String[] args) {
        SortedMap<String, Boolean> studentDetails = new TreeMap<String, Boolean>();
        studentDetails.put("1000", false);
        studentDetails.put("1001", false);
        studentDetails.put("1002", false);
        studentDetails.put("1003", true);
        studentDetails.put("1004", true);
        studentDetails.put("1005", true);
        studentDetails.put("1006", false);
        studentDetails.put("1007", true);
        studentDetails.put("1008", false);
        studentDetails.put("1009", false);
        List<StudentInfo> list = new ArrayList<StudentInfo>();
        createStudentInfo(studentDetails, list);
        list.stream().forEach(System.out::println);
    }

    private static void createStudentInfo(SortedMap<String, Boolean> studentDetails, List<StudentInfo> list) {
        StudentInfo studentInfo;
        List<String> pass = new ArrayList<String>();
        List<String> fail = new ArrayList<String>();

        for (Map.Entry<String, Boolean> entry : studentDetails.entrySet()) {
            studentInfo = new StudentInfo();

            if (!entry.getValue()) {
                fail.add(entry.getKey());
                if (pass.size() > 0) {
                    studentInfo.setStartRange(pass.get(0));
                    studentInfo.setEndRange(pass.get(pass.size() - 1));
                    studentInfo.setPass(true);

                    list.add(studentInfo);

                    pass.clear();
                }
            }
            else {
                pass.add(entry.getKey());

                if (fail.size() > 0) {
                    studentInfo.setStartRange(fail.get(0));
                    studentInfo.setEndRange(fail.get(fail.size() - 1));
                    studentInfo.setPass(false);

                    list.add(studentInfo);

                    fail.clear();
                }
            }
        }
        if (!pass.isEmpty()) {
            studentInfo = new StudentInfo();
            studentInfo.setStartRange(pass.get(0));
            studentInfo.setEndRange(pass.get(pass.size() - 1));
            studentInfo.setPass(true);

            list.add(studentInfo);
        }
        else if (!fail.isEmpty()) {
            studentInfo = new StudentInfo();
            studentInfo.setStartRange(fail.get(0));
            studentInfo.setEndRange(fail.get(fail.size() - 1));
            studentInfo.setPass(false);

            list.add(studentInfo);
        }
    }

    @Override
    public String toString() {
        return "StudentInfo [startRange=" + startRange + ", endRange=" + endRange + ", isPass=" + isPass + "]";
    }

    public String getStartRange() {
        return startRange;
    }
    public void setStartRange(String startRange) {
        this.startRange = startRange;
    }

    public String getEndRange() {
        return endRange;
    }
    public void setEndRange(String endRange) {
        this.endRange = endRange;
    }

    public boolean isPass() {
        return isPass;
    }
    public void setPass(boolean isPass) {
        this.isPass = isPass;
    }
}

输出为:

StudentInfo [startRange=1000, endRange=1002, isPass=false]
StudentInfo [startRange=1003, endRange=1005, isPass=true]
StudentInfo [startRange=1006, endRange=1006, isPass=false]
StudentInfo [startRange=1007, endRange=1007, isPass=true]
StudentInfo [startRange=1008, endRange=1009, isPass=false]