当前位置:网站首页>On the clever use of stream and map
On the clever use of stream and map
2022-07-06 17:05:00 【Smart popcorn】
The title is like this
// hypothesis db There are two such tables in The data are 100w The final thing to return is Provice Tabular List aggregate If status by fasle Then don't go back How to be in java Level solution ?
public class Mydemo {
static ArrayList<Province> provinces = new ArrayList<>();
static ArrayList<ProvinceStatus> provinceStatusList = new ArrayList<>();
static {
for (int i = 0; i < 10000; i++) {
Province province = new Province();
ProvinceStatus provinceStatus = new ProvinceStatus();
province.setName(UUID.randomUUID().toString());
province.setProvinceId(i);
provinceStatus.setProvinceId(i);
provinceStatus.setStatus(new Random().nextBoolean());
// add to
provinces.add(province);
provinceStatusList.add(provinceStatus);
}
System.out.println(" The size of the displayed list is :" + provinceStatusList.stream().filter(ProvinceStatus::getStatus).count());
}
public static void main(String[] args) {
ArrayList<Province> resList = new ArrayList<>();
System.out.println(" The number of final displays :" + resList.size());
}
}
@Data
class Province {
private Integer provinceId;
private String name;
}
@Data
class ProvinceStatus {
private Integer provinceId;
private Boolean status;
}
The answer is as follows , If you have better ideas, please leave a message !
public static void main(String[] args) {
// double for loop ? No use hash It's a good idea
long start1 = System.currentTimeMillis();
System.out.println();
Map<Integer, Boolean> map = provinceStatusList.stream()
.collect(Collectors.toMap(ProvinceStatus::getProvinceId, ProvinceStatus::getStatus));
ArrayList<Province> resList = new ArrayList<>();
for (Province province : Mydemo.provinces) {
if (map.get(province.getProvinceId())) {
// Show these
resList.add(province);
}
}
long start2 = System.currentTimeMillis();
System.out.println(" when :" + (start2 - start1));
System.out.println(" The number of final displays :" + resList.size());
}
}
When there is a large amount of data , Parallel streams are slower , It should be because there is no calculation , After all, parallel flow is suitable for CPU Intensive tasks , That is, when a large number of calculations are involved .
边栏推荐
猜你喜欢
MySQL string function
Resume of a microservice architecture teacher with 10 years of work experience
Eight part essay that everyone likes
Shell_ 04_ Shell script
Solr standalone installation
was unable to send heartbeat
~74 JD top navigation bar exercise
koa中间件
Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
Mongodb learning notes
随机推荐
J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
Install docker under windows10 (through Oracle VM VirtualBox)
~72 horizontal and vertical alignment of text
我走过最迷的路,是字节跳动程序员的脑回路
汇编课后作业
Activiti directory (III) deployment process and initiation process
在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
唯有学C不负众望 TOP1环境配置
redux使用说明
姚班智班齐上阵,竞赛高手聚一堂,这是什么神仙编程大赛?
Full record of ByteDance technology newcomer training: a guide to the new growth of school recruitment
"One year after graduation, I won ACL best paper"
In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
DS18B20數字溫度計系統設計
After the subscript is used to assign a value to the string type, the cout output variable is empty.
yum install xxx报错
~74 JD top navigation bar exercise
Solr word segmentation analysis
Compile homework after class
Activit零零碎碎要人命的坑