List填加序号

package com.example.demo.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qzz
 * @date 2023/3/27
 */
public class ListUtils {

    public static void main(String[] args) {
        //构造list数据,模拟数据库请求列表
        List<UserResponse> userList = new ArrayList<>();
        UserResponse response = new UserResponse();
        response.setUserName("张三");
        userList.add(response);

        response = new UserResponse();
        response.setUserName("里斯");
        userList.add(response);

        response = new UserResponse();
        response.setUserName("咋呼");
        userList.add(response);

        //给list添加序号
        System.out.println(JSONObject.toJSONString(getUserList(userList)));
    }


    /**
     * 给List添加序号
     * @param userList
     * @return
     */
    public static List<UserResponse> getUserList(List<UserResponse> userList) {
        //初始化序号
        Integer[] arr = {1};
        //图片
        if(userList.size()>0){
            //添加序号、附件类型 1:图片 2:视频 3:语音
            userList = userList.stream().peek(e -> {e.setIndex(arr[0] ++ );}).collect(Collectors.toList());
        }
        arr[0] = 1;
        return userList;
    }
}

执行结果:

"D:\Program Files\Java\jdk-11.0.11\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=62051:D:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\bin" -Dfile.encoding=UTF-8 -classpath F:\student-project\demo\target\classes;D:\mvnrepository\org\springframework\boot\spring-boot-starter-web\2.7.9\spring-boot-starter-web-2.7.9.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter\2.7.9\spring-boot-starter-2.7.9.jar;D:\mvnrepository\org\springframework\boot\spring-boot\2.7.9\spring-boot-2.7.9.jar;D:\mvnrepository\org\springframework\boot\spring-boot-autoconfigure\2.7.9\spring-boot-autoconfigure-2.7.9.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-logging\2.7.9\spring-boot-starter-logging-2.7.9.jar;D:\mvnrepository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\mvnrepository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\mvnrepository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\mvnrepository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\mvnrepository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\mvnrepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\mvnrepository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-json\2.7.9\spring-boot-starter-json-2.7.9.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackson-databind\2.13.5\jackson-databind-2.13.5.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackson-annotations\2.13.5\jackson-annotations-2.13.5.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackson-core\2.13.5\jackson-core-2.13.5.jar;D:\mvnrepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.5\jackson-datatype-jdk8-2.13.5.jar;D:\mvnrepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.5\jackson-datatype-jsr310-2.13.5.jar;D:\mvnrepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.5\jackson-module-parameter-names-2.13.5.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-tomcat\2.7.9\spring-boot-starter-tomcat-2.7.9.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.71\tomcat-embed-core-9.0.71.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-el\9.0.71\tomcat-embed-el-9.0.71.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.71\tomcat-embed-websocket-9.0.71.jar;D:\mvnrepository\org\springframework\spring-web\5.3.25\spring-web-5.3.25.jar;D:\mvnrepository\org\springframework\spring-beans\5.3.25\spring-beans-5.3.25.jar;D:\mvnrepository\org\springframework\spring-webmvc\5.3.25\spring-webmvc-5.3.25.jar;D:\mvnrepository\org\springframework\spring-aop\5.3.25\spring-aop-5.3.25.jar;D:\mvnrepository\org\springframework\spring-context\5.3.25\spring-context-5.3.25.jar;D:\mvnrepository\org\springframework\spring-expression\5.3.25\spring-expression-5.3.25.jar;D:\mvnrepository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\mvnrepository\org\springframework\spring-core\5.3.25\spring-core-5.3.25.jar;D:\mvnrepository\org\springframework\spring-jcl\5.3.25\spring-jcl-5.3.25.jar;D:\mvnrepository\com\alibaba\fastjson\2.0.1\fastjson-2.0.1.jar;D:\mvnrepository\com\alibaba\fastjson2\fastjson2-extension\2.0.1\fastjson2-extension-2.0.1.jar;D:\mvnrepository\com\alibaba\fastjson2\fastjson2\2.0.1\fastjson2-2.0.1.jar com.example.demo.utils.ListUtils
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.alibaba.fastjson2.util.JDKUtils (file:/D:/mvnrepository/com/alibaba/fastjson2/fastjson2/2.0.1/fastjson2-2.0.1.jar) to field java.lang.StringUTF16.HI_BYTE_SHIFT
WARNING: Please consider reporting this to the maintainers of com.alibaba.fastjson2.util.JDKUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[{"index":1,"userName":"张三"},{"index":2,"userName":"里斯"},{"index":3,"userName":"咋呼"}]

Process finished with exit code 0

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值