Is there a concise way to iterate over a stream with indices in Java 8?

后端 未结 22 2721
天命终不由人
天命终不由人 2020-11-22 01:42

Is there a concise way to iterate over a stream whilst having access to the index in the stream?

String[] names = {\"Sam\",\"Pamela\", \"Dave\", \"Pascal\",          


        
22条回答
  •  我寻月下人不归
    2020-11-22 02:02

    With https://github.com/poetix/protonpack u can do that zip:

    String[] names = {"Sam","Pamela", "Dave", "Pascal", "Erik"};
    
    List nameList;
    Stream indices = IntStream.range(0, names.length).boxed(); 
    
    nameList = StreamUtils.zip(indices, stream(names),SimpleEntry::new)
            .filter(e -> e.getValue().length() <= e.getKey()).map(Entry::getValue).collect(toList());                   
    
    System.out.println(nameList);
    

提交回复
热议问题