java stream, LIST ,ArrayList常用操作

List 删除某个原素

迭代器 Iterator 版

String strArr = "ssss,bbbbb,ssss,,bbbbb";
        // 自带分割
        String[] arr1 = strArr.split(",");
        //转换为 list
        List<String> aooList = Lists.newArrayList(Arrays.asList(arr1));
        Iterator<String> iterator=aooList.iterator();
        while (iterator.hasNext()){
            String s=iterator.next();
            if(s.equals("ssss")){
                iterator.remove();
            }
        }

stream 版

String strArr = "ssss,bbbbb,ssss,,bbbbb";
        // 自带分割
        String[] arr1 = strArr.split(",");
        //转换为 list
        List<String> aooList = Lists.newArrayList(Arrays.asList(arr1));
        aooList.removeIf(s -> s.equals("ssss"));
		或 新建一个新的 List
		List<String> list = aooList.stream().filter(l->!l.equals("ssss")).collect(Collectors.toList());

List 取出id后 并用 , 号拼接

https://www.jianshu.com/p/cd5dcb9506fa

String str = restrictionList.stream().map(User::getId).collect(Collectors.joining(","));

根据name去重

https://www.jianshu.com/p/cd5dcb9506fa

// 根据name去重

List<Person> unique = persons.stream().collect(
            collectingAndThen(
                    toCollection(() -> new TreeSet<>(comparing(Person::getName))), ArrayList::new)

);

根据name,sex去重

// 根据name,sex两个属性去重

List<Person> unique = persons.stream().collect(
            collectingAndThen(
                    toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)
);

根据FullName和CancelTime筛选

List<BlacklistUser> buList = SystemInitVariable.blacklistUserList.stream().filter(bu ->

    bu.getUsername().equals(us.getFullName()) && bu.getCancelTime().after(currentTime)

).collect(Collectors.toList());

将字符串集合中的数据批量下划线分隔

Set strs =new HashSet<>();
strs.add("a_a");
strs.add("b_b");
strs.add("c_c");

Set strsss = strs.stream().map(str -> str.substring(str.indexOf("_")+1)).collect(Collectors.toSet());

list 根据id 转换为map

https://blog.csdn.net/u013805360/article/details/82686009

Map<Integer, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName,(oldValue, newValue) -> newValue));

List 转换 Map

https://www.cnblogs.com/liululee/p/11038072.html

class Book {
    private String name;
    private int releaseYear;
    private String isbn;
    //getters and setters
}

转为Map<String,Integer> 用 releaseYear 做主键 ,releaseYear 做值

Map<String,Integer> books=Maps.newHashMap();
books.stream().collect(Collectors.toMap(Book::getName, Book::getReleaseYear,
      (existing, replacement) -> existing));

转为map 用 releaseYear 做主键 ,Book 做对象

Map<String,Book> books=Maps.newHashMap();
books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(),
      (existing, replacement) -> existing));

List 转换 ConcurrentMap

Map<String,Book> books=Maps.newHashMap();
books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(),
      (existing, replacement) -> existing,ConcurrentHashMap::new));

List 转换 SortedMap

Map<String,Book> books=Maps.newHashMap();
books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(),
      (existing, replacement) -> existing,TreeMap::new));
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页