java 8中的lambda表达式

java一开始设计的是完全面向对象的,所以一等公民都是对象.函数只是作为对象上的附属.而python,scala等语言中,则有一些函数式编程的特点.不过java 8也提供了lambda表达式,使java也具有了一些函数式编程的特点,带来了一些便利.

1 方法引用

假设我们写一个窗体,窗体上有一个button和一个label,点击button时要改变label的内容.
先看看原来的写法

public class BarWindow extends JFrame {

	public BarWindow() {
		
		button.addActionListener(new MyButtonClickListener());
	}
	private String getCurrentStatus() {
		return "current status is " + System.currentTimeMillis();
	}
	private class MyButtonClickListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			BarWindow.this.label.setText(BarWindow.this.getCurrentStatus());
		}
	}
}

事件监听器MyButtonClickListener因为要访问label这个内部属性,所以是一个java内部类,BarWindow.this用来访问其外部类,而这时只用this显然不能得到label属性

高级一点,可以尝试匿名内部类

public class BarWindow2 extends JFrame {

	public BarWindow2() {
		
		button.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				BarWindow2.this.label.setText(BarWindow2.this.getCurrentStatus());
			}
			
		});
	}
	
	private String getCurrentStatus() {
		return "current status is " + System.currentTimeMillis();
	}
}

此时为了响应点击事件,都需要实例化一个对象(匿名内部类也是要生成class文件的),采用了lambda表达式,则省去了这个class
public class BarWindow3 extends JFrame {

	public BarWindow3() {
		button.addActionListener(e -> {
			this.label.setText(this.getCurrentStatus());
		});
	}

	private String getCurrentStatus() {
		return "current status is " + System.currentTimeMillis();
	}
}

或者用::进行方法的引用
public class BarWindow4 extends JFrame {

	public BarWindow4() {
		button.addActionListener(this::onButtonClicked);
	}

	private void onButtonClicked(ActionEvent e){
		this.label.setText(this.getCurrentStatus());
	}
	
	private String getCurrentStatus() {
		return "current status is " + System.currentTimeMillis();
	}
}

话说如果能button.addActionListener(this.onButtonClicked);就更像函数式编程语言了

2 java中的函数式编程
这里主要是过程式编程和函数式编程的区别,过程式编程需要程序员明确告诉计算机怎么做,函数式则不用那么麻烦.
比如有一个int的集合,想把每一个元素double一下,放到新的集合中

		List<Integer> barList = Arrays.asList(3, 1, -2, 3, -10);

		//过程式编程,需要亲自遍历数据,处理每个元素
		List<Integer> fooList = new ArrayList<Integer>(barList.size());
		for (Integer bar : barList) {
			fooList.add(bar * 2);
		}
		
		//函数式编程
		fooList = barList.stream().map(e -> {
			return 2 * e;
		}).collect(Collectors.toList());


下面一个例子则是把这个集合中大于0的元素找出来,平方之后求和
		int sum = 0;

		for (Integer bar : barList) {
			if (bar >= 0) {
				sum += bar * bar;
			}
		}

		System.out.println(sum);

		sum = barList.stream().filter(e -> {
			return e >= 0;
		}).map(e -> {
			return e * e;
		}).reduce((x, y) -> {
			return x + y;
		}).get();

		System.out.println(sum);


3并行操作
想象有一个数组,对里面每个元素进行操作,如果操作比较耗时,可以考虑多线程,不过这样需要程序员手动的把数组分块,建立任务,启动线程,结果汇总.
java 8的流提供了并行的功能,提供了一些便利
public class Parral {

	public static void main(String[] args) {
		List<String> list = Arrays
				.asList("hello", "bye", "ciao", "bye", "ciao");

		//单线程阻塞
		int sum = 0;
		for (String src : list) {
			sum += bar(src);
		}

		//多线程并行
		sum = list.parallelStream().mapToInt(Parral::bar).sum();
		
	}

	private static int bar(String src) {
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
		}
		return src.length();
	}

}

猜你喜欢

转载自kabike.iteye.com/blog/2199343