如何在不使用中间终端操作的情况下使用Java8流查找字符串中的第一个递归字符

如何在不使用中间终端操作的情况下使用Java8流查找字符串中的第一个递归字符,java,java-8,java-stream,state-monad,Java,Java 8,Java Stream,State Monad,我使用下面的示例代码只是为了说明Java8流API的问题。我不希望解决给定代码的问题,但希望解释为什么在流中不可能/不提供累加器函数 我希望在不使用中间终端操作或使用Java8流api的共享变异的情况下获取字符串中的第一个循环字符。我可以毫不费力地做到这一点扫描/累加器基本上使我能够获得流中的前一个元素,并且我能够返回我自己的类型作为扫描中的前一个元素。我看到Java 8 stream没有任何可能性,但想知道为什么?在stream中实现扫描/累加器操作有什么问题 实现这一目标的解决方法问题: 如

我使用下面的示例代码只是为了说明Java8流API的问题。我不希望解决给定代码的问题,但希望解释为什么在流中不可能/不提供累加器函数

我希望在不使用中间终端操作或使用Java8流api的共享变异的情况下获取字符串中的第一个循环字符。我可以毫不费力地做到这一点扫描/累加器基本上使我能够获得流中的前一个元素,并且我能够返回我自己的类型作为扫描中的前一个元素。我看到Java 8 stream没有任何可能性,但想知道为什么?在stream中实现扫描/累加器操作有什么问题

