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);
    }
}
相关推荐
Johny_Zhao6 小时前
Ubuntu安装部署Zabbix网络监控平台和设备配置添加
linux·网络·mysql·网络安全·信息安全·云计算·apache·zabbix·shell·yum源·系统运维·itsm
搬砖的工人8 小时前
Docker环境下的Apache NiFi安装实践踩坑记录
docker·容器·apache
曼岛_13 小时前
[Java实战]Spring Boot 3 整合 Apache Shiro(二十一)
java·spring boot·apache
网安INF3 天前
Apache Shiro 1.2.4 反序列化漏洞(CVE-2016-4437)
java·网络安全·apache
大数据追光猿4 天前
【大数据】服务器上部署Apache Paimon
大数据·服务器·docker·架构·apache
斯普信专业组4 天前
基于Kubernetes的Apache Pulsar云原生架构解析与集群部署指南(上)
云原生·kubernetes·apache
C-20025 天前
使用Deployment部署运行Nginx和Apache服务
运维·kubernetes·apache
斯普信专业组5 天前
基于Kubernetes的Apache Pulsar云原生架构解析与集群部署指南(下)
云原生·kubernetes·apache
沉默的松饼5 天前
《开源先锋Apache软件基金会:历史沿革、顶级项目与行业影响》
apache
coding侠客5 天前
使用Jmeter进行核心API压力测试
java·jmeter·apache·压力测试