org.apache.commons.lang3.tuple.Pair

一、介绍

1、key-value配对

当我们在需要返回两种值的情况下可以使用这个api,在核心Java库中可以使用配对(Pair)的实现、Apache Commons。如果原来的项目中jdk低于1.8建议使用Apache Commons这种方法,这样不用动原项目的jdk。

2、依赖
复制代码
<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.7</version>
 </dependency>
3、使用

在Apache Commons库中,org.apache.commons.lang3.tuple 包中提供Pair抽象类,不能被直接实例化,可以通过Pair.of(L,R)实例化,提供了getLeft()和getRight()方法。

复制代码
public class RedisTest {

    public static void main(String[] args) {
        try {

            //不可变配对
            Pair<String, String> pair = new ImmutablePair<>("123", "456");
            //可变配对
            Pair<String, String> pairV2 = new MutablePair<>("123", "456");

						//不可变配对set值会抛错
            //pair.setValue("123");
            pairV2.setValue("123");

            System.out.println(pair.getKey());
            System.out.println(pair.getValue());
            System.out.println(pair.getLeft());
            System.out.println(pair.getRight());

            System.out.println("-----------");

            System.out.println(pairV2.getKey());
            System.out.println(pairV2.getValue());
            System.out.println(pairV2.getLeft());
            System.out.println(pairV2.getRight());


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

二、使用场景

接口需要返回两个值,又不想使用Map

复制代码
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@Slf4j
public class PairTest {
    @Test
    public void testPair() {
        //包装类型
        Pair<Boolean, Integer> numPair = isNumberVaild(-1);
        log.info("左边={},右边={}", numPair.getLeft(), numPair.getRight());
        numPair = isNumberVaild(2);
        log.info("左边={},右边={}", numPair.getLeft(), numPair.getRight());
        //对象
        Pair<Integer, UserDTO> userPair = getUser("张三");
        log.info("左边={},右边={}", userPair.getLeft(), userPair.getRight());
    }

    private Pair<Integer, UserDTO> getUser(String name) {
        UserDTO userDTO = UserDTO.builder().userName(name).build();
        return Pair.of(1,userDTO);
    }

    private Pair<Boolean, Integer> isNumberVaild(Integer number) {
        if(number < 0){
            return Pair.of(false, null);
        }
        return Pair.of(true, number*10);
    }
}
相关推荐
程序 代码狂人21 小时前
Apache旗下产品有哪些
apache
地球@+jdhb441 天前
快手私信自动回复卡片一键生成后台制作金钥匙跳转卡片代理后台制作|云雀外链
apache
D愿你归来仍是少年1 天前
Apache Spark 第 6 章 附加篇:Tungsten 引擎深度解析
大数据·spark·apache
D愿你归来仍是少年1 天前
Apache Flink Checkpoint 与 Chandy-Lamport 算法深度解析
算法·flink·apache
AllData公司负责人1 天前
高效同步!离线开发平台(DolphinScheduler) 实现Apache IotDB物联网数据同步到 Doris
apache·doris·iotdb
程序 代码狂人1 天前
Apache是什么
apache
颜颜yan_1 天前
面向工业物联网的大数据底座选型:Apache IoTDB 的架构能力与落地价值分析
大数据·物联网·apache
Eason_LYC1 天前
封神!Apache OFBiz CVE-2024-45507 漏洞复现(从入门到反弹Shell,新手也能拿捏服务器)
服务器·web安全·网络安全·apache·apache ofbiz·cve-2024-45507·getshell
脑电信号要分类2 天前
将多张图片拼接成一个pdf文件输出
pdf·c#·apache
不是书本的小明2 天前
Apache vs Nginx vs Tomcat 核心区别与优化
nginx·tomcat·apache