• java方法返回多个值,使用Pair、Triple


    前言

    在编写java代码时,会遇到一个方法中需要返回多个返回值的场景。比如一个方法中返回两个boolean值;或者返回一个String,一个Integer类型; 以下整理java中一个方法返回多个值的写法。

    一、返回bean实体类

    新建一个实体类,将需要返回的信息存放在实体类中

    public class Test  {
        public static void main(String[] args) {
            Student student = getStudent();
            System.out.println(student);
        }
    
        public static Student getStudent(){
            Student  student = new Student();
            student.setName("张三");
            student.setAge(11);
            student.setSex("男");
            return student;
        }
    }
    @Data
    class Student{
        String name; //姓名
        int age;// 年龄
        String sex; //性别
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    执行结果:

    Student(name=张三, age=11, sex=)
    
    • 1

    二、返回map集合

    将返回信息存放在map集合中

    public class Test  {
        public static void main(String[] args) {
            Map<String,Object> map = getStudent();
            System.out.println("姓名:"+map.get("name")+",年龄:"+map.get("age")+",性别:"+map.get("sex"));
        }
    
        public static Map<String,Object> getStudent(){
            Map<String,Object> map = new HashMap<>();
            map.put("name", "张三");
            map.put("age", 11);
            map.put("sex", "男");
            return map;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    执行结果:

    姓名:张三,年龄:11,性别:男
    
    • 1

    三、返回String[]

    若一个方法需要返回的类型都是String类型,可以使用String[]数组

    public class Test  {
        public static void main(String[] args) {
            String[] arr = getStudent();
            System.out.println("姓名:"+arr[0]+",性别:"+arr[1]);
        }
    
        public static String[] getStudent(){
            String[] arr = new String[2];
            arr[0] = "张三";
            arr[1] = "男";
            return arr;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    执行结果:

    姓名:张三,年龄:11,性别:男
    
    • 1

    四、List<Object>

    public class Test  {
        public static void main(String[] args) {
            List<Object> list = getStudent();
            System.out.println("姓名:"+list.get(0)+",年龄:"+list.get(1)+",性别:"+list.get(2));
        }
    
        public static List<Object> getStudent(){
            List<Object> list = new ArrayList<>();
            list.add("张三");
            list.add(11);
            list.add("男");
            return list;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    执行结果:

    姓名:张三,年龄:11,性别:男
    
    • 1

    五、Pair、Triple

    org.apache.commons.lang3 提供了返回多个值的工具类,返回2个值用Pair,3个值用Triple

    cn.hutool.core.lang包中也提供了关于Pair的工具包

    以下内容主要基于commons-lang3包进行讲解。
    在commons-lang3包中主要提供了两种Pair类的用法:一种是Pair<L, R>理解为左边与右边;一种是Pair<K, V> 键值对key-value的形式;

    5.1 org.apache.commons.math3.util.Pair

    org.apache.commons.math3.util.Pair是Pair<K, V>键值对的形式,提供的方法主要是getKey(),getValue()

    import org.apache.commons.math3.util.Pair;
    
    public class Test  {
        public static void main(String[] args) {
            //org.apache.commons.math3.util.Pair;
            Pair<Boolean,String> pair = new Pair<>(true,"张三");
            System.out.println(pair.getKey());
            System.out.println(pair.getValue());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    执行结果:

    true
    张三
    
    • 1
    • 2

    5.2 org.apache.commons.lang3.tuple.Pair

    org.apache.commons.lang3.tuple.Pair是一个抽象类,提供Pair<L, R>左边与右边的形式的工具类,获取值的方法有getKey(), getValue(),getLeft(),getRight()

    import org.apache.commons.lang3.tuple.ImmutablePair;
    import org.apache.commons.lang3.tuple.Pair;
    
    public class Test  {
        public static void main(String[] args) {
            Pair<Boolean,String> pair = new ImmutablePair<>(true,"张三");
            System.out.println(pair.getKey());
            System.out.println(pair.getValue());
            System.out.println(pair.getLeft());
            System.out.println(pair.getRight());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    执行结果:

    true
    张三
    true
    张三
    
    • 1
    • 2
    • 3
    • 4

    5.3 org.apache.commons.lang3.tuple.Triple

    Triple提供返回三个参数的工具包,Triple也是一个抽象类,以Triple<L, M, R>的形式返回三个参数

    import org.apache.commons.lang3.tuple.ImmutableTriple;
    import org.apache.commons.lang3.tuple.Triple;
    
    public class Test  {
        public static void main(String[] args) {
            Triple triple = new ImmutableTriple("张三", 11,"男");
            System.out.println(triple.getLeft());
            System.out.println(triple.getMiddle());
            System.out.println(triple.getRight());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    执行结果:

    张三
    11
    • 1
    • 2
    • 3
  • 相关阅读:
    使用Spring Boot和MyBatis访问数据库
    树莓派无桌面配置WiFi连接
    543. 二叉树的直径
    完成flex布局与float布局
    使用Python求解方程
    投资自己,成就未来——人大女王金融硕士助力您成为金融领域的佼佼者
    laravel生命周期超细腻
    swiper第一个、最后一个滑块按钮disabled时(.swiper-button-disabled),点击触发下层点击事件的问题
    技术停滞:如何更新?
    VSCode配置MingW编译调试环境
  • 原文地址:https://blog.csdn.net/weixin_49114503/article/details/125455179