Stream流
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
@Test
public void test1() {
String[] names = {"aa", "bb", "cc", "dd"};
Stream<String> stream = Stream.of(names);
stream.forEach(c -> System.out.println(c));
}
@Test
public void test2(){
long count = Stream.of("aa", "bb", "cc", "dd").count();
System.out.println("arrays->"+count);
ArrayList<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
long listCount = list.stream().count();
System.out.println("list->"+listCount);
}
@Test
public void test3(){
ArrayList<String> list = new ArrayList<>();
list.add("sui aa");
list.add("sui bb");
list.add("cc");
list.add("dd");
list.add("sui ee");
Stream<String> s = list.stream().filter((String c) -> c.startsWith("sui"));
s.forEach(c-> System.out.println(c));
}
@Test
public void test4() {
ArrayList<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
list.stream().limit(3).forEach(c-> System.out.println("获得前3个元素数据"+c));
}
@Test
public void test5() {
ArrayList<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("ee");
Stream<String> stream = list.stream();
stream.limit(3).forEach(c-> System.out.println("获得前3个元素数据"+c));
list.stream().skip(2).forEach(c-> System.out.println("跳过前2个元素"+c));
list.stream().skip(list.size()-2).forEach(c-> System.out.println("获得最后2个元素数据"+c));
}
@Test
public void test6(){
ArrayList<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
Stream<String> stream = list.stream();
Stream<Integer> stream1 = stream.map((String s) ->{return Integer.parseInt(s);});
stream1.forEach(s-> System.out.println(s+5));
}
@Test
public void test7() {
String[] s1 = {"a","b","c","d"};
String[] s2 = {"A","B","C","D"};
Stream<String> stream1 = Stream.of(s1);
Stream<String> stream2 = Stream.of(s2);
Stream<String> stream = Stream.concat(stream1, stream2);
stream.forEach(s-> System.out.println(s));
}
@Test
public void test8() {
Stream<String> stream = Stream.of("10", "20", "30", "40", "50");
List<String> list = stream.collect(Collectors.toList());
System.out.println(list);
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("aa",18));
students.add(new Student("bb",18));
students.add(new Student("cc",15));
students.add(new Student("dd",20));
students.add(new Student("ee",22));
Map<Integer, List<Student>> listMap = students.stream().collect(Collectors.groupingBy(Student::getAge));
for (Integer key : listMap.keySet()) {
System.out.println(key);
List<Student> students1 = listMap.get(key);
students1.stream().forEach((Student a)-> System.out.println(a));
}
}
@Test
public void test9() {
Stream<String> stream = Stream.of("10", "20", "30", "40", "50");
Object[] objArray = stream.toArray();
Stream<Object> stream1 = Stream.of(objArray);
stream1.forEach((Object a)->{
System.out.println(a);
});
}
}
|