实现这一目标的解决方法问题:

  • 如果我使用终端操作,那么我的所有输入都会被处理,这是不必要的

  • 如果我使用变异,代码就不能并行化,或者如果有人使用stream.parallel,就会带来更多问题

  • 给定的-String str=“rtydret”

    输出-y-因为y是字符串中的第一个重复字符

    实现这一目标的必要示例:-

    List<Character> list = new ArrayList<>();
    
        for (char charecter : str.toCharArray()) {
            if (list.contains(charecter)) {
                System.out.println(charecter);
                break;
            } else {
                list.add(charecter);
            }
        }
    
    List List=new ArrayList();
    for(字符:str.toCharArray()){
    if(列表包含(字符)){
    系统输出打印LN(charecter);
    打破
    }否则{
    列表。添加(charecter);
    }
    }
    
    我也不想使用下面的代码,因为它使用终端操作,这意味着它处理了字符串中的所有字符,这很糟糕

    Map<Character, Long> collect =  "abcsdnvs".chars().mapToObj(i -> (char)i).collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
        collect.forEach( (x,y) -> System.out.println( "Key: " + x + " Val: " + y));
    
        Optional<Character> firstRepeat = collect.entrySet().stream().filter( (e) -> e.getValue() > 1).map(e -> e.getKey()).findFirst();
        System.out.println("First repeating:" + firstRepeat.orElse(null));
    
    Map collect=“abcsdnvs.chars().mapToObj(i->(char)i).collect(Collectors.groupingBy(Function.identity(),LinkedHashMap::new,Collectors.counting());
    collect.forEach((x,y)->System.out.println(“Key:+x+”Val:+y));
    可选的firstRepeat=collect.entrySet().stream().filter((e)->e.getValue()>1.map(e->e.getKey()).findFirst();
    System.out.println(“第一次重复:+firstRepeat.orElse(null));
    
    任何洞察都会大有裨益。谢谢

    我看到Java8流没有任何可能性,但想知道为什么

    因为这将使语言,特别是带有流的部分,以及映射/过滤/减少,比现在更复杂

    考虑到您可以在不使用流的情况下毫无问题地实现它,这是毫无意义的。这是在语言的复杂性和它带来或不带来的便利性之间的折衷

    我看到Java8流没有任何可能性,但想知道为什么

    因为这将使语言,特别是带有流的部分,以及映射/过滤/减少,比现在更复杂

    考虑到您可以在不使用流的情况下毫无问题地实现它,这是毫无意义的。这是在语言的复杂性和它带来或不带来的便利性之间的折衷

    我看到Java8流没有任何可能性,但想知道为什么

    我认为并行处理无法达到效果,因为除非处理所有字符,否则无法确定并行处理时哪一个先到。(因为您需要比较parallel的所有结果的索引(如果有的话),以确定哪一个先到)

    如果没有parallel ready限制(不要与此代码一起使用parallel),您可以尝试以下操作:

    Set<Integer> filter = new HashSet<>();
    OptionalInt c = str.chars().filter(e -> !filter.add(e)).findFirst();
    
    if (c.isPresent()) {
       // You can access your repeated char here
       (char) c.getAsInt();
    }
    
    Set filter=newhashset();
    Optionant c=str.chars().filter(e->!filter.add(e)).findFirst();
    if(c.isPresent()){
    //您可以在此处访问您的重复字符
    (char)c.getAsInt();
    }
    
    我看到Java8流没有任何可能性,但想知道为什么

    我认为并行处理无法达到效果,因为除非处理所有字符,否则无法确定并行处理时哪一个先到。(因为您需要比较parallel的所有结果的索引(如果有的话),以确定哪一个先到)

    如果没有parallel ready限制(不要与此代码一起使用parallel),您可以尝试以下操作:

    Set<Integer> filter = new HashSet<>();
    OptionalInt c = str.chars().filter(e -> !filter.add(e)).findFirst();
    
    if (c.isPresent()) {
       // You can access your repeated char here
       (char) c.getAsInt();
    }
    
    Set filter=newhashset();
    Optionant c=str.chars().filter(e->!filter.add(e)).findFirst();
    if(c.isPresent()){
    //您可以在此处访问您的重复字符
    (char)c.getAsInt();
    }
    
    有两种方法。第一个(我建议使用正则表达式):

    第二个是两个循环的流方式:

    int index = IntStream.range(0, input.length())
                .filter(x -> IntStream.range(x, input.length())
                        .filter(y -> input.indexOf(x) == input.indexOf(y))
                        .findAny()
                        .isPresent()
                )
                .findFirst()
                .getAsInt();
    
    System.out.println(input.charAt(index));
    
    第三个,你显然不喜欢的是:

     char result = input.chars()
                .collect(
                        () -> new LinkedHashMap<Character, Integer>(),
                        (map, x) -> map.merge((char) x, 1, (oldV, newV) -> oldV + newV),
                        (left, right) -> {
                            for (Entry<Character, Integer> e : right.entrySet()) {
                                left.merge(e.getKey(), e.getValue(), (oldV, newV) -> oldV + newV);
                            }
    
                        }
                )
                .entrySet()
                .stream()
                .filter(x -> x.getValue() > 1)
                .map(Entry::getKey)
                .findFirst()
                .orElse('?');
    
    char result=input.chars()
    .收集(
    ()->新建LinkedHashMap(),
    (map,x)->map.merge((char)x,1,(oldV,newV)->oldV+newV),
    (左,右)->{
    for(条目e:right.entrySet()){
    left.merge(e.getKey(),e.getValue(),(oldV,newV)->oldV+newV);
    }
    }
    )
    .entrySet()
    .stream()
    .filter(x->x.getValue()>1)
    .map(条目::getKey)
    .findFirst()
    .orElse(“?”);
    

    基本上和你做的一样。请注意,您正在尝试解决一个可能不需要首先解决的问题(性能问题,通过所有输入并收集到
    地图)。

    有两种方法。第一个(我建议使用正则表达式):

    第二个是两个循环的流方式:

    int index = IntStream.range(0, input.length())
                .filter(x -> IntStream.range(x, input.length())
                        .filter(y -> input.indexOf(x) == input.indexOf(y))
                        .findAny()
                        .isPresent()
                )
                .findFirst()
                .getAsInt();
    
    System.out.println(input.charAt(index));
    
    第三个,你显然不喜欢的是:

     char result = input.chars()
                .collect(
                        () -> new LinkedHashMap<Character, Integer>(),
                        (map, x) -> map.merge((char) x, 1, (oldV, newV) -> oldV + newV),
                        (left, right) -> {
                            for (Entry<Character, Integer> e : right.entrySet()) {
                                left.merge(e.getKey(), e.getValue(), (oldV, newV) -> oldV + newV);
                            }
    
                        }
                )
                .entrySet()
                .stream()
                .filter(x -> x.getValue() > 1)
                .map(Entry::getKey)
                .findFirst()
                .orElse('?');
    
    char result=input.chars()
    .收集(
    ()->新建LinkedHashMap(),
    (map,x)->map.merge((char)x,1,(oldV,newV)->oldV+newV),
    (左,钻机)