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;
}
}
本站主要用于日常笔记的记录和生活日志。本站不保证所有内容信息可靠!(大多数文章属于搬运!)如有版权问题,请联系我立即删除:“abcdsjx@126.com”。
QQ: 1164453243
邮箱: abcdsjx@126.com