挑战全网最全CollectionUtils工具类解析Springframework CollectionUtils (二)

本文详细探讨了Springframework中的CollectionUtils工具类,通过分析其依赖和提供的方法,对比了其与Java内置处理集合的优劣。作者提供了相关链接以供深入研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为什么这个是工具是这个依赖地址:
https://blog.youkuaiyun.com/weixin_44458365/article/details/119448509
依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--或者-->
        <dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-core</artifactId>
     		<version>5.3.1</version>
      		<scope>compile</scope>
    	</dependency>

方法:
在这里插入图片描述
实验室:

Emp empIns = new Emp().setDepartId(1L).setDepartName("molizhuangquanquan").setEmail("94655718@qq.com").setDepartNumber(23);
        Emp empIn = new Emp().setDepartId(1l).setDepartName("chenpi1").setEmail("94655718@qq.com").setDepartNumber(10);
        Emp empInss = new Emp().setDepartId(2l).setDepartName("chenpi1").setEmail("94655718@qq.com").setDepartNumber(10);
        List<Emp> empList1 = Arrays.asList(empIn, empIns);
        List<Emp> empList2 = Arrays.asList( empInss);

        HashMap<Long, List<Emp>> hashMaop = new HashMap<>();
        hashMaop.put(1L, empList1);
        hashMaop.put(2L, empList2);

        String[] arrayA = new String[] { "1", "2", "3", "4"};
        String[] arrayB = new String[] { "3", "4", "5", "6" };
        List<String> listA = Arrays.asList(arrayA);
        List<String> listB = Arrays.asList(arrayB);

        Properties properties = Environment.getProperties();

                MultiValueMap<Long, Emp> objectObjectMultiValueMap = CollectionUtils.toMultiValueMap(hashMaop);
        System.out.println("objectObjectMultiValueMap = " + objectObjectMultiValueMap);
		// objectObjectMultiValueMap = {1=[Emp(departId=1, departName=chenpi1, email=94655718@qq.com, departNumber=10), Emp(departId=1, departName=molizhuangquanquan, email=94655718@qq.com, departNumber=23)], 2=[Emp(departId=2, departName=chenpi1, email=94655718@qq.com, departNumber=10)]}
		
        List<Integer> intList = new ArrayList<>();
        CollectionUtils.mergeArrayIntoCollection(arrayA, intList);
        System.out.println("intList = " + intList);
        // intList = [1, 2, 3, 4]

        List<Integer> objects = (List<Integer>) CollectionUtils.arrayToList(arrayA);
        System.out.println("objects = " + objects);
        // objects = [1, 2, 3, 4]

        List<Long> lonList = (List<Long>) CollectionUtils.arrayToList(arrayA);
        System.out.println("lonList = " + lonList);
		//lonList = [1, 2, 3, 4]
		
        LinkedHashMap<Object, Object> linkedHashMap = CollectionUtils.newLinkedHashMap(10);
        CollectionUtils.mergePropertiesIntoMap(properties, new HashMap<>());

        boolean b1 = CollectionUtils.containsInstance(intList, 1);
        System.out.println("b1 = " + b1);
		// b1 = false
		
        String firstMatch = CollectionUtils.findFirstMatch(listB, listA);
        System.out.println("firstMatch = " + firstMatch);
        // firstMatch = 3

        String valueOfType = CollectionUtils.findValueOfType(listA, String.class);
        System.out.println("valueOfType = " + valueOfType);
		// valueOfType = null
		
        boolean b = CollectionUtils.hasUniqueObject(listA);
        System.out.println("b = " + b);
		// b = false
		
        Class<?> afd = CollectionUtils.findCommonElementType(empList1);
        System.out.println("afd = " + afd);
		// afd = class com.example.integrateSpringdemo.entity.Emp
		
        String s = CollectionUtils.firstElement(listA);
        System.out.println("s = " + s);
		// s = 1
		
        String s1 = CollectionUtils.lastElement(listA);
        System.out.println("s1 = " + s1);
		// s1 = 4
		
        Vector<String> strings = new Vector<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        strings.add("4");
        Enumeration<String> enm = strings.elements();
        boolean contains = CollectionUtils.contains(enm, 1);
        System.out.println("contains = " + contains);
        // contains = false

说句心里话,还不如我用java自带的去处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值