如何通过分组使用 Java 对不重复的数据进行分类?

How to categorize unduplicate data using Java by grouping?

我在这里尝试按 object_type 和 object_name 对数据进行分组,然后我需要根据下面给出的输出创建 json 结构。我不确定我应该如何继续,我尝试了一些代码但失败了,如果有人可以指导我如何继续前进那将有很大帮助,在这里我想按 object_type 分组,其次通过 object_name

public class Entity {

    private String object_type;
    private String object_name;
    private String permission_type;
    private String is_enabled;
    
    
    public Entity(String object_type, String object_name, String permission_type, String is_enabled) {
        this.object_type = object_type;
        this.object_name = object_name;
        this.permission_type = permission_type;
        this.is_enabled = is_enabled;
    }

    public class ResultEntity {
        public final String object_type;
        public final List<Map.Entry<String, String>> permission_type;
        public final Set<String> object_name;
    
        public ResultEntity(String object_type, List<Map.Entry<String, String>> permission_type, Set<String> object_name) {
            this.object_type = object_type;
            this.permission_type = permission_type;
            this.object_name = object_name;
        }
    }

然后,使用您提供的示例数据,这:

public class TestJson {
    public static void main(String[] args) throws JsonProcessingException {
        List<Entity> entities = new ArrayList<>();
        entities.add(new Entity("Builder", "App Builder", "VIEW", "true"));
        entities.add(new Entity("Builder", "App Builder", "EDIT", "true"));
        entities.add(new Entity("General", "planning", "VIEW", "true"));
        entities.add(new Entity("General", "planning", "EDIT", "true"));
        entities.add(new Entity("General", "Localization", "VIEW", "true"));
        entities.add(new Entity("General ", "Localization", "EDIT", "true"));
        entities.add(new Entity("General ", "Localization", "clone", "true"));

        List<ResultEntity> res = createResult(entities);
        ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
        System.out.println(mapper.writeValueAsString(res));
    }

    private static List<ResultEntity> createResult(List<Entity> entities) {
        Map<String, List<Entity>> map = entities.stream().collect(Collectors.groupingBy(Entity::getObject_type));

        List<ResultEntity> result = map.entrySet().stream().map(entry -> {
            String objectType = entry.getKey();
            Set<String> objectNames = entry.getValue().stream().map(Entity::getObject_name).collect(Collectors.toSet());
            List<Map.Entry<String, String>> permissions = entry.getValue()
                    .stream()
                    .map(entity -> new AbstractMap.SimpleEntry<>(entity.getPermission_type(), entity.getIs_enabled()))
                    .collect(Collectors.toList());
            return new ResultEntity(objectType, permissions, objectNames);
        }).collect(Collectors.toList());
        return result;
    }
}

会产生这个:

    [ {
  "object_type" : "Builder",
  "permission_type" : [ 
    {"VIEW" : "true" }, 
    {"EDIT" : "true"}
    ],
  "object_name" : [ "App Builder" ]
}, {
  "object_type" : "General",
  "permission_type" : [ 
    {"VIEW" : "true"},
    {"EDIT" : "true" }, 
    {"VIEW" : "true" }
    ],
  "object_name" : [ "planning", "Localization" ]
}, {
  "object_type" : "General ",
  "permission_type" : [ 
    {   "EDIT" : "true" },
    {   "clone" : "true"}
    ],
  "object_name" : [ "Localization" ]
} ]

但所需的输出是这样的:

    [ {
      "object_type" : "Builder",
      "permission_type" : [ 
        { "VIEW" : "true"}, 
        {"EDIT" : "true"}
        ],
      "object_name" : [ "App Builder" ]
    }, 
{
      "object_type" : "General",
      "permission_type" : [   
        { "VIEW" : "true"}, 
        {"EDIT" : "true"} 
        ],
      "object_name" : [ "planning" ]
      }, 
{
      "object_type" : "General ",
      "permission_type" : [ 
        { "VIEW" : "true"},
        { "EDIT" : "true"}, 
        {"clone" : "true"}
        ],
      "object_name" : [ "Localization" ]
    } ]

要获得我认为您想要的 JSON 表示,其中权限是对象数组,您可以将 ResultEntry::permission_type 的类型从 Map<String, String> 更改为 List<Map.Entry<String, String>>:

public class ResultEntity {
    public final String object_type;
    public final List<Map.Entry<String, String>> permission_type;
    public final Set<String> object_name;

