package test.demo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.log4j.Log4j2;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
@Log4j2
public class JavaDemoTest extends TestCase {
@Test
public void testListStreamSorted(){
String s = "[{\"rank\":1,\"team\":\"Liverpool\",\"points\":100}," +
"{\"rank\":2,\"team\":\"Arsenal\",\"points\":98},\n" +
"{\"rank\":4,\"team\":\"Chelsea\",\"points\":190},\n" +
"{\"rank\":3,\"team\":\"Manchester Utd\",\"points\":97}]";
// string 转 List对象
List<PremierLeagueTable> premierLeagueTableList = JSON.parseObject(s, new TypeReference<List<PremierLeagueTable>>() {
});
// 按 rank字段顺序排序
System.out.println("sort rank");
premierLeagueTableList = premierLeagueTableList.stream().sorted(Comparator.comparing(PremierLeagueTable::getRank)).collect(Collectors.toList());
for(int i=0;i<premierLeagueTableList.size();i++){
System.out.println("rank:"+premierLeagueTableList.get(i).getRank()+",team:"+premierLeagueTableList.get(i).getTeam()+",points:"+premierLeagueTableList.get(i).getPoints());
}
// 按 rank字段倒序排序
System.out.println("sort reversed rank");
premierLeagueTableList = premierLeagueTableList.stream().sorted(Comparator.comparing(PremierLeagueTable::getRank).reversed()).collect(Collectors.toList());
for(int i=0;i<premierLeagueTableList.size();i++){
System.out.println("rank:"+premierLeagueTableList.get(i).getRank()+",team:"+premierLeagueTableList.get(i).getTeam()+",points:"+premierLeagueTableList.get(i).getPoints());
}
// 按 points字段顺序排序
System.out.println("sort points");
premierLeagueTableList = premierLeagueTableList.stream().sorted(Comparator.comparing(PremierLeagueTable::getPoints)).collect(Collectors.toList());
for(int i=0;i<premierLeagueTableList.size();i++){
System.out.println("rank:"+premierLeagueTableList.get(i).getRank()+",team:"+premierLeagueTableList.get(i).getTeam()+",points:"+premierLeagueTableList.get(i).getPoints());
}
}
@Test
public void testListStreamMap(){
String s = "[{\"rank\":1,\"team\":\"Liverpool\",\"points\":100}," +
"{\"rank\":2,\"team\":\"Arsenal\",\"points\":98},\n" +
"{\"rank\":4,\"team\":\"Chelsea\",\"points\":190},\n" +
"{\"rank\":3,\"team\":\"Manchester Utd\",\"points\":97}]";
// string 转 List对象
List<PremierLeagueTable> premierLeagueTableList = JSON.parseObject(s, new TypeReference<List<PremierLeagueTable>>() {
});
System.out.println("n->n");
premierLeagueTableList.stream().map(n->n).forEach(n-> System.out.println(n));
// 对 list 对象的字段操作
System.out.println("n->n.getRank():");
premierLeagueTableList.stream().map(n->n.getRank()).forEach(n-> System.out.println(n));
System.out.println("n->n.getRank()*10:");
premierLeagueTableList.stream().map(n->n.getRank()*10).forEach(n-> System.out.println(n));
System.out.println("n->n.getTeam():");
premierLeagueTableList.stream().map(n->n.getTeam()).forEach(n-> System.out.println(n));
}
@Test
public void testListStreamFilter(){
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
long count = strings.stream().filter(string -> string.isEmpty()).count();
System.out.println("获取空字符串的数量:"+count);
// 含有bc字符串的数量
count = strings.stream().filter(string -> string.contains("bc")).count();
System.out.println("含有bc字符串的数量:"+count);
}
@Test
public void testListStreamStatistics(){
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("总数 : " + stats.getCount());
System.out.println("平均数 : " + stats.getAverage());
}
}
package test.demo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class PremierLeagueTable {
@ApiModelProperty("rank")
private int rank;
@ApiModelProperty("team")
private String team;
@ApiModelProperty("points")
private int points;
}
streams 特性
https://www.runoob.com/java/java8-streams.html