• Pair 和 Triple 应用实践/获取指定时间间隔


     目录

    1、Pair 的详细解析     

    2、Triple 的详细解析

    3、实用案例:使用Pair封装指定时间的时间间隔


            org.apache.commons.lang3 工具包依赖

    1. <dependency>
    2. <groupId>org.apache.commons</groupId>
    3. <artifactId>commons-lang3</artifactId>
    4. <version>3.12.0</version>
    5. </dependency>

            Apache Commons Lang3 是一个 Java 工具库,提供了许多实用的类和方法,可以帮助开发人员更加便捷地编写 Java 程序。该库提供了一些常见的工具类,包括:

    1. ArrayUtils:提供了一系列静态方法来操作数组,例如数组的复制、查找、比较等操作。

    2. StringUtils:提供了一系列静态方法来操作字符串,例如字符串的比较、替换、分割等操作。

    3. ObjectUtils:提供了一些静态方法来操作对象,例如对象的比较、空值检查等操作。

    4. BooleanUtils:提供了一些静态方法来操作布尔类型的数据,例如布尔值的转换、判断等操作。

    5. NumberUtils:提供了一些静态方法来操作数字类型的数据,例如数字的转换、判断等操作。

            除此之外,还提供了一些有用的数据结构封装,比如返回多个值的数据结构Pair(2个值)和Triple(3个值)

    1、Pair 的详细解析     

            Apache Commons Lang3是一个常用的Java工具库,其中的Pair类可以用于存储一对值,类似于一个二元组。Pair类是一个泛型类,可以存储任意两种类型的值。

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

    1. public Pair(L left, R right) // 构造函数,创建一个具有给定左值和右值的Pair对象
    2. public L getLeft() // 返回左值
    3. public R getRight() // 返回右值
    4. public void setLeft(L left) // 设置左值
    5. public void setRight(R right) // 设置右值
    6. public void setValue(L left, R right) // 同时设置左值和右值
    7. public boolean equals(Object obj) // 判断两个Pair对象是否相等
    8. public int hashCode() // 返回Pair对象的哈希码
    9. public String toString() // 返回Pair对象的字符串表示

            可以看到,Pair类提供了许多方法来操作其包含的两个值。除了构造函数和获取/设置方法之外,Pair还提供了equals和hashCode方法,这使得它可以用作Map的键或集合的元素。toString方法返回一个格式为"(left,right)"的字符串,用于调试和日志记录。

            下面是一个简单的示例,说明如何使用Pair类:

    1. import org.apache.commons.lang3.tuple.Pair;
    2. public class PairExample {
    3. public static void main(String[] args) {
    4. Pair<String, Integer> pair = Pair.of("John", 25);
    5. System.out.println("Name: " + pair.getLeft()); // 输出"Name: John"
    6. System.out.println("Age: " + pair.getRight()); // 输出"Age: 25"
    7. pair.setValue("Bob", 30);
    8. System.out.println("Name: " + pair.getLeft()); // 输出"Name: Bob"
    9. System.out.println("Age: " + pair.getRight()); // 输出"Age: 30"
    10. }
    11. }

            在这个示例中,我们创建了一个Pair对象,其中左值是一个字符串"John",右值是一个整数25。然后我们使用getLeft和getRight方法获取左值和右值,并使用setValue方法更改它们。最后,我们再次使用getLeft和getRight方法来检查更改是否已生效。

    ImmutablePairMutablePair

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

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

       MutablePair是一个可变的类,可以在创建之后更改其值,表示一个可变的键值对。其他与ImmutablePair类似。

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

    1. public static void main(String[] args) {
    2. Pair<Integer, Integer> pair = Pair.of(1, 10); //同ImmutablePair.of(1, 10)
    3. System.out.println(pair.getLeft()); //1
    4. System.out.println(pair.getRight()); //10
    5. //pair.setValue(30); //报错:java.lang.UnsupportedOperationException
    6. pair = MutablePair.of(1, 10);
    7. ((MutablePair<Integer, Integer>) pair).setLeft(100);
    8. ((MutablePair<Integer, Integer>) pair).setRight(200);
    9. System.out.println(pair.getLeft()); //100
    10. System.out.println(pair.getRight()); //200
    11. pair.setValue(200); //备注setValue同setRight方法
    12. }

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

    2、Triple 的详细解析

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

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

    1. public Triple(L left, M middle, R right) // 构造函数,创建一个具有给定左值、中值和右值的Triple对象
    2. public L getLeft() // 返回左值
    3. public M getMiddle() // 返回中值
    4. public R getRight() // 返回右值
    5. public void setLeft(L left) // 设置左值
    6. public void setMiddle(M middle) // 设置中值
    7. public void setRight(R right) // 设置右值
    8. public void setValue(L left, M middle, R right) // 同时设置左值、中值和右值
    9. public boolean equals(Object obj) // 判断两个Triple对象是否相等
    10. public int hashCode() // 返回Triple对象的哈希码
    11. public String toString() // 返回Triple对象的字符串表示

            与Pair类类似,Triple类也提供了许多方法来操作其包含的三个值。除了构造函数和获取/设置方法之外,Triple还提供了equals和hashCode方法,这使得它可以用作Map的键或集合的元素。toString方法返回一个格式为"(left,middle,right)"的字符串,用于调试和日志记录。

            下面是一个简单的示例,说明如何使用Triple类:

    1. import org.apache.commons.lang3.tuple.Triple;
    2. public class TripleExample {
    3. public static void main(String[] args) {
    4. Triple<String, Integer, Boolean> triple = Triple.of("John", 25, true);
    5. System.out.println("Name: " + triple.getLeft()); // 输出"Name: John"
    6. System.out.println("Age: " + triple.getMiddle()); // 输出"Age: 25"
    7. System.out.println("IsMale: " + triple.getRight()); // 输出"IsMale: true"
    8. triple.setValue("Bob", 30, false);
    9. System.out.println("Name: " + triple.getLeft()); // 输出"Name: Bob"
    10. System.out.println("Age: " + triple.getMiddle()); // 输出"Age: 30"
    11. System.out.println("IsMale: " + triple.getRight()); // 输出"IsMale: false"
    12. }
    13. }

            在这个示例中,我们创建了一个Triple对象,其中左值是一个字符串"John",中值是一个整数25,右值是一个布尔值true。然后我们使用getLeft、getMiddle和getRight方法获取三个值,并使用setValue方法更改它们。最后,我们再次使用getLeft、getMiddle和getRight方法来检查更改是否已生效。

            Triple类是一个灵活的工具类,适用于许多需要存储三个值的场景

    3、实用案例:使用Pair封装指定时间的时间间隔

            使用Pair获取指定时间的时间间隔代码如下:

    1. import java.time.DayOfWeek;
    2. import java.time.LocalDate;
    3. import java.time.Month;
    4. import java.time.temporal.TemporalAdjusters;
    5. import org.apache.commons.lang3.tuple.ImmutablePair;
    6. import org.apache.commons.lang3.tuple.Pair;
    7. /**
    8. * 日期帮助类
    9. *
    10. * @Date 2022/7/11 15:17
    11. */
    12. public class DateUtil {
    13. /**
    14. * 获取(1-本周 2-本月 3-本季度)
    15. *
    16. * @return {@link Pair< LocalDate, LocalDate>}
    17. * @date 2022/7/11 15:36
    18. */
    19. public static Pair<LocalDate, LocalDate> getByDateType(Integer dateType,LocalDate localDate) {
    20. LocalDate today = LocalDate.now();
    21. if (localDate!=null){
    22. today = localDate;
    23. }
    24. if (dateType == null ){
    25. return null;
    26. }
    27. if (dateType == 1) { // 本周
    28. return ImmutablePair.of(today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)), // 星期一
    29. today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY))); // 星期天
    30. }
    31. if (dateType == 2) { // 本月
    32. return ImmutablePair.of(today.with(TemporalAdjusters.firstDayOfMonth()), //月的第一天
    33. today.with(TemporalAdjusters.lastDayOfMonth())); // 月的最后一天
    34. }
    35. if (dateType == 3) { // 本季度
    36. Month month = today.getMonth();
    37. Month firstMonthOfQuarter = month.firstMonthOfQuarter();
    38. Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
    39. return ImmutablePair.of(LocalDate.of(today.getYear(), firstMonthOfQuarter, 1),
    40. LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear())));
    41. }
    42. return null;
    43. }
    44. }
    1. /**
    2. * 获取指定日的时间间隔
    3. */
    4. public static Pair<LocalDateTime, LocalDateTime> getDayIntervalTime(LocalDateTime localDateTime) {
    5. LocalDateTime dateTime = getDateTime(localDateTime);
    6. return ImmutablePair.of(LocalDateTime.of(dateTime.toLocalDate(), LocalTime.MIN),
    7. LocalDateTime.of(dateTime.toLocalDate(), LocalTime.MAX));
    8. }
    9. /**
    10. * 获取指定日,本周的时间间隔
    11. */
    12. public static Pair<LocalDateTime, LocalDateTime> getWeekIntervalTime(LocalDateTime localDateTime) {
    13. LocalDateTime dateTime = getDateTime(localDateTime);
    14. return ImmutablePair
    15. .of(dateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).withHour(0).withMinute(0).withSecond(0),
    16. dateTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).withHour(23).withMinute(59).withSecond(59));
    17. }
    18. /**
    19. * 获取指定日,本月的时间间隔
    20. */
    21. public static Pair<LocalDateTime, LocalDateTime> getMonthIntervalTime(LocalDateTime localDateTime) {
    22. LocalDateTime dateTime = getDateTime(localDateTime);
    23. return ImmutablePair.of(dateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0),
    24. dateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59));
    25. }
    26. /**
    27. * 获取指定日,本季的时间间隔
    28. */
    29. public static Pair<LocalDateTime, LocalDateTime> getQuarterIntervalTime(LocalDateTime localDateTime) {
    30. LocalDateTime dateTime = getDateTime(localDateTime);
    31. Month month = dateTime.getMonth();
    32. Month firstMonthOfQuarter = month.firstMonthOfQuarter();
    33. Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
    34. return ImmutablePair.of(LocalDateTime.of(dateTime.getYear(), firstMonthOfQuarter, 1, 0, 0, 0),
    35. LocalDateTime.of(dateTime.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(dateTime.toLocalDate().isLeapYear()), 23,
    36. 59, 59));
    37. }
    38. /**
    39. * 获取指定日,年的时间间隔
    40. */
    41. public static Pair<LocalDateTime, LocalDateTime> getYearIntervalTime(LocalDateTime localDateTime) {
    42. LocalDateTime dateTime = getDateTime(localDateTime);
    43. return ImmutablePair.of(dateTime.with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0),
    44. dateTime.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59));
    45. }
    46. public static LocalDateTime getDateTime(LocalDateTime localDateTime) {
    47. LocalDateTime dateTime = localDateTime;
    48. return Objects.isNull(dateTime) ? LocalDateTime.now() : localDateTime;
    49. }

            还可以参考:介绍 java 8 TemporalAdjuster 类型_梦想画家的博客-CSDN博客_temporaladjuster

  • 相关阅读:
    父组件与子组件的属性透传
    百数报表——帮助企业数据化重要工具
    java:找不到符号
    【无标题】
    推荐 3 个腾讯开源的 GitHub 项目
    《向量数据库指南》——向量数据库 有必要走向专业化吗?
    推荐系统之K近邻(KNN)算法
    go module 导入本地包
    《Qt开发》基于QWT的柱形图绘制
    java计算机毕业设计基于安卓Android的在线心理咨询与健康App(源码+系统+mysql数据库+Lw文档)
  • 原文地址:https://blog.csdn.net/swadian2008/article/details/126468437