美文网首页
commons-lang3 元组使用

commons-lang3 元组使用

作者: Tinyspot | 来源:发表于2023-12-06 15:17 被阅读0次

1. org.apache.commons.lang3.tuple 包

1.1 抽象类 Pair (二元组)

  • Pair 是抽象类,不可直接实例化,通过 Pair.of(L,R) 实例化
  • Pair 只放一组 key/value,在接口需返回两个值时比较适用
public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L, R>>, Serializable {

    public static <L, R> Pair<L, R> of(final L left, final R right) {
        return ImmutablePair.of(left, right);
    }

    public final L getKey() {
        return getLeft();
    }

    public R getValue() {
        return getRight();
    }
}
public final class ImmutablePair<L, R> extends Pair<L, R> {
    @Override
    public R setValue(final R value) {
        throw new UnsupportedOperationException();
    }
}

public class MutablePair<L, R> extends Pair<L, R> { 
}

1.2 抽象类 Triple (三元组)

public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Serializable {
    public static <L, M, R> Triple<L, M, R> of(final L left, final M middle, final R right) {
        return new ImmutableTriple<>(left, middle, right);
    }
}

2. Pair 示例

2.1 示例

@Test
public void isMutable() {
    // 不可变配对
    Pair pair = new ImmutablePair<>("left", "right");
    // 可变配对
    Pair mutablePair = new MutablePair<>("left", "right");

    // pair.setValue("333"); // java.lang.UnsupportedOperationException
    mutablePair.setValue("111");
    
    // (left,right); (left,111)
    System.out.println(pair + "; " + mutablePair);
}
@Test
public void demo() {
    Pair<String, String> pair = Pair.of("left", "right");
    System.out.println(pair.getKey() + "; " + pair.getLeft());
    System.out.println(pair.getValue() + "; " + pair.getRight());

    // setNull
    Pair<String, String> pair2 = Pair.of(null, null);
    Pair<String, String> pair3 = Pair.of(null, "right");
    Pair<String, String> pair4 = Pair.of("left", null);
    System.out.println(pair2 + "; " + pair3 + "; " + pair4);
}

2.2 结合Map

@Test
public void combineMap() {
    Map<Long, Pair<String, String>> map = new HashMap<>();
    map.put(1001L, Pair.of("left", "right"));

    Map<Pair<String, String>, Boolean> booleanMap = new HashMap<>();
    booleanMap.put(Pair.of("left", "right"), true);
}

3. Triple 示例

@Test
public void triple() {
    Triple<String, String, String> triple = Triple.of("left", "middle", "right");
    System.out.println(triple.getLeft() + "; " + triple.getMiddle() + "; " + triple.getRight());

    Triple<Pair<String, String>, String, List<String>> multiTriple =
            Triple.of(Pair.of("userId", "orderId"), "middle", Lists.newArrayList("right1", "right1"));
    System.out.println(multiTriple);
}

相关文章

  • SpringBoot--实战开发--commons-lang3(

    一、commons-lang3简介 commons-lang3是Apache的Jakarta commons工程下...

  • SpringBoo集成commons-lang3

    一、commons-lang3简介 commons-lang3是Apache的Jakarta commons工程下...

  • kotlin中缀函数 to

    二元元组 三元元组 to函数的使用 元组的概念? 元组的使用场景

  • python 元组

    元组 元组通常用来存储不同的元素。 a、创建元组 元组的创建使用小括号()创建,元组中的元素使用逗号隔离。 创建一...

  • 集合、元组

    元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简...

  • Python元组操作及方法总结

    一、元组概念 Python的元组与列表类似,不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组创...

  • Python基础_05:元组(2019-1-14)

    元组 python中元组和列表类似不同之处在于元组中的元素不可修改元组使用(),列表使用[]return a,b,...

  • python 基础 - 元组

    Python 元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号...

  • 元祖

    Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。...

  • 元组(tuple)

    1.元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改。 元组使用小括号(),列表使用方括号[]...

网友评论

      本文标题:commons-lang3 元组使用

      本文链接:https://www.haomeiwen.com/subject/pyujgdtx.html