Java优化IN的写法

白色玫瑰 程序猿

时间: 2023-07-11 阅读: 14 字数:4245

{}
sd f

目录


package com.cyc.usercenter.controller;

import com.cyc.usercenter.domain.User;

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

public class TestController {


   public static void main(String[] args) {

      List<User> listTotal = new ArrayList<>();
      List<User> list = list();
      //总条数
      int listCount = list.size();
      //TODO 设置查询的条数,可根据实际情况查询的量来确定,如果查询的量大,设置的值对应大
      int tag = 100;
      if (listCount > 5000) {
         tag = 200;
      } else if (listCount > 50000) {
         tag = 500;
      }


      int selectCount = 0;
      if (listCount % tag == 0) {
         selectCount = listCount / tag;
      } else {
         selectCount = listCount / tag + 1;
      }

      System.out.println("需要查询次数 = " + selectCount);
      long start = System.currentTimeMillis();

      //执行批量查询操作,selectCount次数
      for (int i = 0; i < selectCount; i++) {
         int index = i * tag;
         int ent = tag * (i + 1);
         if (i == selectCount - 1) {
            ent = listCount;
         }
         List<User> users = list.subList(index, ent);
//         System.out.println("users = " + "次数" + (i + 1) + "----" + users);
         listTotal.addAll(users);
      }

      long b = System.currentTimeMillis() - start;
      System.out.println("执行完查询所需秒数: " + b);
      System.out.println("listTotal = " + listTotal);


   }

   private static List<User> list() {
      List<User> list = new ArrayList<>();
      for (int i = 1; i <= 10000; i++) {
         User u = new User();
         u.setId(i);
         u.setName("张三" + i);
         list.add(u);
      }
      return list;
   }


}
package com.cyc.usercenter.controller;

import com.cyc.usercenter.domain.User;

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

public class TestController {


   public static void main(String[] args) {

      List<User> listTotal = new ArrayList<>();
      List<User> list = list();
      //总条数
      int listCount = list.size();
      //TODO 设置查询的条数,可根据实际情况查询的量来确定,如果查询的量大,设置的值对应大
      int tag = 100;
      if (listCount > 5000) {
         tag = 200;
      } else if (listCount > 50000) {
         tag = 500;
      }


      int selectCount = 0;
      if (listCount % tag == 0) {
         selectCount = listCount / tag;
      } else {
         selectCount = listCount / tag + 1;
      }

      System.out.println("需要查询次数 = " + selectCount);
      long start = System.currentTimeMillis();

      //执行批量查询操作,selectCount次数
      for (int i = 0; i < selectCount; i++) {
         int index = i * tag;
         int ent = tag * (i + 1);
         if (i == selectCount - 1) {
            ent = listCount;
         }
         List<User> users = list.subList(index, ent);
//         System.out.println("users = " + "次数" + (i + 1) + "----" + users);
         listTotal.addAll(users);
      }

      long b = System.currentTimeMillis() - start;
      System.out.println("执行完查询所需秒数: " + b);
      System.out.println("listTotal = " + listTotal);


   }

   private static List<User> list() {
      List<User> list = new ArrayList<>();
      for (int i = 1; i <= 10000; i++) {
         User u = new User();
         u.setId(i);
         u.setName("张三" + i);
         list.add(u);
      }
      return list;
   }


}


原文地址:https://blog.csdn.net/chai1230/article/details/104643832?ops_request_misc=&request_id=7358bddffa5a4b5a842fdc40cf8ff8c5&biz_id=&utm_medium=distribute.pc_search_result.none-task-blog-2~all~koosearch~default-4-104643832-null-null.142^v88^insert_down28v1,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/f65cbfa6c8954da7a64de393f2545ea1
最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复