整型处理
IntStream
求1-100中的偶数之和
int result = IntStream.rangeClosed(1, 100)
.filter(a -> a % 2 == 0)
.sum();
System.out.println(result);
int[] 与 Integer[] 转换
int[] intArray = {1,2,3,4,5};
Integer[] integers = Arrays.stream(intArray)
//each boxed to an {@code Integer}.
// 将每个int转成Integer
.boxed()
.toArray(Integer[]::new);
System.out.println(Arrays.toString(integers));
字符串处理
join方法
String[] str = {"hello", "world", "fonxian"};
String s = String.join(",", str);
System.out.println(s);
集合处理
去掉List中的重复元素
List<String> words = Arrays.asList("a", "b", "c", "d", "a", "b");
words.stream()
.distinct()
.collect(Collectors.toList())
.forEach(System.out::println);
接口
默认实现
interface Animal{
void move();
// 默认实现
default void eat(){
System.out.println("eat");
}
}
class Dog implements Animal{
@Override
public void move() {
}
}
class Cat implements Animal{
@Override
public void move() {
}