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);
    }
}
相关推荐
悠悠~飘7 小时前
php学习(第四天)
php·apache
白鹭14 小时前
apache实现LAMP+apache(URL重定向)
linux·运维·apache·url重定向·apache实现lamp架构
aramae14 小时前
终端之外:解锁Linux命令行的魔法与力量
linux·服务器·apache
lifallen16 小时前
Hadoop MapOutputBuffer:Map高性能核心揭秘
java·大数据·数据结构·hadoop·算法·apache
acaad19 小时前
Apache Poi 实现导出excel表格 合并区域边框未完全显示的问题
spring·apache·excel
Hello.Reader19 小时前
Apache Flink 从流处理基础到恰好一次语义
flink·apache·linq
渣渣盟1 天前
Spark核心:单跳转换率计算全解析
大数据·spark·scala·apache
从零开始学习人工智能2 天前
SpringBoot + Apache Tika:一站式解决文件数据提取难题
spring boot·后端·apache
lifallen2 天前
揭秘KafkaStreams 线程缓存:NamedCache深度解析
数据结构·算法·缓存·kafka·apache
HashData酷克数据2 天前
Apache Cloudberry 2.0.0 发布:1981 项变更提交,续写 Greenplum 未竟之路
apache·cloudberry