import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
/** * Created by Shusheng Shi on 2017/5/1. */
public class Sample {
public static void main(String[] args) {
List<Integer> values = Arrays.asList(1, 2, 3, 4, 5, 6);
for (int i = 0; i < values.size(); i++) {
System.out.println(values.get(i));
}
for (int e : values) {
System.out.println(e);
}
Iterator<Integer> iterator = values.iterator();
while (iterator.hasNext()) {
Integer i = iterator.next();
System.out.println(i);
}
System.out.println("=======================================");
values.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println(values);
}
});
values.forEach(value -> System.out.println(values));
System.out.println("=======================================");
int total = 0;
for (int e : values) {
total += e * 2;
}
System.out.println(total);
System.out.println(
values.stream()
.map(e -> e * 2)
.reduce(0, (c, e) -> c + e)
);
}
}
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/** * Created by Shusheng Shi on 2017/5/1. */
public class Sample {
public static void main(String[] args) {
List<Integer> values = Arrays.asList(1, 2, 3, 4, 5, 6,7,8,9,10);
}
public static boolean isEven(int number) {
System.out.println("isEven for " + number);
return number % 2 == 0;
}
public static boolean isGT3(int number) {
System.out.println("isGT3 for " + number);
return number > 3;
}
public static int doubleIt(int number) {
System.out.println("doubleIt for " + number);
return number * 2;
}
private static int totalValues(List<Integer> numbers, Predicate<Integer> selector) {
return numbers.stream()
.filter(selector)
.reduce(0, (c, e) -> c + e);
}
}