【问题标题】:Make Java API Stream throwing exception使 Java API Stream 抛出异常
【发布时间】:2021-09-30 11:13:15
【问题描述】:

我有一个对象,其中包含一个属性,该属性是一个包含 2 个字符串属性的对象列表。我的目标是将此映射的键与另一个包含 String 属性的对象进行比较。

为了更清楚我的请求包含地图:

"documentType": "Document"
"fields": [
  {
    "name": "something",
    "value": "123456789"
  },
  {
    "name": "somethingElse",
    "value": "Someone"
  },
  {
    "name": "notADocumentColumn",
    "value": "shouldThrowException"
  },
  {
    "name": "notexistingcolumn",
    "value": "not a column"
  }
]

我的 sharePointDriveResponse 包含列:

"columns": [
     {
         "columnGroup": "Document",
         "description": "",
         "displayName": "something",
         "enforceUniqueValues": false,
         "hidden": false,
         "id": "8553196d-ec8d-4564-9861-xxxxxxxxxxxx",
         "indexed": false,
         "name": "FileLeafRef",
         "readOnly": false,
         "required": true
    },
    {
         "columnGroup": "Billing or Whatever",
         "description": "",
         "displayName": "notADocumentColumn",
         "enforceUniqueValues": false,
         "hidden": false,
         "id": "3a6b296c-3f50-445c-a13f-xxxxxxxxxxxx",
         "indexed": false,
         "name": "ComplianceAssetId",
         "readOnly": true,
         "required": false,
         "text": {
             "allowMultipleLines": false,
             "appendChangesToExistingText": false,
             "linesForEditing": 0,
             "maxLength": 255
         }
     },

在这个阶段,我实现了一些可以正常工作的东西:

List<String> check = sharePointDriveResponse.getColumns()
        .stream()
        .map(SharePointColumnsResponse::getDisplayName)
        .collect(Collectors.toList())
        .stream()
        .filter(request.getFields()::containsKey)
        .collect(Collectors.toList());

if(check.size() == request.getFields().size()) {
    System.out.println("It's OK !");
} else {
    throw new RequestException("One column provided doesn't exist");
}

我的问题如下:

  1. 在此处流式传输数据时是否可能引发异常?
sharePointDriveResponse.getColumns()
            .stream()
            .map(SharePointColumnsResponse::getDisplayName)
            .collect(Collectors.toList())
            .stream()
            .filter(request.getFields()::containsKey)
            .collect(Collectors.toList());
            // some operator here to throw exception 
  1. 我的第二个问题是,我还想根据请求中定义的内容类型和 SharePointDriveResponse 中的列允许的内容类型抛出异常。

在示例中:我发送了一个创建文档的请求,并传递了一个不允许用于文档但允许用于其他内容类型的字段。

"columnGroup": "Billing or Whatever",
"description": "",
"displayName": "notADocumentColumn",

我也想为这种情况抛出异常。 我也可以像以前一样重建类似的东西,但我会因为这个简单的任务而降低效率。

  1. 我还有另一种情况,即我的“columnGroup”的值应被视为所有内容类型都接受。这是基本的部分实现。
List<String> checkColumnsContentType = sharePointDriveResponse.getColumns()
        .stream()
        .map(SharePointColumnsResponse::getColumnGroup)
        .collect(Collectors.toList())
        .stream()
        .filter(request.getFields()::containsKey)
        .collect(Collectors.toList());

【问题讨论】:

  • 你为什么要.collect(toList()).stream()?这没有意义。
  • 哎呀,你是对的。我删除了 .collect(Collectors.toList()).stream() 以获得相同的结果。我该睡觉了:)

标签: java java-stream


【解决方案1】:

我找到了问题第 1 点的答案。由于写这个问题需要一些时间,所以即使在解决了这个问题之后,我也让这个问题发生了。 这是我的实现。

    sharePointDriveResponse.getColumns()
            .stream()
            .map(SharePointColumnsResponse::getDisplayName)
            .collect(toList())
            .stream()
            .filter(request.getFields()::containsKey)
            .collect(collectingAndThen(toList(), l -> {
                if (l.size() != request.getFields().size())
                    throw new RuntimeException("One column provided doesn't exist");
                return l;
            }));

【讨论】:

    猜你喜欢
    • 2012-07-23
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多