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);
    }
}
相关推荐
lisanmengmeng11 小时前
apache-tomcat 安装部署
java·tomcat·apache
Hello.Reader16 小时前
Apache StreamPark 快速上手从一键安装到跑起第一个 Flink SQL 任务
sql·flink·apache
sanx181 天前
专业电竞体育数据与系统解决方案
前端·数据库·apache·数据库开发·时序数据库
光军oi1 天前
全栈开发杂谈————关于websocket若干问题的大讨论
java·websocket·apache
二饭5 天前
POI操作Docx的踩坑指南(一)
java·apache
Faith_xzc7 天前
Apache Doris 内部数据裁剪与过滤机制的实现原理
apache
Trainer21077 天前
十分钟搭建thinkphp开发框架
php·apache·phpstorm·composer
syntaxseeker7 天前
Apache Beam入门教程:统一批流处理模型
其他·apache
Full Stack Developme7 天前
Java 工具类 Hutool、Guava 与 Apache Commons 的区别
java·apache·guava
忆~遂愿7 天前
谷歌云+Apache Airflow,数据处理自动化的强力武器
人工智能·python·深度学习·opencv·自动化·apache