简体   繁体   English

如何在Java 8流中执行条件操作以跳过下一个流操作?

[英]How to do conditional operation in java 8 stream to skip next stream operation?

In Traditional way I have written this simple java for loop 我以传统方式编写了这个简单的java for循环

public String setString(String string) {  
StringBuilder stringBuilder = new StringBuilder();

    // This for loop I want to convert into stream. 
    for (char c : string.toCharArray()) {
       if (Character.isSpaceChar(c))
           stringBuilder.append(c);
       else if (isBangla(c))
           stringBuilder.append(get(c));
    }
    return stringBuilder.toString();
}

String get(int c) {
    return keyMap.getMap((char) c); // Map<Character, String> keyMap
}

boolean isBangla(int codePoint) {
    return ((codePoint >= BANGLA_CHAR_START && codePoint <= BANGLA_CHAR_END) &&
        (codePoint <= BANGLA_NUMBER_START || codePoint >= BANGLA_NUMBER_END)));
}

I have tried to something like this. 我已经尝试过这样的事情。

String str = string.chars()
            .filter(i -> Character.isSpaceChar(i)) // if this is true need to add string 
            .filter(this::isBangla)
            .mapToObj(this::get)
            .collect(Collectors.joining());

What I am trying to do is if the first filter detect it is a spaceChar then it will not do any further stream processing rather it jump into the collect . 我要尝试做的是,如果第一个过滤器检测到它是一个spaceChar那么它将不做任何进一步的流处理,而是跳入collect

So how could I achieve this kind of if/else situation to skip next stream operation ? 那么我如何才能实现这种if/else情况以跳过下一个流操作呢? Thanks in advance. 提前致谢。

Well, you could probably use something like this: 好吧,您可能会使用以下内容:

String str = string.chars()
           .filter( ch -> Character.isSpaceChar(ch) || isBangla(ch) )
           .mapToObj( ch -> isBangla(ch) ? get(ch) : Character.toString((char)ch))
           .collect(Collectors.joining());

As you can see, this stream checks isBangla twice. 如您所见,此流两次检查isBangla The first time is to leave only characters which are either space or bangla in the stream, the second one in order to know which of them to convert. 第一次是在流中仅保留空格或孟加拉字符,第二次是为了知道要转换哪个字符。

Because stream operations are independent - each stream doesn't know what happened in the stream before it - such operations may be more efficient when written in a traditional loop. 由于流操作是独立的-每个流之前都不知道流中发生了什么-在传统循环中编写时,这样的操作可能会更有效。 What's more, you are creating a lot of little strings when you run this in a stream, where you have to convert all characters into strings, not just the bangla ones. 此外,在流中运行时,您正在创建很多小的字符串,在其中您必须将所有字符转换为字符串,而不仅仅是孟加拉字符。

It's important to note that you're not "skipping" anything here. 重要的是要注意,您这里没有“跳过”任何内容。 In a traditional loop, you can skip operations . 在传统循环中,您可以跳过操作 But in a stream, skipping elements means that they will not be in the final result. 但是在流中,跳过元素意味着它们将不在最终结果中。 What you do in a stream is pass those elements unchanged (or in this case, changed from a char to a String ), not skip. 您在流中要做的是不更改地传递这些元素(或在这种情况下,从char更改为String ),而不跳过。

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

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