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

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

问题描述

我有一个目录 bean,其中包含以下属性:

I have a Catalog bean with the following properties:

catalog_id
catalog_name
catalog_price
catalog_title

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

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

有一个目录对象的列表,我想用相同的 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.

当我尝试添加时第二个sysout:

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

在行字符串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天全站免登陆