Java 8函数编程轻松入门(四)方法引用


C#中系统提供了许多IEnumerable的扩展方法。同样在Java 8中新引入了Collector类。

  • 1.方法引用

  • 定义: 简而言之:就是一个Lambda表达式。在Java 8中,我们我们会使用Lambda表达式创建匿名方法。但是更多的时候,我们的Lambda表达式可能需要调用一个已存在的方法。这时候我们可以通过个一个方法名字来引用已存在的方法会更加清晰。
  • 语法: Classname::methodName
  • 与C#的类比: 类似C#的委托。C#的委托其实就是方法的指针。而在Java 8中,方法引用其实就是将方法的地址引用指向调用方。
  • C#代码
class Program
{
    static void Main(string[] args)
    {
        Func<int, int, int> calc = Add;
    }

    public static int Add(int i, int j)
    {
        return i + j;
    }
}
  • Java 8代码 Lambda表达式
public class Chapter05 {
    public static void main(String[] args) {
        BinaryOperator<Integer> r = (x, y) -> x + y;
        int result = r.apply(1, 2);
    }
}
  • Java 8代码 方法引用
public class Chapter05 {
    public static void main(String[] args) {
        BinaryOperator<Integer> r = Chapter05::add;
        int result = r.apply(1, 2);
    }

    public static Integer add(Integer i, Integer j) {
        return i + j;
    }
}
  • 2.使用Collectors

  • 定义: 一种通用的、从流(Stream)生成复杂值的结构。
  • 2.1转换成其他集合(toList(),toSet())

public static void main(String[] args) {
    List<Integer> arrays = new ArrayList<Integer>() {{
        add(1);
        add(2);
        add(3);
        add(4);
        add(5);
    }};
    List<Integer> afterArrays = arrays.stream().filter(u -> u > 2).collect(Collectors.toList());
}
  • List<> 数据结构转换成Set<>数据结构。下面的ArrayList结构转换为HashSet结构
public static void main(String[] args) {
List<Integer> arrays = new ArrayList<Integer>() {{
    add(1);
    add(2);
    add(3);
    add(4);
    add(5);
}};

Set<Integer> setArrays = arrays.stream().collect(Collectors.toSet());
}
  • 上面是Stream类库在背后自动为你挑选合适的数据类型。当然你也可以指定对应类型的数据结构。如下指定TreeSet类型
Set<Integer>  treesetArrays=arrays.stream().collect(Collectors.toCollection(TreeSet::new));
  • 2.2转换成值

  • 如Stream提供的min\max以及Collectors提供的minBy\maxBy
int max=treesetArrays.stream().max(Comparator.comparing(u->u)).get();

int collectMax=treesetArrays.stream().collect(Collectors.maxBy(Comparator.comparing(u->u))).get();

System.out.println("max:"+max);

System.out.println("CollectMax:"+collectMax);


int avg= treesetArrays.stream().collect(Collectors.averagingInt(u->u)).intValue();
System.out.println("Avg:"+avg);
  • 2.3数据分块

  • 类似C#的IEnumerable的GroupBy()
  • 有Person的集合。按照性别分组
public class Person {
    public String name;

    public int age;

    public String sex;

    public Person(){

    }

    public Person(String n,int a,String sex){
        this.name=n;
        this.age=a;
        this.sex=sex;
    }


    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
public class GroupByTest {
    public static void main(String[] args){
        List<Person> persons=new ArrayList<>();
        persons.add(new Person("jack",23,"男"));
        persons.add(new Person("Tom",20,"男"));
        persons.add(new Person("Su",21,"女"));
        persons.add(new Person("Lily",22,"女"));

        Map<String,List<Person>> group=persons.stream().collect(Collectors.groupingBy(u->u.getSex()));

    }
}
  • 2.4Collectors.joining

  • 收集流中的数据,生成自定义的格式的字符串
 String str=persons.stream().map(u->u.getName()).collect(Collectors.joining(","));
 //jack,Tom,Su,Lily
  • 3.Lambda细节

  • Java 8 可以使用computeIfAbsent缓存。

  • 未使用computeIfAbsent

public class DetailsTest {
    public static void main(String[] args){
        Map<String,String> keyval=new HashMap<>();
        String val=keyval.get("hello");
        if(val==null){
            keyval.put("hello","world");
        }
        System.out.print(keyval.get("hello"));
    }
}
  • 使用computeIfAbsent。现在只需要一句话
public class DetailsTest {
    public static void main(String[] args){
        Map<String,String> keyval=new HashMap<>();
        keyval.computeIfAbsent("hello",u->keyval.put(u,"world"));
    }
}
  • Map也添加了forEach函数
keyval.forEach((k,v)->{System.out.println("key:"+k+",value:"+v);});
  • 总结:

  • 1.方法引用是一种引用方法的轻量级语法,类似C#的委托
  • 2.收集器类似C#的IEnumerable的扩展方法
优质内容筛选与推荐>>
1、样式属性(1)
2、2017-2018-1 学号20155311 《信息安全系统设计基础》第11周学习总结
3、python-class(4)
4、c# 扩展方法奇思妙用高级篇五:ToString(string format) 扩展
5、.NET获取文件的MIME类型(Content Type)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号