java 不允许返回多个value,暴力点解决, 直接return数组, 规范一点封装个对象。

在返回两个或者三个返回值时,这几个返回值关系又没有强相关。

为了酷炫+通用,使用开发好的 pair 或者trple

commons-lang3

引入 commons-lang3.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

    // 可变 pair
    MutablePair<String, Exception> pair = new MutablePair<>();
    pair.setLeft("11");
    pair.setRight(new RuntimeException());

    System.out.println(pair.left);

    // 不可变pair
    ImmutablePair<String, Exception> pair2 = new ImmutablePair<>("11", new Exception());
    System.out.println(pair2.left);

    //  可变triple
    MutableTriple<String, Integer, Exception> triple = new MutableTriple<>();
    triple.setLeft("left");
    triple.setMiddle(22);
    triple.setRight(new RuntimeException());
    System.out.println(triple.left);

hutool 好像只提供了pair

引入依赖

cn.hutool
hutool-all
5.8.11

    // hutool 提供的pair
    Pair p = new cn.hutool.core.lang.mutable.MutablePair("ste", new RuntimeException());
    System.out.println(p.getKey());

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