1

Есть карта:

Map<String, Object> columns = new HashMap<>();

Она создается в стриме, и наполняется там же примерно так:

columns.put("description", inaccessibleError.getDescription());
columns.put("errorPriority", inaccessibleError.getPriority());
columns.put("agentId", agentId);
columns.put("systemType", agentType.getDisplayName());
columns.put("fqdn", agentFqdn);
columns.put("errorMessage", p.errorMessage);
columns.put("path", path);
columns.put("errorTime", errorDateTime);
columns.put("errorCount", errorCount);
columns.put("notShownErrorCount", errorCount <= limitByPath ? 0 : errorCount - limitByPath);
columns.put("foldersTotalErrorCount", foldersTotalErrorCount[0]);
columns.put("filesTotalErrorCount", filesTotalErrorCount[0]);
return columns;

Собственно вопрос: мне надо возвращать только ту map, у которой ключ "errorPriority" равен заранее заданной переменной. inaccessibleError.getPriority() - стринг. Как это лучше реализовать?

1 ответ 1

1

Вот так можно

package test;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {

  private final HashMap<String, Service> modes = new HashMap<>();

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

    for (int i = 0; i < 10; i++) {

      Map<String, Object> columns = new HashMap();

      columns.put("description", "inaccessibleError.getDescription()");
      columns.put("errorPriority", i % 2 == 0 ? "inaccessibleError.getPriority()" : "other");
      columns.put("agentId", i);
      columns.put("systemType", "agentType.getDisplayName()");
      columns.put("fqdn", "agentFqdn");
      columns.put("errorMessage", "p.errorMessage");
      columns.put("path", "path");
      columns.put("errorTime", "errorDateTime");
      columns.put("errorCount", "errorCount");
      columns.put("notShownErrorCount", "errorCount <= limitByPath ? 0 : errorCount - limitByPath");
      columns.put("foldersTotalErrorCount", "foldersTotalErrorCount[0]");
      columns.put("filesTotalErrorCount", "filesTotalErrorCount[0]");

      listOfMap.add(columns);
    }

    List result =
        listOfMap.stream()
            .filter(map -> map.get("errorPriority").equals("inaccessibleError.getPriority()"))
            .collect(Collectors.toList());

    result.forEach(System.out::println);
  }
}

и ответ

{path=path, errorTime=errorDateTime, foldersTotalErrorCount=foldersTotalErrorCount[0], agentId=0, errorPriority=inaccessibleError.getPriority(), fqdn=agentFqdn, notShownErrorCount=errorCount <= limitByPath ? 0 : errorCount - limitByPath, filesTotalErrorCount=filesTotalErrorCount[0], systemType=agentType.getDisplayName(), errorMessage=p.errorMessage, description=inaccessibleError.getDescription(), errorCount=errorCount}
{path=path, errorTime=errorDateTime, foldersTotalErrorCount=foldersTotalErrorCount[0], agentId=2, errorPriority=inaccessibleError.getPriority(), fqdn=agentFqdn, notShownErrorCount=errorCount <= limitByPath ? 0 : errorCount - limitByPath, filesTotalErrorCount=filesTotalErrorCount[0], systemType=agentType.getDisplayName(), errorMessage=p.errorMessage, description=inaccessibleError.getDescription(), errorCount=errorCount}
{path=path, errorTime=errorDateTime, foldersTotalErrorCount=foldersTotalErrorCount[0], agentId=4, errorPriority=inaccessibleError.getPriority(), fqdn=agentFqdn, notShownErrorCount=errorCount <= limitByPath ? 0 : errorCount - limitByPath, filesTotalErrorCount=filesTotalErrorCount[0], systemType=agentType.getDisplayName(), errorMessage=p.errorMessage, description=inaccessibleError.getDescription(), errorCount=errorCount}
{path=path, errorTime=errorDateTime, foldersTotalErrorCount=foldersTotalErrorCount[0], agentId=6, errorPriority=inaccessibleError.getPriority(), fqdn=agentFqdn, notShownErrorCount=errorCount <= limitByPath ? 0 : errorCount - limitByPath, filesTotalErrorCount=filesTotalErrorCount[0], systemType=agentType.getDisplayName(), errorMessage=p.errorMessage, description=inaccessibleError.getDescription(), errorCount=errorCount}
{path=path, errorTime=errorDateTime, foldersTotalErrorCount=foldersTotalErrorCount[0], agentId=8, errorPriority=inaccessibleError.getPriority(), fqdn=agentFqdn, notShownErrorCount=errorCount <= limitByPath ? 0 : errorCount - limitByPath, filesTotalErrorCount=filesTotalErrorCount[0], systemType=agentType.getDisplayName(), errorMessage=p.errorMessage, description=inaccessibleError.getDescription(), errorCount=errorCount}

Естественно в вашем случае фильтер будет таким

.filter(map -> map.get("errorPriority").equals(inaccessibleError.getPriority()))
1
  • 1
    Дружище, я сидел с этим больше 3 часов. Глаза замылились. Спасибо тебе просто огромное!)) От всего сердца)
    – Arty Mart
    16 июн 2021 в 10:31

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.