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);
    }
}
相关推荐
脑电信号要分类10 小时前
将多张图片拼接成一个pdf文件输出
pdf·c#·apache
不是书本的小明14 小时前
Apache vs Nginx vs Tomcat 核心区别与优化
nginx·tomcat·apache
Suchadar2 天前
源码编译Apache
apache
一字白首3 天前
小程序组件化进阶:从复用到通信的完整指南DAY04
前端·小程序·apache
专注_每天进步一点点4 天前
mysql-connector-j(8.0 及以上版本,包括你使用的 8.3.0)并非采用 GPL 许可证,因此你在项目中引入该依赖时,不需要遵循 GPL 的开源要求(比如开源你的整个项目)
数据库·mysql·apache
不爱学英文的码字机器4 天前
Apache RocketMQ+cpolar 让消息服务全网可达
apache·rocketmq
鸽芷咕4 天前
海量时序数据选型指南:从大数据架构演进看 Apache IoTDB 的崛起
大数据·数据库·架构·apache
D愿你归来仍是少年4 天前
Apache Spark 第 3 章:核心概念 RDD / DataFrame
大数据·spark·apache
D愿你归来仍是少年5 天前
Apache Spark 第 4 章:Spark 整体架构
spark·apache
D愿你归来仍是少年5 天前
Apache Flink 算子(Operator)深度解析
大数据·flink·apache