Java自带常用工具类

这篇具有很好参考价值的文章主要介绍了Java自带常用工具类。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、Objects:专为操作Java对象而生的工具类

1.对象判空

Objects 的 isNull 方法用于判断对象是否为空,而 nonNull 方法判断对象是否不为空。

示例代码如下:

import java.util.Objects;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Objects工具测试
 */
public class ObjectTest {
    public static void main(String[] args) {
        String name = null;
        // 输出true
        System.out.println(Objects.isNull(name));
        // 输出false
        System.out.println(Objects.nonNull(name));
    }
}

2.判断两个对象是否相等

我们经常需要判断两个对象是否相等,Objects 给我们提供了 equals 方法。

示例代码如下:

import java.util.Objects;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Objects工具测试
 */
public class ObjectTest {
    public static void main(String[] args) {
        String name1 = "a";
        String name2 = "A";
        // 比较两个对象是否相等 输出false
        System.out.println(Objects.equals(name1, name2));
    }
}

如果是两个对象之间的比较,还需要重写对象的equals和hashCode方法。

我们定义一个学生类 没有实现equals方法

/**
 * @author qinxun
 * @date 2023-06-02
 * @Descripion: 学生类
 */
public class Student {

    private String name;

    private String school;

    public Student() {
    }

    public Student(String name, String school) {
        this.name = name;
        this.school = school;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }
    
}

在测试类比较两个对象,发现两个对象并不相等

import java.util.Objects;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Objects工具测试
 */
public class ObjectTest {
    public static void main(String[] args) {
        Student student1 = new Student("qx", "桂林");
        Student student2 = new Student("qx", "桂林");
        // 比较两个对象是否相等 输出false
        System.out.println(Objects.equals(student1, student2));
    }
}

接下来我们在实体类中实现equals方法

import java.util.Objects;

/**
 * @author qinxun
 * @date 2023-06-02
 * @Descripion: 学生类
 */
public class Student {

    private String name;

    private String school;

    public Student() {
    }

    public Student(String name, String school) {
        this.name = name;
        this.school = school;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name) && Objects.equals(school, student.school);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, school);
    }
}

我们再次调用测试方法

import java.util.Objects;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Objects工具测试
 */
public class ObjectTest {
    public static void main(String[] args) {
        Student student1 = new Student("qx", "桂林");
        Student student2 = new Student("qx", "桂林");
        // 比较两个对象是否相等 输出true
        System.out.println(Objects.equals(student1, student2));
    }
}

执行程序结果返回:

true

3.对象为空时抛异常

如果我们想在对象为空时,抛出空指针异常可以使用 Objects 的 requireNonNull 方法。

示例代码如下:

import java.util.Objects;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Objects工具测试
 */
public class ObjectTest {
    public static void main(String[] args) {
        String name = null;
        System.out.println(Objects.requireNonNull(name, "名字为空"));
    }
}

执行程序,抛出了空指针异常。

Exception in thread "main" java.lang.NullPointerException: 名字为空
	at java.util.Objects.requireNonNull(Objects.java:228)
	at demo2.ObjectTest.main(ObjectTest.java:13)

4.比较两个数组

deepEquals() 用于比较两个数组的对象 

import java.util.Objects;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Objects工具测试
 */
public class ObjectTest {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3};
        int[] array2 = {1, 2, 3};
        // 输出true
        System.out.println(Objects.deepEquals(array1, array2));
    }
}

二、Arrays:专为数组而生的工具类

1.创建数组

  • copyOf:复制指定的数组,截取或用 null 填充
  • copyOfRange:复制指定范围内的数组到一个新的数组
  • fill:对数组进行填充

示例代码如下:

import java.util.Arrays;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: 数组工具类测试
 */
public class ArrayTest {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int[] revised = Arrays.copyOf(arr, 3);
        // 输出[1, 2, 3]
        System.out.println(Arrays.toString(revised));
        int[] expanded = Arrays.copyOf(arr, 5);
        // 输出[1, 2, 3, 4, 0]
        System.out.println(Arrays.toString(expanded));

        // 第二个参数包含,第三个参数不包含, 所以从第二位开始截取,截取到第三位
        int[] range = Arrays.copyOfRange(arr, 1, 3);
        // 输出[2, 3]
        System.out.println(Arrays.toString(range));

        
        int[] temp = new int[4];
        // 数组填充
        Arrays.fill(temp, 2);
        // 输出[2, 2, 2, 2]
        System.out.println(Arrays.toString(temp));
    }
}

2.比较数组

Arrays 类的 equals() 方法用来判断两个数组是否相等

import java.util.Arrays;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: 数组工具类测试
 */
public class ArrayTest {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int[] arr1 = {2, 3, 4};
        // 输出false
        System.out.println(Arrays.equals(arr, arr1));
    }
}

3.数组排序

Arrays 类的 sort() 方法用来对数组进行排序

import java.util.Arrays;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: 数组工具类测试
 */
public class ArrayTest {
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 1};
        // 排序
        Arrays.sort(arr);
        // 输出[1, 2, 3, 4]
        System.out.println(Arrays.toString(arr));

    }
}

4.数组转Stream流

Arrays 类的 stream() 方法可以将数组转换成流

import java.util.Arrays;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: 数组工具类测试
 */
public class ArrayTest {
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 1};
        // 输出3 4 2 1 
        Arrays.stream(arr).forEach(System.out::println);

    }
}

5.打印数组

使用 Arrays.toString()

import java.util.Arrays;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: 数组工具类测试
 */
public class ArrayTest {
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 1};
        // 输出[3, 4, 2, 1]
        System.out.println(Arrays.toString(arr));

    }
}

6.数组转 List

使用Arrays.asList方法

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: 数组工具类测试
 */
public class ArrayTest {
    public static void main(String[] args) {
        Integer[] arr = {3, 4, 2, 1};
        // 数组转list
        List<Integer> list = new ArrayList<>(Arrays.asList(arr));
        // 输出[3, 4, 2, 1]
        System.out.println(list);
    }
}

三、Collections:专为集合框架而生的工具类

1.排序操作

  • reverse(List list):反转顺序
  • shuffle(List list):洗牌,将顺序打乱
  • sort(List list):自然升序
  • sort(List list, Comparator c):按照自定义的比较器排序
  • swap(List list, int i, int j):将 i 和 j 位置的元素交换位置
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Collections工具类测试
 */
public class CollectionTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("d");
        list.add("c");
        list.add("e");

        System.out.println("原始顺序:" + list);

        // 反转
        Collections.reverse(list);
        System.out.println("反转后:" + list);

        // 洗牌
        Collections.shuffle(list);
        System.out.println("洗牌后:" + list);

        // 自然升序
        Collections.sort(list);
        System.out.println("自然升序后:" + list);

        // 交换
        Collections.swap(list, 2, 4);
        System.out.println("交换后:" + list);

    }
}
原始顺序:[a, b, d, c, e]
反转后:[e, c, d, b, a]
洗牌后:[a, e, c, b, d]
自然升序后:[a, b, c, d, e]
交换后:[a, b, e, d, c]

2.查找操作

  • binarySearch(List list, Object key):二分查找法,前提是 List 已经排序过了
  • max(Collection coll):返回最大元素
  • max(Collection coll, Comparator comp):根据自定义比较器,返回最大元素
  • min(Collection coll):返回最小元素
  • min(Collection coll, Comparator comp):根据自定义比较器,返回最小元素
  • fill(List list, Object obj):使用指定对象填充
  • frequency(Collection c, Object o):返回指定对象出现的次数
/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Collections工具类测试
 */
public class CollectionTest {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        // 输出1
        System.out.println(Collections.binarySearch(list, 2));

        // 输出3
        System.out.println(Collections.max(list));

        // 输出1
        System.out.println(Collections.min(list));

        // 输出1
        System.out.println(Collections.frequency(list, 2));
    }
}

3.同步控制

ArrayList 是线程不安全的,没法在多线程环境下使用,那 Collections 工具类中提供了多个 synchronizedXxx 方法,这些方法会返回一个同步的对象,从而解决多线程中访问集合时的安全问题。

我们先使用普通的ArrayList来实现多线程的数据添加

import java.util.ArrayList;
import java.util.List;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Collections工具类测试
 */
public class CollectionTest {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> synchronizedList = new ArrayList<>();

        // 使用线程的方式添加两个数据到集合
        Thread thread = new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                synchronizedList.add(i);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();

        Thread thread1 = new Thread(() -> {
            for (int i = 11; i <= 20; i++) {
                synchronizedList.add(i);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread1.start();
        Thread.sleep(1000);
        // 输出[1, 11, 12, 2, 13, 3, 4, 14, 5, 15, null, 16, 7, 17, 8, 18, 9, 19, 10, 20]
        System.out.println(synchronizedList);

    }
}

我们发现不是我们需要的结果。

接下来我们使用Collections.synchronizedList方式创建可以处理多线程的集合。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Collections工具类测试
 */
public class CollectionTest {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> synchronizedList = Collections.synchronizedList(new ArrayList<>());
        // 使用线程的方式添加两个数据到集合
        Thread thread = new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                synchronizedList.add(i);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();

        Thread thread1 = new Thread(() -> {
            for (int i = 11; i <= 20; i++) {
                synchronizedList.add(i);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread1.start();
        Thread.sleep(1000);
        // 输出[1, 11, 2, 12, 3, 13, 14, 4, 15, 5, 16, 6, 17, 7, 18, 8, 19, 9, 20, 10]
        System.out.println(synchronizedList);
    }
}

实现了我们需要的结果。

4.不可变集合

  • emptyXxx():制造一个空的不可变集合
  • singletonXxx():制造一个只有一个元素的不可变集合
  • unmodifiableXxx():为指定集合制作一个不可变集合
import java.util.Collections;
import java.util.List;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Collections工具类测试
 */
public class CollectionTest {
    public static void main(String[] args) throws InterruptedException {
        List<Object> emptyList = Collections.emptyList();
        emptyList.add(3);
        System.out.println(emptyList);
    }
}

运行程序报错

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)
	at demo2.CollectionTest.main(CollectionTest.java:16)

5.其他文章来源地址https://www.toymoban.com/news/detail-475976.html

  • addAll(Collection<? super T> c, T... elements),往集合中添加元素
  • disjoint(Collection<?> c1, Collection<?> c2),判断两个集合是否没有交集
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author qinxun
 * @date 2023-06-08
 * @Descripion: Collections工具类测试
 */
public class CollectionTest {
    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "hello", "world");
        // 输出[hello, world]
        System.out.println(list);

        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);

        List<Integer> list2 = new ArrayList<>();
        list2.add(1);
        list2.add(2);
        
        System.out.println("是否有交集:" + Collections.disjoint(list1, list2));
    }
}