    public ResultEntity(String object_type, List<Map.Entry<String, String>> permission_type, Set<String> object_name) {
        this.object_type = object_type;
        this.permission_type = permission_type;
        this.object_name = object_name;
    }
}

然后,使用您提供的示例数据,这:

public class TestJson {
    public static void main(String[] args) throws JsonProcessingException {
        List<Entity> entities = new ArrayList<>();
        entities.add(new Entity("Builder", "App Builder", "VIEW", "true"));
        entities.add(new Entity("Builder", "App Builder", "EDIT", "false"));
        entities.add(new Entity("General", "planning", "VIEW", "false"));
        entities.add(new Entity("General", "planning", "EDIT", "true"));
        entities.add(new Entity("General", "Localization", "VIEW", "false"));
        entities.add(new Entity("General ", "Localization", "EDIT", "true"));
        entities.add(new Entity("General ", "Localization", "clone", "true"));

        List<ResultEntity> res = createResult(entities);
        ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
        System.out.println(mapper.writeValueAsString(res));
    }

    private static List<ResultEntity> createResult(List<Entity> entities) {
        Map<String, List<Entity>> map = entities.stream().collect(Collectors.groupingBy(Entity::getObject_type));

        List<ResultEntity> result = map.entrySet().stream().map(entry -> {
            String objectType = entry.getKey();
            Set<String> objectNames = entry.getValue().stream().map(Entity::getObject_name).collect(Collectors.toSet());
            List<Map.Entry<String, String>> permissions = entry.getValue()
                    .stream()
                    .map(entity -> new AbstractMap.SimpleEntry<>(entity.getPermission_type(), entity.getIs_enabled()))
                    .collect(Collectors.toList());
            return new ResultEntity(objectType, permissions, objectNames);
        }).collect(Collectors.toList());
        return result;
    }
}

会产生这个:

[ {
  "object_type" : "Builder",
  "permission_type" : [ {
    "VIEW" : "true"
  }, {
    "EDIT" : "false"
  } ],
  "object_name" : [ "App Builder" ]
}, {
  "object_type" : "General",
  "permission_type" : [ {
    "VIEW" : "false"
  }, {
    "EDIT" : "true"
  }, {
    "VIEW" : "false"
  } ],
  "object_name" : [ "planning", "Localization" ]
}, {
  "object_type" : "General ",
  "permission_type" : [ {
    "EDIT" : "true"
  }, {
    "clone" : "true"
  } ],
  "object_name" : [ "Localization" ]
} ]

使用此方法有帮助

private static List<ResultEntity> createResult(List<Entity> entities) {
        Map<String, List<Entity>> map = entities.stream().collect(Collectors.groupingBy(Entity::getObject_name));
        Map<String, String> objTypes = entities.stream().distinct()
                .collect(Collectors.toMap(Entity::getObject_name, Entity::getObject_type, (a1, a2) -> a1));
        List<ResultEntity> result = map.entrySet().stream().map(entry -> {
            Set<String> entObjName = new HashSet<String>();
            List<Entry<String, Boolean>> permissions = entry.getValue().stream()
                    .filter(e -> e.getObject_name().equalsIgnoreCase(entry.getKey()))
                    .map(entity -> new AbstractMap.SimpleEntry<>(entity.getPermission_type(), entity.getIs_enabled()))
                    .collect(Collectors.toList());
            entObjName.add(entry.getKey());
            return new ResultEntity(objTypes.get(entry.getKey()), permissions, entObjName );
        }).collect(Collectors.toList());
        return result;