简体   繁体   English

使用java 8按类型列表的对象属性对对象列表进行分组:JAVA

[英]using java 8 to group list of object by object attribute of type list: JAVA

I have an Object like我有一个像

public class User {

    private String id;
    private List<String> brands_ids;
}

and I have a list of User objects like: example data我有一个用户对象列表,例如:示例数据

[
  {
    "id": 1,
    "brands_ids": [
      10,
      20,
      30
    ]
  },
  {
    "id": 2,
    "brands_ids": [
      10,
      50
    ]
  },
  {
    "id": 3,
    "brands_ids": [
      10,
      80
    ]
  }
]

My Question is, How to Group this list to know in which objects the brand id appears, for example brand id=10 appears in all three objects, the brand id=30 only in one object我的问题是,如何对该列表进行分组以了解品牌 id 出现在哪些对象中,例如品牌 id=10 出现在所有三个对象中,品牌 id=30 仅出现在一个对象中

a result of a map with key=brand id and value = count would solve my issue something like this: {10:3},{20:1},{30,1},{50,1},{80,1}具有 key=brand id 和 value = count 的地图的结果将解决我的问题,如下所示:{10:3},{20:1},{30,1},{50,1},{80,1 }

Take a look here and here看看这里这里

This code fragment create 3 users, like in your example.这个代码片段创建了 3 个用户,就像你的例子一样。

    var users = Arrays.asList(
            new User("1", Arrays.asList("10", "20", "30")),
            new User("2", Arrays.asList("10", "50")),
            new User("3", Arrays.asList("10", "80")));

    Map<String, Long> result = users
            .stream()
            .flatMap(user -> user.getBrand_ids().stream())
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

    System.out.println(result);
    // Result is: {80=1, 50=1, 30=1, 20=1, 10=3}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java Streams按属性将对象列表组合在一起,并将它们减少为具有另一个属性平均值的新对象列表 - Using Java Streams to group together a List of objects by an attribute and reduce them to a new list of object with the average of another attribute 按属性分组对象列表,并将其他剩余属性设置为不同的对象列表:Java 8流和Lambdas - Group list of object by an attribute and set other remaining attribute to different object list : Java 8 stream and Lambdas 如何使用 java stream 将 object 列表分组为 map 和 select 第一个不同类型的 object? - How to group a object list into a map and select the first object of different type for each key using java stream? Java-具有不同对象类型的列表 - Java - List with different object type 检查java列表 <object> 对象类型 - Checking java List<object> object type 使用 java 8 根据该对象的可为空的 ArrayList 属性对对象列表进行排序 - Sorting a list of objects based on a nullable ArrayList attribute of that object using java 8 使用 Java API 按唯一键将对象列表分组为对象 - Group a list of objects by Unique key as a object using Java API Java8将对象列表转换为对象的一个​​属性列表 - Java8 Transform list of object to list of one attribute of object Java - 返回列表特定属性的总和<object><div id="text_translate"><p>假设我有以下 class:</p><pre> class Transaction{ Float amount; String Id; }</pre><p> 现在我想实现一个 function:</p><pre> public static Float sum(List&lt;Object&gt; list, String attribute)</pre><p> 这将返回列表中指定属性的所有对象的总和。</p><p> 我怎样才能做到这一点?</p></div></object> - Java - Return sum for specific attribute of List<Object> 按多个字段的 object 列表分组 Java 8 - Group By List of object on multiple fields Java 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM