首页 > 编程语言 >【JAVA基础】List处理

【JAVA基础】List处理

时间:2022-11-29 15:34:46浏览次数:38  
标签:JAVA String 处理 list partition List Lists new

List处理

List使用Lists.partition()分片

public static <T> List<List<T>> partition(List<T> list, int size)

参数:该方法接受两个参数:

list:该列表将根据分区大小分为子列表。
size:每个子列表的期望大小。最后一个子列表的大小可能较小。

返回值:该方法返回连续子列表的列表。每个子列表(除了最后一个子列表)的大小都等于分区大小。

异常:如果分区大小为非正数,则Lists.partition()方法将引发IllegalArgumentException。

以下示例说明了上述方法的实现:

范例2:

// Java code to show implementation of 
// Guava's Lists.partition() method 
  
import com.google.common.collect.Lists; 
import java.util.Arrays; 
import java.util.List; 
  
class GFG { 
  
    // Driver's code 
    public static void main(String[] args) 
    { 
  
        // Creating a List of Characters 
        List<Character> myList 
            = Arrays.asList('H', 'E', 'L', 'L', 'O', 
                            'G', 'E', 'E', 'K', 'S'); 
  
        // Using Lists.partition() method to divide 
        // the original list into sublists of the same 
        // size, which are just views of the original list. 
        // The final list may be smaller. 
        List<List<Character> > lists 
            = Lists.partition(myList, 3); 
  
        // Displaying the sublists 
        for (List<Character> sublist:lists) 
            System.out.println(sublist); 
    } 
}

输出:
[H, E, L]
[L, O, G]
[E, E, K]
[S]
/**
     * 接收集团物料
     */
    @ApiOperation(value = "集团物料接收接口")
    @Permission(permissionPublic = true)
    @PostMapping("/receiveinfo")
    public String receiveinfo(@PathVariable Long organizationId,@RequestBody String reqContent) {

        String esbCode = "0";
        String esbDesc = "接收成功";
        JtMdmMaterialReqDTO jtMdmMaterialReqDTO = new JtMdmMaterialReqDTO();
        List<JtMdmResultDataInfoDTO> jtMdmResultDataInfoDTOList = new ArrayList<>();
        try{
            //String 转json
            jtMdmMaterialReqDTO = JSONObject.parseObject(reqContent,JtMdmMaterialReqDTO.class);

            List<JtMdmMaterialDataInfosDTO> jtMdmMaterialDataInfosDTOList = jtMdmMaterialReqDTO.getDATAINFOS();
            //切片
            List<List<JtMdmMaterialDataInfosDTO>> list = Lists.partition(jtMdmMaterialDataInfosDTOList,1);
            for (List<JtMdmMaterialDataInfosDTO> jtMdmMaterialDataInfosDTOS : list) {
                List<JtMdmMdMaterial> jtMdmMdMaterialList = new ArrayList<>();
                //报文转JtMdmMdMaterial
                jtMdmMaterialDataInfosDTOS.forEach(jtMdmMaterialDataInfosDTO -> {
                    jtMdmMdMaterialList.add(jtMdmMaterialDataInfosDTO.getDATAINFO());
                });

                List<JtMdmResultDataInfoDTO> pieceJtMdmResultDataInfoDTO = this.jtMdmMdMaterialService.receiveInfo(jtMdmMaterialReqDTO.getUUID(),jtMdmMdMaterialList);
                jtMdmResultDataInfoDTOList.addAll(pieceJtMdmResultDataInfoDTO);

            }

        }catch (Exception e){
            esbCode = "1";
            esbDesc = e.getMessage();
        }


        JtMdmResultEsbDTO jtMdmResultEsbDTO = new JtMdmResultEsbDTO
                                            .Builder()
                                            .setESB(new JtMdmResultDTO
                                                    .Builder(esbCode,esbDesc)
                                                    .setDATA(new JtMdmResultDataDTO
                                                            .Builder()
                                                            .setDATAINFOS(new JtMdmResultDataInfosDTO
                                                                        .Builder(jtMdmMaterialReqDTO.getUUID())
                                                                        .setDATAINFO(jtMdmResultDataInfoDTOList)
                                                                    .build())
                                                            .build())
                                                    .build())
                                            .build();
        return JSONObject.toJSONString(jtMdmResultEsbDTO);

    }

标签:JAVA,String,处理,list,partition,List,Lists,new
From: https://www.cnblogs.com/SPiCa24/p/16935503.html

相关文章

  • Java的ExecutorService的shutdownNow()方法并不能保证一定会结束线程的解决办法
    这几天使用ExecutorService的时候遇到了在Task内部进行读取文件操作而导致死循环的问题,当我试图调用shutdownNow()方法的时候,发现并不是像我预想的一样会理解结束线程。我......
  • day07_java_数组
    d07Java数组(p51-p59)1.什么是数组?数组就是一组数的集合。数组是相同类型数据的有序集合。数组描述的是相同类型的若干个数据,按照一定的先后顺序排列组合而成。......
  • Java8新特性
    1.利用distinct去重 并保持数组的顺序ArrayList<Integer>list=newArrayList();list.add(7);list.add(5);list.add(9);list.a......
  • 光纤加速计算 383-高速信号处理板 XCKU060的双路QSFP+光纤PCIe 卡 XCKU060板卡
    基于kintexUltraScaleXCKU060的双路QSFP+光纤PCIe卡 一、板卡概述       本板卡系北京太速科技自主研发,基于Xilinx UltraScale Kintex系列FP......
  • java如何高效地读取一个超大文件?(四种方式分析对比)
    读取大文件的四种方式本地压缩了一个文件夹,大概500M左右。虽然不是很大但是,相对还可以。方法1:Guava读取Stringpath="G:\\java书籍及工具.zip";Files.readLines(new......
  • java基础多线程之共享数据
    java基础巩固笔记5-多线程之共享数据线程范围内共享数据ThreadLocal类多线程访问共享数据几种方式本文主要总结线程共享数据的相关知识,主要......
  • Java 注解和反射(六)获取泛型,注解信息
    反射操作泛型**Java采用泛型擦出的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据的安全性和免除强制类型转换问题,但是,一旦编译完成,所有和泛型有关的类......
  • mysql-单行处理函数
    1单行处理函数lower()对于输出转换成小写upper()对于输出转换成大写substr()取子字符串下标从1开始length()去长度concat()将字符串进行拼接......
  • JAVA面试题--Dubbo
    Dubbo1.Dubbo是什么?2.为什么要用Dubbo?3.Dubbo和Dubbox有什么区别?4.dubbo都支持什么协议,推荐用哪种?5.Dubbo需要Web容器吗?6.Dubbo内置了哪几种服务容器?7.Dubbo......
  • JAVA面试题--Java高并发
    Java高并发1.什么是进程2.什么是线程3.进程间如何通讯4.线程间如何通讯5.同步和异步有何不同,在什么情况下分别使用它们?举例说明6.进程调度算法7.Java中Unsafe类详......