循环遍历具有相同属性的项目 (catalog_id) [英] Looping through item occurences with same property (catalog_id)

查看:43
本文介绍了循环遍历具有相同属性的项目 (catalog_id)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下属性的 Catalog bean:

I have a Catalog bean with the following properties:

catalog_id
catalog_name
catalog_price
catalog_title

catalog_id 不是唯一的,可以有多个 Catalog 对象具有相同的 id.

catalog_id is not unique, there can be multiple Catalog object with the same id.

有一个 Catalog 对象列表,我想收集所有具有相同 id 的对象,并且在第二个 for 循环中我想通过出现的次数循环那个id.

Having a list of Catalog objects, I want to collect all with the same id and in a second for loop I want to loop with through the number of occurance of that id.

假设:

catalogList2.stream()
        .collect(Collectors.groupingBy(Catalog::getId, Collectors.counting()))
        .entrySet()
        .stream()
        .map(p->"catalog id--->"+p.getKey()+"    -"+p.getValue()).forEach(System.out::println);

从上面的代码我可以得到以下输出:

From the above code I am able to get the below output:

catalog id--->17553    -- 8
catalog id--->545    -- 8
catalog id--->546    -- 6
catalog id--->40962    -- 16
catalog id--->901    -- 12

但是,在那之后我想做如下:(它只是我想要实现的结构)

But, after that I want to do like below : ( its just a structure I want to implement)

for(Catalog cat1:p.getKey()){

    for(int i=0;i<p.getValue();i++){

        System.out.println("Name -->"+cat1.get(i)));
        //Something i will do here.

    }

}

我不确定如何实现这种结构.

I am not sure how to implement this structure.

更新

我在 Eclipse 中收到消息 Lambda 表达式的参数目录无法重新声明在封闭范围内定义的另一个局部变量.

Am getting the message in my eclipse Lambda expression's parameter catalog cannot redeclare another local variable defined in an enclosing scope.

当我尝试添加第二个系统输出时:

when am trying to add 2nd sysout :

 catalogList2.stream()
    .collect(Collectors.groupingBy(Catalog::getId))
    .forEach((id, catalogs) -> {
        System.out.println("Catalog id : " + id + " : " + catalogs.size());
        catalogs.forEach(catalog -> {
            System.out.println("Name -->"+catalog.getCatalog_attr_name());
            System.out.println("Value --->"+catalog.getCatalog_attr_value());
        });
    });

更新 2

我在 Eclipse 中收到此消息 在封闭范围中定义的局部变量目录必须是最终的或有效的最终

Am getting this message in eclipse Local variable catalog defined in an enclosing scope must be final or effectively final

在行 String catalogName=catalog.getCatalog_name();//在这一行出现错误

请在这里找到我的代码;

Please find my code here ;

JSONObject jsonObj=null;
JSONArray respArray=new JSONArray();
catalogList2.stream()
.collect(Collectors.groupingBy(Catalog::getId))
.forEach((id, catalogs) -> {
    System.out.println("Catalog id : " + id + " : " + catalogs.size());
    String catalogName=catalog.getCatalog_name();  // getting error on this line
    String longDescriptionStr = catalogName.concat("_long_description");
    String descriptionStr = catalogName.concat("_description");
    jsonObj.put("catalog_id", id);
    catalogs.forEach(c -> {
        String longDescription=null;
        String description=null;
        String catalogAttrName = c.getCatalog_attr_name();
        if(StringUtils.equalsIgnoreCase(catalogAttrName, longDescriptionStr)) {
             longDescription = c.getCatalog_attr_value();
        }
        if(StringUtils.equalsAnyIgnoreCase(catalogAttrName, descriptionStr)) {
            description = c.getCatalog_attr_value();
        }
        jsonObj.put("description", description);
        jsonObj.put("longDescription", longDescription);

    });
    respArray.put(jsonObj);

});
System.out.println("Printing JSON Array --->"+respArray.toString());

推荐答案

在没有下游收集器的情况下使用 groupingBy 会将 ID 映射到具有这些 ID 的目录列表.然后,您可以询问列表的大小以获取计数并遍历列表以获取每个目录:

Using groupingBy without a downstream collector will map the IDs to lists of the catalogs with those IDs. You can then ask the lists for their size to get the count and iterate through the lists to get each catalog:

catalogList2.stream()
    .collect(Collectors.groupingBy(Catalog::getId))
    .forEach((id, catalogs) -> {
        System.out.println("Catalog id : " + id + " : " + catalogs.size());
        catalogs.forEach(System.out::println);
    });

这篇关于循环遍历具有相同属性的项目 (catalog_id)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