list转map & Java Stream的使用

一、list ->Map

1、有如下表对应表结构,实现

1.1、List<RedBagGood>  redBagGoodsList --->  List<Long> suSnapIds

1.2、List<SuSnapInfo>  suSnapInfoList  --->  Map<Long,List<SuSnapInfo>> suSnapInfoList2SuSnapInfoMap

1.3、List<SuSnapInfo>  suSnapInfoList  --->  Map<Long,SuSnapInfo> suSnapInfoList2SuSnapInfo

2、先进行一些简单的操作

定义类

    private Integer rank;  
    private String description;  
      
    public Movie(Integer rank, String description) {  
        super();  
        this.rank = rank;  
        this.description = description;  
    }  
      
    public Integer getRank() {  
        return rank;  
    }  
  
    public String getDescription() {  
        return description;  
    }

	@Override
	public String toString() {
		return "Movie [rank=" + rank + ", description=" + description + "]";
	}

2.1  List<Movie> movies --->  Map<Integer, Movie>
直接遍历将对应值put放入map

		//创建一个 List<Movie> movies
	    List<Movie> movies = new ArrayList<Movie>();  
	    movies.add(new Movie(1, "The Shawshank Redemption"));  
	    movies.add(new Movie(2, "The Godfather"));  
	  	//List<Movie> movies --> Map<Integer, Movie>
	    Map<Integer, Movie> mappedMovies = new HashMap<Integer, Movie>();  
	    for (Movie movie : movies) {  
	        mappedMovies.put(movie.getRank(), movie);  
	    }  
	    //打印map
	 	for(Map.Entry<Integer, Movie> entry:mappedMovies.entrySet()) {
		 	int rank=entry.getKey();
		 	Movie movie=entry.getValue();
		 	System.out.println(rank+":"+movie);
	 	}


2.2使用流

//创建一个List
List<Movie> movies = new ArrayList<Movie>();  
movies.add(new Movie(1, "The Shawshank Redemption"));  
movies.add(new Movie(2, "The Godfather"));  

List<Movie> movies ---> List<Long> rankList

List<Integer> rankList=movies.stream().map(p->p.getRank()).collect(Collectors.toList());

List<Movie> movies---> Map<Integer, Movie> mappedMovies

Map<Integer, Movie> mappedMovies = movies.stream().collect(  Collectors.toMap(Movie::getRank, (p) -> p));

List<Movie>---> Map<Integer, List<Movie>>

Map<Integer, List<Movie>> mappedMoviesMap = movies.stream().collect(  Collectors.groupingBy(p->p.getRank()));

3、回到问题

List<RedBagGood>  redBagGoodsList --->  List<Long> suSnapIds 即:RedBagGoods的ids(redBagGoodsList)获取SuSnapInfo表的ids

List<Long> suSnapIds = redBagGoodsList.stream().map(p->p.getSuSnapId()).collect(Collectors.toList());

去重

List<Byte> giftStatusList = redBagGoodsList.stream().map(p -> p.getGiftStatus()).distinct().collect(Collectors.toList());

List<SuSnapInfo>  suSnapInfoList  --->  Map<Long,List<SuSnapInfo>> suSnapInfoList2SuSnapInfoMap 即 : 将list转为map,键为所有的id,值为对象列表

Map<Long,List<RedBag>> redBagId2RedBagMap = redBagList.stream().collect(Collectors.groupingBy(p->p.getRedBagId()));

统计数量

long suCount = suSnapInfoList.stream().map(p->p.getSuName()).distinct().count();

过滤

List<Long> giftIds = redBagGoodsList.stream().map(p -> p.getGiftBagId()).filter(p -> p.equals(firstGiftIds))
					.collect(Collectors.toList());

二、List去重

比较简单,只给出代码

		// 集合去重
		List<String> ls1 = new ArrayList<>();
		ls1.add("a");
		ls1.add("b");
		ls1.add("c");

		List<String> ls2 = new ArrayList<>();
		ls2.add("a");
		ls2.add("b");
		ls2.add("c");
		ls2.add("d");
		// 是否存在ls1完全包含ls2的情况
		boolean exist = ls1.containsAll(ls2);
		if (!exist) {
			ls2.removeAll(ls1);
		}
		System.out.println(ls2);// d
发布了131 篇原创文章 · 获赞 79 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/qq_31780525/article/details/78780699