import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author 954L
* @create 2019/12/31 20:42
*/
public class Main {
//两个list数据比较时,将其中一个转为map,进行查找可大大降低时间复杂度
public static void main(String[] args) {
List<User> list1 = new ArrayList<>();
for (int i = 0; i <= 600000; i++){
list1.add(User.builder().name("呵呵" + i).build());
}
List<User> list2 = new ArrayList<>(list1);
Map<String, User> list2Map = list2.stream()
.collect(Collectors.toMap(User::getName, Function.identity()));
long startTime = System.currentTimeMillis();
for (User user1: list1){
User user = list2Map.get(user1.getName());
if (user != null){
//TODO
}
}
}
}
双重for循环list比较,优化为Map
于 2022-08-31 09:51:42 首次发布