java 8 feature notes

Datetime:2016-08-23 00:38:37          Topic: Java8           Share

Java 8 features make codes readable and compactable, so it is meaningful to learn about the skills and welcome the 9th version coming.

1. Convert intList to StrList.

  • Basic method
public List<String> BasicMethod() {
    List<String> strList = new ArrayList<>();
    List<Integer> intList = new ArrayList<>();
    for (int i : intList) {
        strList.add(String.valueOf(i));
    }
    return strList;
}
  • Foreach method
public List<String> ForeachMethod() {
    List<String> strList = new ArrayList<>();
    List<Integer> intList = ImmutableList.of(1, 2, 3);
    intList.forEach(i -> strList.add(String.valueOf(i)));
    return strList;
}
  • Stream method
import java.util.stream.Collectors;
public List<String> StreamMethod() {
    List<Integer> intList = ImmutableList.of(1, 2, 3);
    return intList.stream().map(i -> String.valueOf(i)).collect(Collectors.toList());
}




About List