到了这里,关于Java自带常用工具类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java常用工具之Scanner类

    Java 的 Scanner 类是一个方便在控制台扫描用户输入的工具类,虽然它也可以扫描文件内容,但我们通常更喜欢它扮演前面的角色,因为扫描文件可以通过文件流来完成。 接下来,我们通过几个简单的示例讲一下 Scanner 类。 通常,我们会使用 Scanner 类来扫描控制台输入,尤其是

    2024年02月05日
    浏览(42)
  • JAVA开发常用测试工具

    什么是junit JUnit 是一个用于编写和运行单元测试的开源框架,是Java 开发中最常用的单元测试框架之一。它为 Java 开发者提供了一种简单、灵活且可扩展的方式来编写自动化测试代码,并帮助开发者进行单元测试的管理和执行。 JUnit 提供了一组注解和断言方法,使开发者能够

    2024年04月25日
    浏览(41)
  • 常用的数据可视化工具有哪些?要操作简单的

    随着数据量的剧增,对分析效率和数据信息传递都带来了不小的挑战,于是数据可视化工具应运而生,通过直观形象的图表来展现、传递数据信息,提高数据分析报表的易读性。那么,常用的操作简单数据可视化工具有哪些? 以下便是一些比较常见,且相对来说操作简单、易

    2024年02月11日
    浏览(41)
  • Java常用第三方工具类

    一、Apache StringUtils:专为Java字符串而生的工具类 首先引入依赖: 1.字符串判空 isEmpty: 判断null和\\\"\\\" isNotEmpty:判断null和\\\"\\\" isBlank:判断null和\\\"\\\"和\\\" \\\" isNotBlank:判断null和\\\"\\\"和\\\" \\\" 示例代码如下: 执行结果: 2.分割字符串 使用StringUtils的split()方法分割字符串成数组。 示例代码如下:

    2024年02月08日
    浏览(42)
  • java项目常用工具jar包方法整理

    一、commons-lang3 1、 字符串,数字,日期,数组等工具类 StringUtils 字符串工具类 StringEscapeUtils NumberUtils 数字工具类 ArrayUtils 数组工具类 RandomUtils 随机数工具类 RandomStringUtils 随机字符串工具类 DateUtils 日期工具类 DateFormatUtils时间格式化 DurationFormatUtils时间段格式化 StopWatch 秒表

    2024年02月11日
    浏览(26)
  • Java常用的加密解密工具类

    在软件开发中,数据的安全性是一个重要的考虑因素。为了保护敏感数据,我们经常需要对数据进行加密和解密操作。为了简化加密解密操作,提高开发效率,我们可以使用一个常用的加密解密工具类。本文将介绍一个常用的 Java 加密解密工具类,并提供详细的使用说明和示

    2024年02月02日
    浏览(38)
  • java开发常用的工具以及配置类

    今天接着上一篇博文,聊 java开发常用的工具以及配置类。 13 日期操作相关的java类。 比如在开发预约旅游 预约体检 预约售票等相关和日期相关的业务,需要获取日期以及判断是一周的哪一天等。 14 处理请求路径的URL工具类 比如,有时需要根据传入的路径url获取请求参数或

    2024年02月08日
    浏览(38)
  • F12-开发者工具常用操作与使用说明之网络network

    开发十分钟,调试三小时。 在开发过程中,我们希望是用更多的时间来创造,而不是被各种各样的问题所困扰。 尤其是在跟后端对接口的时候,是否能够快速的发现问题,就显得很重要。 network面板中提供了哪些功能,能否让我们解脱一点呢? 这是查看请求启动器的调用链

    2024年02月07日
    浏览(28)
  • 私藏工具分享 300+++ 编程使用工具,js、css、动画插件、可视化、文件操作、音视频、智能化、编辑器、2D3D、其他常用工具、跨平台框架、代码工具、图像工具、文件转换、vue3生态、后端常用插件

    目录 Javascript 工具 可视化 动画插件 文件操作 音视频 智能化 编辑器 2D 3D 其他常用js 跨平台 框架 Flutter Electron Uniapp CSS 动画 工具 UI 编程工具 代码工具 图像工具 文件转换 Vue3生态工具 1.Web UI库 2.移动UI库 3.相关工具 4.可视化 5.插件 6.相关生态 7.动画 8.音视频 全栈-后端 插件

    2024年02月21日
    浏览(42)
  • Java常用的几种JSON解析工具

    一、Gson:Google开源的JSON解析库 1.添加依赖 示例代码如下: 二、fastjson:阿里巴巴开源的JSON解析库 1.添加依赖 JSON.toJSONString(obj):用于序列化对象,转成json数据。 JSON.parseObject(obj,class): 用于反序列化对象,转成数据对象。 JSON.parseArray():把 JSON 字符串转成集合 示例代码如下: 2.使

    2024年02月09日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包