자바에있는 목록에서 문자열의지도 및 개체 만들기 (8)

Abhilash :

나는 다음과 같은 클래스가 :

class Data {
        String systemId;
        String fileName;
        int x;
        int y;

        Data(String systemId, String fileName, int x, int y) {
            this.systemId = systemId;
            this.fileName = fileName;
            this.x = x;
            this.y = y;
        }
        public String getSystemId() {
            return systemId;
        }

        public void setSystemId(String systemId) {
            this.systemId = systemId;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }


class Result {
        int x;
        int y;

        Result(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }

List<Data> dataList = new ArrayList<>();
Data x1 = new Data("n1", "f1", 1, 2);
Data x2 = new Data("n1", "f1", 3, 4);
Data x3 = new Data("n1", "f1", 5, 6);
Data x4 = new Data("n1", "f2", 7, 8);
Data x5 = new Data("n2", "f1", 9, 10);
Data x6 = new Data("n2", "f2", 11, 12);
Data x7 = new Data("n3", "f1", 13, 14);
Data x8 = new Data("n4", "f1", 15, 16);

dataList.add(x1);dataList.add(x2);dataList.add(x3);dataList.add(x4);dataList.add(x5);dataList.add(x6);dataList.add(x7);dataList.add(x8);

나는 자바가 만들 스트림을 사용하려면 Map<String, List<Result>>지정된 입력 목록의 아웃. 또한,리스트의 값이 요구 필드에 따라 오름차순으로 정렬 (X 및 Y)

나는 다음과 같이 될 출력지도가 필요합니다 :

{"n1:f1" : [(1, 2), (3, 4), (5, 6)]
 "n1:f2" : [(7, 8)]
 "n2:f1" : [(9, 10)]
 "n2:f2" : [(11, 12)]
 "n3:f1" : [(13, 14)]
 "n4:f1" : [(15, 16)]
}

맵의 키는 콜론으로 연결된 SYSTEMID와 파일 이름의 조합입니다. 나는 SYSTEMID와 파일 이름의 조합에 의해 그룹화 시도했지만 그 방법을 진행 할 수 없습니다.

또한 :

당신은 사용할 수 groupingBy와 컬렉터를 mapping하류로 :

Map<String, List<Result>> map = data.stream()
        .collect(Collectors.groupingBy(a -> a.getSystemId() + ":" + a.getFileName()
                , Collectors.mapping(a -> new Result(a.getX(), a.getY()),
                        Collectors.toList())));

추천

출처http://43.154.161.224:23101/article/api/json?id=231782&siteId=1