首次使用Lambda表达式-sunziren

  需要将List<Apple> list = new ArrayList<Apple>(); 按照Apple对象中的price属性从大到小排序。

  第一个念头闪过的是冒泡排序,转念一想,自己都觉得麻烦,遂放弃了这个馊主意。

  第二个念头就是使用list对象自带的sort方法,我的代码如下:

 List<Apple> list = new ArrayList<List>();
 list.sort((vo1, vo2) -> {
     Integer i1 = Integer.parseInt(vo1.getPrice());
     Integer i2 = Integer.parseInt(vo2.getPrice());
     return i2.compareTo(i1);
 });

  上面这段代码就是Lambda表达式。

  通过上面的代码,list中的Apple对象就按照price从大到小排序了。

  感觉就好像,传参的时候直接把方法传过去了。


  转载请注明出处。

原文地址:https://www.cnblogs.com/sunziren/p/11376978.html