每个和第一个索引的Java 8

时间:2017-03-24 09:53:21

标签: java for-loop foreach java-8

任何人都知道如何以Java 8方式实现以下片段代码是否有任何流方法来检测forEach中的第一个元素?

List<String> myList = new ArrayList<String>();
myList.add("A");
myList.add("B");

int i = 0;

for (final String value : myList) {
   if (i == 0) {
     System.out.println("Hey that's the first element");
   }

   System.out.println(value);

   i++;
 }

Java8:

myList.stream().forEach(value -> {
  // TODO: How to do something special for first element?

  System.out.println(value);
});

此外,让我们说目标是以下(控制台输出):

A    Something special
B
C
D
E
F

6 个答案:

答案 0 :(得分:3)

愿这对你有用吗?

myList.stream().findFirst().ifPresent(e -> System.out.println("Special: " + e));
myList.stream().skip(1).forEach(System.out::println);

输出:

Special: A
B

替代:

myList.stream().findFirst().ifPresent(e -> somethingSpecial(e));
myList.stream().forEach(System.out::println);

答案 1 :(得分:1)

我认为流中没有提供的东西。

您可以考虑以下几种方法:

方法1:

int[] i = {0};  // I am using it only as an int holder.
myList.stream().forEach(value -> {
  if (i[0]++ == 0) {
    doSomethingSpecial;
  }
  System.out.println(value);
});

方法2:

如果您的列表可以快速进行随机访问(例如ArrayList),您可以考虑:

IntStream.range(0, myList.size())
  .forEach(i -> {
    ValueType value = myList.get(i);
    if (i == 0) {
      doSomethingSpecial();
    }
    System.out.println(value);
  });

答案 2 :(得分:1)

尝试StreamEx

StreamEx.of("A", "B", "C") //
        .peekFirst(e -> System.out.println(e + " Something special")).skip(1) //
        .forEach(System.out::println);

答案 3 :(得分:0)

没有理由不使用简单的for循环,foreach很漂亮,但仅限于副作用。

答案 4 :(得分:0)

这样的东西
    List<String> mylist = Arrays.asList("A", "B", "C", "D", "E", "F");
    Optional<String> findFirst = mylist.stream().findFirst();
    findFirst.ifPresent(i -> System.out.println("Hey! " + i + ", that's the first element"));
    mylist.stream().skip(1).forEach(System.out::println);

答案 5 :(得分:0)

如果您尝试在单个流语句中实现它,那么我将按照以下方法进行操作:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class Solution {
    public static void main(String[] args) {
        List<String> myList = new ArrayList<>();
        myList.add("A");
        myList.add("B");
        myList.add("C");
        myList.add("D");
        myList.add("E");
        myList.add("F");

        AtomicBoolean firstElementProcessed = new AtomicBoolean(false);
        myList.stream().map(s -> {
            if (firstElementProcessed.getAndSet(true)) {
                return s;
            }
            return (s + " Something special");
        }).forEach(System.out::println);
    }
}

也许为了提高可读性,我将其重构为:

public class Solution {
        ...

        AtomicBoolean firstElementProcessed = new AtomicBoolean(false);
        myList.stream().map(s -> getString(firstElementProcessed, s)).forEach(System.out::println);
    }

    private static String getString(AtomicBoolean firstElementProcessed, String s) {
        if (firstElementProcessed.getAndSet(true)) {
            return s;
        }
        return (s + " Something special");
    }
}