Lambda expressions filter songs with a length of less than 1 minute
•
Java
Filter to find song names with a time length > 60.
import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;/**
- @Author: cxh
- @CreateTime: 17/12/14 20:47
- @ProjectName: JavaBaseTest
- <测试类></>
*/public class Test {
public static void main(String[] args) {ArrayList<Album> albums=new ArrayList<>(); int len=30; for(int i=0;i<5;i++){ //name String name="LiLy"+i; //tracks ArrayList<Track> tracks=new ArrayList<>(); tracks.add(new Track("John"+i,len+=10)); //artists ArrayList<Artist> artists=new ArrayList<>(); ArrayList<String> mems=new ArrayList<>(); mems.add("lili"+i);mems.add("zhouxin"+i);mems.add("jimi"+i); Artist artist=new Artist("qiduobaihe"+i,mems,"china"+i); artists.add(artist); albums.add(new Album(name,tracks,artists)); } //找出长度大于1的曲目 Set<String> res=albums.stream().flatMap(album -> album.getTracks().stream()) .filter(track -> track.getLength()>60) .map(track -> track.getName()) .collect(Collectors.toSet()); for(String str:res) Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(str); }
}
John3 John4 Process finished with exit code 0
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码