1. List去重合并
// 两个不同的list 根据id去重合并
List<T> list = new ArrayList<>();
List<T> list1 = new ArrayList<>();
list.forEach(o -> {
list1.removeIf(next ->next.getId().equals(o.getId()));
});
list.addAll(list1)
2.List取某属性的所有值/List转数组
public static void main(String[] args) {
List<Book> list = new ArrayList<>();
list.add(new Book("1", "sql基础大全", 200));
list.add(new Book("2", "Java基础", 500));
log.info(list);
list.forEach(book -> {
book.toString();
log.info(book.toString());
});
List<String> nameList = list.stream().map(Book -> Book.getName()+"_"+Book.getPrice()).collect(Collectors.toList());
String[] strings = nameList.toArray(new String[nameList.size()]);
for (String string : strings) {
log.info("begin show string msg -------------");
log.info(string);
}
log.info(nameList);
log.info(strings);
}
@Data
public static class Book{
private String id;
private String name;
private int price;
public Book(String s, String s1, int i) {
setId(s);
setName(s1);
setPrice(i);
}
}
3.判断List中是否存在某一个值
List<Book> list = new ArrayList<>();
list.add(new Book("1", "sql基础大全", 200));
list.add(new Book("2", "Java基础", 500));
if (list.stream().filter(w -> String.valueOf(w.getId()).equals("1")).findAny().isPresent()){
System.out.println("存在");
}
4.判断string数组中存在某个值
String[] array = {"1","2","3","4"};
boolean flag = Arrays.asList(array).contains("5");
System.out.println("flag:" + flag);//--- flag:true
评论区