在Java8中从列表创建字符串和对象的映射

在Java8中从列表创建字符串和对象的映射,java,java-8,java-stream,Java,Java 8,Java Stream,我有以下课程: 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;

我有以下课程:

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);

映射的键是systemid和filename的组合,它们由冒号连接。我尝试通过systemid和filename的组合进行分组,但无法继续使用这种方法

您可以将
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())));
Map Map=data.stream()
.collect(Collectors.groupingBy(a->a.getSystemId()+”:“+a.getFileName())
,Collectors.mapping(a->newresult(a.getX(),a.getY()),
收藏家;

注意:这依赖于分组键
a->a.getSystemId()+“:“+a.getFileName()
的唯一性来生成正确的结果。是的,这是正确的。我使用类似的方法使用方法引用,而不是->a.getSystemId()+“:”+a.getFileName(),但它给出了编译错误。我们能用它实现这一点吗?如果不是,为什么?如果您所说的实现是指能够使用方法引用来创建分组键。您可以创建util,例如
String createGroupingKey(Data Data)
,它接受一种类型的
Data
属性,以期望的格式返回键,然后将其引用为
.groupingBy(UtilClass::createGroupingKey,…。
@Abhilash28Abhi这不是方法引用的工作方式。在本例中,方法引用代表
函数
对象,而不是方法调用的潜在结果。如果强制转换每个方法引用,可以用字符串将它们连接起来,但这仍然不能满足您的需要(例如:
((函数)字符串::格式)+“:”+((函数)字符串::修剪)
)。将其作为lambda表达式编写是一种方法。@Abhilash28Abhi简单的答案可能是
数据::getSystemId
如果您认为它是
字符串,那么它就不是
字符串。
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())));