Java-8-UnaryOperator
UnaryOperator<T> extends Function<T, T>
源码:
@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {
static <T> UnaryOperator<T> identity() {
return t -> t;
}
}
这个接口继承Function接口,Funtion接口,定义了一个apply的抽象类,接收一个泛型T对象,并且返回泛型R对象
这个接口,只接收一个泛型参数T,集成Function接口,也就是说,传入泛型T类型的参数,调用apply后,返回也T类型的参数;这个接口定义了一个静态方法,返回泛型对象的本身
简单例子
public class M1 {
public static void main(String[] args) {
UnaryOperator<Integer> u1 = integer ->
integer + 100;
System.out.println(u1.apply(100));
System.out.println("-----------");
List<String> strings = Arrays.asList(
"asas",
"ddd",
"csd"
);
UnaryOperator<String> unaryOperator = s ->
s;
for (String s : strings) {
System.out.println(unaryOperator.apply(s));
}
System.out.println("-----------");
List<Integer> list = Arrays.asList(10,20,30,40,50);
UnaryOperator<Integer> unaryOperator2 = integer -> integer*integer;
unaryOperatorFun(unaryOperator2,list)
.forEach(System.out::println);
}
private static List<Integer> unaryOperatorFun(UnaryOperator<Integer> unaryOpt, List<Integer> list){
List<Integer> uniList = new ArrayList<>();
list.forEach(i->uniList.add(unaryOpt.apply(i)));
return uniList;
}
}
DoubleUnaryOperator IntUnaryOperator LongUnaryOperator
public class M2{
public static void main(String[] args) {
DoubleUnaryOperator o1 = d ->
d + d * 12.36;
System.out.println(o1.applyAsDouble(12.58));
System.out.println("-------------------------");
IntUnaryOperator o2 = i ->
(int) (i * 100 + 236.22);
System.out.println(o2.applyAsInt(121));
System.out.println("-------------------------");
LongUnaryOperator longUnaryOperator = l ->
(long) (l * 12.36 + 96.33);
System.out.println(longUnaryOperator.applyAsLong(1212));
}
}
BinaryOperator<T> extends BiFunction<T,T,T>
源码:
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
}
public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
}
}
BinaryOperator 二元操作符, 传入的两个参数的类型和返回类型相同,继承BiFunction
public class M1 {
public static void main(String[] args) {
BinaryOperator<Integer> b1 = (i1,i2) -> i1 + i2;
System.out.println(compute_1(100,150,b1));
System.out.println("-----------------");
System.out.println(compute_1(102,145,(x,y) -> x * y));
System.out.println("-----------------");
BinaryOperator<String> b2 = (s1,s2) -> s1.length() + "---" + s2.length();
System.out.println(b2.apply("abc","123456"));
}
public static Integer compute_1(int a1,int a2,BinaryOperator<Integer> binaryOperator){
return binaryOperator.apply(a1,a2);
}
}
public class Student {
private String name;
private Integer age;
private String className;
public Student(String name,Integer age, String className){
this.name=name;
this.age=age;
this.className = className;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String getClassName() {
return className;
}
}
public class M2 {
public static void main(String[] args) {
Student s1 = new Student("Shyam", 22,"A");
Student s2 = new Student("Ram",23,"A");
Student s3 = new Student("Mohan",22,"B");
Student s4 = new Student("Ramesh",21,"B");
List<Student> list = Arrays.asList(s1,s2,s3,s4);
Comparator<Student> comparator = Comparator.comparing(Student::getAge);
//Using BinaryOperator.maxBy
System.out.println("---BinaryOperator.maxBy---");
Map<String, Optional<Student>> map = list.stream()
.collect(Collectors.groupingBy(Student::getClassName,
Collectors.reducing(BinaryOperator.maxBy(comparator))));
map.forEach((k,v)->System.out.println("Class:"+k+" Age:"+
((Optional<Student>)v).get().getAge()+" Name:"+((Optional<Student>)v).get().getName()));
//---BinaryOperator.maxBy---
//Class:A Age:23 Name:Ram
//Class:B Age:22 Name:Mohan
System.out.println("");
}
}
DoubleBinaryOperator IntBinaryOperator LongBinaryOperator
public class M3 {
public static void main(String[] args) {
DoubleBinaryOperator doubleBinaryOperator = (d1,d2) ->
d1*12.36 + d2 * 23.369;
System.out.println(doubleBinaryOperator.applyAsDouble(10.25,236.69));
System.out.println("-----------------");
IntBinaryOperator intBinaryOperator = (i1,i2) ->
i1 + i2;
System.out.println(intBinaryOperator.applyAsInt(100,3000));
System.out.println("-----------------");
LongBinaryOperator longBinaryOperator = (l1,l2) ->
l1 * l2;
System.out.println(longBinaryOperator.applyAsLong(125L,20L));
}
}
网友评论