函数对象
来源:互联网 发布:网络执法官3.56破解版 编辑:程序博客网 时间:2024/06/10 18:25
函数对象是一种携带有一种操作方法的对象(往往是单例对象),它的行为就像是一个可以传递出去的单个方法一样,并且可以拥有在多个调用之间持久化的状态,它类似于策略模式。
代码例子:
//不同类型函数对象interface Combiner<T> { T combine(T x, T y); }interface UnaryFunction<R,T> { R function(T x); }interface Collector<T> extends UnaryFunction<T,T> { T result(); }interface UnaryPredicate<T> { boolean test(T x); }public class Functional { //用函数对象combiner调用combine() public static <T> T reduce(Iterable<T> seq, Combiner<T> combiner) { Iterator<T> it = seq.iterator(); if(it.hasNext()) { T result = it.next(); while(it.hasNext()) result = combiner.combine(result, it.next()); return result; } //如果seq集合为空,则抛出异常 return null; } //用函数对象Collector调用function() public static <T> Collector<T> forEach(Iterable<T> seq, Collector<T> func) { for(T t : seq) func.function(t); return func; } //用函数对象UnaryFunction对象调用function() public static <R,T> List<R> transform(Iterable<T> seq, UnaryFunction<R,T> func) { List<R> result = new ArrayList<R>(); for(T t : seq) result.add(func.function(t)); return result; } //用函数对象UnaryPredicate调用test() public static <T> List<T> filter(Iterable<T> seq, UnaryPredicate<T> pred) { List<T> result = new ArrayList<T>(); for(T t : seq) if(pred.test(t)) result.add(t); return result; } //计算整数和的函数对象 static class IntegerAdder implements Combiner<Integer> { public Integer combine(Integer x, Integer y) { return x + y; } } //计算整数差的函数对象 static class IntegerSubtracter implements Combiner<Integer> { public Integer combine(Integer x, Integer y) { return x - y; } } //计算浮点数和的函数对象 static class BigDecimalAdder implements Combiner<BigDecimal> { public BigDecimal combine(BigDecimal x, BigDecimal y) { return x.add(y); } } //计算大整数和的函数对象 static class BigIntegerAdder implements Combiner<BigInteger> { public BigInteger combine(BigInteger x, BigInteger y) { return x.add(y); } } //用原子计算整数和 static class AtomicLongAdder implements Combiner<AtomicLong> { public AtomicLong combine(AtomicLong x, AtomicLong y) { return new AtomicLong(x.addAndGet(y.get())); } } //精确浮点数的最后一位 static class BigDecimalUlp implements UnaryFunction<BigDecimal,BigDecimal> { public BigDecimal function(BigDecimal x) { return x.ulp(); } } //帅选合格的数字 static class GreaterThan<T extends Comparable<T>> implements UnaryPredicate<T> { private T bound; public GreaterThan(T bound) { this.bound = bound; } public boolean test(T x) { return x.compareTo(bound) > 0; } } //计算整数相乘的值 static class MultiplyingIntegerCollector implements Collector<Integer> { private Integer val = 1; public Integer function(Integer x) { val *= x; return val; } public Integer result() { return val; } } public static void main(String[] args) { List<Integer> li = Arrays.asList(1, 2, 3, 4, 5, 6, 7); //计算总和 Integer result = reduce(li, new IntegerAdder()); System.out.println(result); //计算数字想减的总结果 result = reduce(li, new IntegerSubtracter()); System.out.println(result); //过滤数字 System.out.println(filter(li, new GreaterThan<Integer>(4))); //返回计算数字累乘的结果 System.out.println(forEach(li, new MultiplyingIntegerCollector()).result()); //返回过滤后的数字累乘的结果 System.out.println(forEach(filter(li, new GreaterThan<Integer>(4)), new MultiplyingIntegerCollector()).result()); //设置BigDecimal的精确度 MathContext mc = new MathContext(7); List<BigDecimal> lbd = Arrays.asList( new BigDecimal(1.1, mc), new BigDecimal(2.2, mc), new BigDecimal(3.3, mc), new BigDecimal(4.4, mc)); //计算浮点数结果 BigDecimal rbd = reduce(lbd, new BigDecimalAdder()); System.out.println(rbd); //过滤数字 System.out.println(filter(lbd, new GreaterThan<BigDecimal>(new BigDecimal(3)))); List<BigInteger> lbi = new ArrayList<BigInteger>(); BigInteger bi = BigInteger.valueOf(11); for(int i = 0; i < 11; i++) { lbi.add(bi); //返回大于这个数的不重复质素 bi = bi.nextProbablePrime(); } System.out.println(lbi); BigInteger rbi = reduce(lbi, new BigIntegerAdder()); System.out.println(rbi); //确保判断质素的出错概率不超过1-(1/2)^5 System.out.println(rbi.isProbablePrime(5)); List<AtomicLong> lal = Arrays.asList( new AtomicLong(11), new AtomicLong(47), new AtomicLong(74), new AtomicLong(133)); //计算原子数 AtomicLong ral = reduce(lal, new AtomicLongAdder()); System.out.println(ral); System.out.println(transform(lbd,new BigDecimalUlp())); }}
运行结果:
28
-26
[5, 6, 7]
5040
210
11.000000
[3.300000, 4.400000]
[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
311
true
265
[0.000001, 0.000001, 0.000001, 0.000001]
0 0
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 对象 函数
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象
- 函数对象~
- 函数对象
- 阿里云防火墙
- BroadcastReceiver
- 数学之美 (胡军)
- leetcode_num168&171_excel title&number
- Simple_Wallet 在服务器端通过RPC操纵钱包
- 函数对象
- Linux系统cpuinfo详解
- Google Hosts
- 【Python】科学计算(一)——Mandelbrot集合
- Configuration(AnnotationConfiguration)
- 深入浅出的讲解傅里叶变换(2)
- pow函数总结
- HDU 1012 u Calculate e
- 泛型参数Generic