简体   繁体   English

这是java.util.stream的正确使用吗?

[英]Is this the right use of java.util.stream?

Update 更新资料

Ok, I think I know how to work with streams now. 好的,我想我现在知道如何使用流。 The code in the old post is a mess and I'm not proud of it. 旧帖子中的代码一团糟,我对此并不感到骄傲。 So, thank you for you help for directing me in the right direction. 因此,感谢您为我提供正确指导的帮助。 I wrote a supplier class , which provides me with items and used static filters and mapper functions: 我编写了一个Supplier类 ,它为我提供了项目,并使用了静态过滤器mapper函数:

final TradeSupplier tradeSupplier = new TradeSupplier();
Stream.generate(tradeSupplier).map(TradeSupplier::getPrice)
        .map(TradeSupplier::getTradePartner)
        .map(TradeSupplier::getTradeInfo)
        .filter(TradeSupplier::validateInfo)
        .map(TradeSupplier::getPartnerAssetId)
        .filter(TradeSupplier::validatePartnerAssetId).forEach(t -> {
            if (trade.sendTrade(t)) {
                tradeSupplier.finishedItem();
                TradeCache.save(t);
            }
        });

With this design, I don't need flatMap , because it's just an one by one mapping. 通过这种设计,我不需要flatMap ,因为它只是一对一的映射。 Additional information is filed into the item, which is just in the stream I hope, this code is better than the code below... What do you think? 我希望在流中添加其他信息,该代码比下面的代码要好...您如何看待? I'm appreciative for any help to improve my understanding of streams :) 我非常感谢您对我对信息流的理解,:)


Old post 旧帖子

I'm looking for help for the "new" stream api of java 8: first I get a list of items, for every item I collect a list of strings and after that, i want to combine the string with their corresponding item: 我正在寻找Java 8的“新”流api的帮助:首先,我获得了一个项目列表,对于每个项目,我都会收集一个字符串列表,然后,我想将字符串与相应的项目组合起来:

input: 输入:

  • item1 项目1
  • item2 item2

wanted output: 想要的输出:

  • item1; item1; string1 字符串1
  • item1; item1; string2 字符串2
  • item2; item2; string1 字符串1
  • item2; item2; string2 字符串2
  • item2; item2; string3 字符串3

Is the following code the right way to use this api? 以下代码是使用此API的正确方法吗?

Code (with stream api) 代码(使用流API)

// input is a list of items
analyst.getInRange(wantMinValue, wantMaxValue)
        .stream()
        .filter(i -> !haveItem.getName().contains(i.getName())
                || (!haveItem.getModel().contains(i.getModel()) && haveItem
                        .getQuality() > i.getQuality()))
// get extra information and add it to a list (key: item; value: string) 
        .collect(Collectors.toMap(s -> s, s -> lounge.getItemId(s)))
        .entrySet()
        .stream()
// delete all null and empty strings 
        .filter(e -> e.getValue() != null && !e.getValue().isEmpty())
// for every entry in list, create an list and add to result
        .forEach(
                e -> {
                    lounge.getListOfValue(e.getValue(), 1)
                            .stream()
                            .filter(s -> s != null && !s.isEmpty())
                            .map(s -> lounge.getStringFromOldString(s))
                            .filter(s -> s != null && !s.isEmpty())
                            .collect(
                                    Collectors
                                            .toCollection(HashSet::new))
// add list to resulting list
                            .forEach(
                                    s -> {
                                        result.add(new TradeLink(s,
                                                haveItem, e.getKey()));
                                    });

                });

First thing: .filter(s -> s != null && !s.isEmpty()) 第一件事: .filter(s -> s != null && !s.isEmpty())

Don't include these things unless these are actually things that can happen. 除非这些实际上是可能发生的,否则不要包括这些东西 Are empty strings or null strings actually going to come up in your application? 您的应用程序中实际上会出现空字符串还是空字符串? (If so, that probably reflects a design flaw in the first place. It may be better to let your application crash, because nulls generally shouldn't be in your program in ways like this.) (如果是这样,那可能首先反映出设计缺陷。让您的应用程序崩溃可能会更好,因为通常不应该以这种方式将null放入程序中。)

Second: don't do the mutable thing you're doing here: 第二:不要在这里做任何可变的事情:

      .forEach(
            e -> {
                lounge.getListOfValue(e.getValue(), 1)
                        .stream()
                        .filter(s -> s != null && !s.isEmpty())
                        .map(s -> lounge.getStringFromOldString(s))
                        .filter(s -> s != null && !s.isEmpty())
                        .collect(
                                Collectors
                                        .toCollection(HashSet::new))
                        // add list to resulting list
                        .forEach(
                                s -> {
                                    result.add(new TradeLink(s,
                                            haveItem, e.getKey()));
                                });

            });

Instead, do something like: 相反,请执行以下操作:

  .flatMap(e ->
                lounge.getListOfValue(e.getValue(), 1)
                        .stream()
                        .map(lounge::getStringFromOldString)
                        .distinct()
                        .map(s -> new TradeLink(s, haveItem, e.getKey()))
  .collect(toList())

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM