关于Apache-Commons-Lang3中元组的使用

关于Apache-Commons-Lang3中元组的使用

在日常工作中,有时候我们并不清楚有这些工具类的存在,造成在开发过程中重新实现导致时间浪费,且开发的代码质量不佳。而 apache 其实已经提供了系列的工具包给我们使用,只是大多数人,平时没有注意到。接下来我们从使用最多的 org.apache.commons.lang3 这个 jar 包开始,了解 apache 为我们提供了怎样的方便体验!

本篇介绍的是关于 Commons-Lang3 中元组的使用



1、Commons-Lang3元组介绍

1.1、Commons-Lang3元组的应用场景

在实际工作当中,有时候我们会遇到期望返回 1 个以上返回值的情况,在接触元组之前,我们最常用的方式,可能有以下三种:

  1. 定义一个 Class,将返回值作为 Class 的属性,该 Class 作为方法的返回值返回;
  2. 将返回值放入 Object 数组中,数组作为方法的返回值返回;
  3. 将返回值放入 List 或 Map 中,List 或 Map 作为方法的返回值返回

对于使用 Class 的场景,如果返回的两个值并没有任何关联关系,或者说每一个方法返回的参数都不同,那么我们就得为每一个方法的返回类型去创建对应的类来取包装。

使用 map 作为返回值的话调用方在不清楚 map 中具体有什么内容的时候需要去遍历 keySet 或 entrySet,而 list 和 array 也是同样的问题,不知道哪一个参数存放在哪里。

此时我们就需要使用我们本篇的主角————元组。

1.2、Commons-Lang3 元组的介绍

Commons-Lang3 元组指的就是 org.apache.commons.lang3.tuple 包下的 Pair 和 Triple 两个抽象类及其对应子类。

image-20230711221753315


2、Commons-Lang3元组使用————Pair

2.1、Pair简介

Pair 类可以用于存储一对值,类似于一个二元组。Pair 类是一个泛型类,可以存储任意两种类型的值。

以下是 Pair 类的构造方法和方法列表:

		// 构造函数
		public Pair() {
    
    }

		// 创建一个具有给定左值和右值的 Pair 对象,默认用的是子类 ImmutablePair,
    public static <L, R> Pair<L, R> of(L left, R right) {
    
    
        return new ImmutablePair(left, right);
    }

		// 返回左值
    public abstract L getLeft();
    
    public final L getKey() {
    
    
        return this.getLeft();
    }

		// 返回右值
    public abstract R getRight();

    public R getValue() {
    
    
        return this.getRight();
    }

    public int compareTo(Pair<L, R> other) {
    
    
        return (new CompareToBuilder()).append(this.getLeft(), other.getLeft()).append(this.getRight(), other.getRight()).toComparison();
    }

		// 比较两个 Pair
    public boolean equals(Object obj) {
    
    
        if (obj == this) {
    
    
            return true;
        } else if (!(obj instanceof Map.Entry)) {
    
    
            return false;
        } else {
    
    
            Map.Entry<?, ?> other = (Map.Entry)obj;
            return Objects.equals(this.getKey(), other.getKey()) && Objects.equals(this.getValue(), other.getValue());
        }
    }

		// 返回 Pair 对象的哈希码
    public int hashCode() {
    
    
        return (this.getKey() == null ? 0 : this.getKey().hashCode()) ^ (this.getValue() == null ? 0 : this.getValue().hashCode());
    }

		// 返回 Pair 对象的字符串表示
    public String toString() {
    
    
        return "(" + this.getLeft() + ',' + this.getRight() + ')';
    }

    public String toString(String format) {
    
    
        return String.format(format, this.getLeft(), this.getRight());
    }
2.2、Pair案例
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

public class Demo {
    
    
  
    /**
     *  二元组
     */
    public static Pair<A, B> testPair() {
    
    
        A a = new A();
        B b = new B();
        return new ImmutablePair<>(a, b);
    }

    public static void main(String[] args) {
    
    
        Pair pair = testPair();
        System.out.println("a : " + pair.getLeft());
        System.out.println("b : " + pair.getRight());
    }
}

2.3、Pair子类

ImmutablePair 和 MutablePair 是 Apache Commons Lang 库中 Pair 的两个实现类,用于表示具有两个值的键值对。

  • ImmutablePair 是一个不可变的类,一旦创建,就不能更改其值,表示一个只读的键值对。它的构造函数接受两个参数,分别是键和值,并且它还提供了一些方法来获取键和值的值。例如,可以使用getLeft() 方法获取键的值,使用 getRight() 方法获取值的值。
  • MutablePair 是一个可变的类,可以在创建之后更改其值,表示一个可变的键值对。其他与 ImmutablePair 类似。

这两个类都实现了 Map.Entry 接口,因此可以将它们作为键值对添加到 Map 中。此外,它们还实现了 Serializable 接口,因此可以将它们序列化为字节数组并在网络上传输或保存到文件中。

应用场景:Pair 类是一个灵活的工具类,适用于许多需要存储一对值的场景。它可以简化代码并提高代码的可读性和可维护性。

3、Commons-Lang3元组使用————Triple

3.1、Triple 简介

Triple 类是一个泛型类,可以存储三个值。类似于Pair类,Triple类也可以用于将多个值封装在一起,以便于传递、比较或存储。

以下是 Triple 类的构造方法和方法列表:

    // 构造函数
		public Triple() {
    
    }
		// 构造函数,创建一个具有给定左值、中值和右值的 Triple 对象
    public static <L, M, R> Triple<L, M, R> of(L left, M middle, R right) {
    
    
        return new ImmutableTriple(left, middle, right);
    }

		// 返回左值
    public abstract L getLeft();

		// 返回中值
    public abstract M getMiddle();

		// 返回右值
    public abstract R getRight();

    public int compareTo(Triple<L, M, R> other) {
    
    
        return (new CompareToBuilder()).append(this.getLeft(), other.getLeft()).append(this.getMiddle(), other.getMiddle()).append(this.getRight(), other.getRight()).toComparison();
    }

		// 判断两个 Triple 对象是否相等
    public boolean equals(Object obj) {
    
    
        if (obj == this) {
    
    
            return true;
        } else if (!(obj instanceof Triple)) {
    
    
            return false;
        } else {
    
    
            Triple<?, ?, ?> other = (Triple)obj;
            return Objects.equals(this.getLeft(), other.getLeft()) && Objects.equals(this.getMiddle(), other.getMiddle()) && Objects.equals(this.getRight(), other.getRight());
        }
    }

		// 返回 Triple 对象的哈希码
    public int hashCode() {
    
    
        return (this.getLeft() == null ? 0 : this.getLeft().hashCode()) ^ (this.getMiddle() == null ? 0 : this.getMiddle().hashCode()) ^ (this.getRight() == null ? 0 : this.getRight().hashCode());
    }

		// 返回 Triple 对象的字符串表示
    public String toString() {
    
    
        return "(" + this.getLeft() + "," + this.getMiddle() + "," + this.getRight() + ")";
    }

    public String toString(String format) {
    
    
        return String.format(format, this.getLeft(), this.getMiddle(), this.getRight());
    }
3.2、Triple案例
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;

public class Demo {
    
    

    /**
     *  三元组
     */
    public static Triple<A, B, C> testTriple() {
    
    
        A a = new A();
        B b = new B();
        C c = new C();
        return new ImmutableTriple<>(a, b, c);
    }

    public static void main(String[] args) {
    
    
        Triple triple = testTriple();
        System.out.println("a : " + triple.getLeft());
        System.out.println("b : " + triple.getMiddle());
        System.out.println("c : " + triple.getRight());
    }
}
3.3、Triple子类

同 Pair 子类相同,ImmutableTriple 不可变组件对象,MutableTriple 可改变值的三个元素组件对象。

猜你喜欢

转载自blog.csdn.net/weixin_45187434/article/details/131670860