IMF 自定义Reduce函数,学习理解Apply、callback函数、泛型类型、iterator迭代器
2016-05-27 15:48
465 查看
IMF 自定义Reduce函数,学习理解Apply、callback函数
泛型:可以定义为各种类型,使用大写字母标识,名称自己随便定义了。
apply函数,就理解成一个普通函数吧,在一个接口中定义,无具体的实现方法,具体内容在匿名接口方法中去实现。这里有点类似Spark Streaming电商广告点击综合案例底层数据层的建模和编码实现(基于MySQL) 中的callback,异曲同工之妙。
Iterable:迭代器,Iterable.next依次取得迭代器的值
package com.dt.spark.SparkApps.SparkStreaming;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IMFReduce {
public static MYT myIMFreduce(final Iterable iterable, final Func func, MYT origin) {
for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
origin = func.IMFapply((MYF) (iterator.next()), origin);
}
return origin;
}
public static void main(String[] args) {
System.out.println("=======IMF reduce===========!");
List people = new ArrayList();
people.add(new Person("张三", 21));
people.add(new Person("李四", 25));
people.add(new Person("王五", 38));
people.add(new Person("小李", 24));
Integer maxAge = IMFReduce.myIMFreduce(people, new Func() {
int countAge =0;
public Integer IMFapply(Person person, Integer origin) {
countAge += person.getAge();
return countAge;
}
},22);
System.out.println("myIMFreduce 汇总年龄累计"+maxAge);
}
public interface Func {
T IMFapply(F currentElement, T origin);
}
}
class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
运行结果:
=======IMF reduce===========!
myIMFreduce 汇总年龄累计108
泛型:可以定义为各种类型,使用大写字母标识,名称自己随便定义了。
apply函数,就理解成一个普通函数吧,在一个接口中定义,无具体的实现方法,具体内容在匿名接口方法中去实现。这里有点类似Spark Streaming电商广告点击综合案例底层数据层的建模和编码实现(基于MySQL) 中的callback,异曲同工之妙。
Iterable:迭代器,Iterable.next依次取得迭代器的值
package com.dt.spark.SparkApps.SparkStreaming;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IMFReduce {
public static MYT myIMFreduce(final Iterable iterable, final Func func, MYT origin) {
for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
origin = func.IMFapply((MYF) (iterator.next()), origin);
}
return origin;
}
public static void main(String[] args) {
System.out.println("=======IMF reduce===========!");
List people = new ArrayList();
people.add(new Person("张三", 21));
people.add(new Person("李四", 25));
people.add(new Person("王五", 38));
people.add(new Person("小李", 24));
Integer maxAge = IMFReduce.myIMFreduce(people, new Func() {
int countAge =0;
public Integer IMFapply(Person person, Integer origin) {
countAge += person.getAge();
return countAge;
}
},22);
System.out.println("myIMFreduce 汇总年龄累计"+maxAge);
}
public interface Func {
T IMFapply(F currentElement, T origin);
}
}
class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
运行结果:
=======IMF reduce===========!
myIMFreduce 汇总年龄累计108
相关文章推荐
- web.xml文件中配置<mime-mapping>下载文件类型
- Android 探究 LayoutInflater setFactory
- android关机流程-framework部分
- Android 5.0+ 解析(三)Palette类
- Android获取系统应用包名
- iOS解决导航栏标题向右偏移问题
- android分析之自定义圆形头像
- android设置中常用的PreferenceActivity用法介绍
- commons-pool2中GenericKeyedObjectPool应用demo
- [置顶] 在unity中UUGI的一些使用和操作
- Android 5.0+ 解析(二)CardView控件
- Android Template(模板) 编写(一)
- android自定义控件学习【1】----自定义卡券View
- Android 进程间通信,基于Messeger(IPC)
- Starting the application on Mac does not work(拷贝platforms到不同的位置,才能解决问题),还可设置DYLD_PRINT_LIBRARIES=1 观察动态库
- 微信昵称乱码及mysql编码格式设置(utf8mb4)
- Windows下Cordova环境搭建及如何用android studio导入cordova项目生成apk
- iOS 后台运行程序
- PNG格式图片原理
- 【android随笔】工具类整理--两次点击BACK键,退出应用的实现