package StreamT;
import java.util.ArrayList;
import java.util.stream.Stream;
public class Test01 {
public static void main(String[] args) {
ArrayList<String> s1 = new ArrayList<>();
s1.add("林青霞");
s1.add("棋棋");
s1.add("风清扬");
s1.add("张敏");
s1.add("张曼玉");
s1.add("张无忌");
s1.add("刘岩");
s1.stream().filter(s -> s.startsWith("张")).filter(s -> s.length()==3).forEach(System.out::println);
}
}
package StreamT;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Stream;
public class Test02 {
public static void main(String[] args) {
Stream<String> stream = new ArrayList<String>().stream();
Stream<String> stream1 = new HashSet<String>().stream();
Stream<Map.Entry<String, String>> stream2 = new HashMap<String, String>().entrySet().stream();
Stream<String> stream3 = new HashMap<String, String>().keySet().stream();
Stream<String> stream4 = new HashMap<String, String>().values().stream();
String[] s1 ={"hello","java","i love you"};
Stream<String> s11 = Stream.of(s1);
Stream<String> hello = Stream.of("hello", "java");
Stream<Integer> integerStream = Stream.of(10, 20, 30);
}
}
package StreamT;
import java.util.ArrayList;
import java.util.stream.Stream;
public class Test03 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("林青霞");
arrayList.add("棋棋");
arrayList.add("王祖贤");
arrayList.add("刘岩");
arrayList.add("张无忌");
arrayList.add("张曼玉");
arrayList.stream().filter(s -> s.startsWith("张")).forEach(System.out::println);
System.out.println("----------------------");
arrayList.stream().filter(s -> s.length()==3).forEach(System.out::println);
System.out.println("----------------------");
arrayList.stream().filter(s -> s.startsWith("张")).filter(s -> s.length()==3).forEach(System.out::println);
System.out.println("----------------------");
arrayList.stream().filter(s -> s.startsWith("张") && s.length()==3).forEach(System.out::println);
}
}
package StreamT;
import java.util.ArrayList;
public class limitskipTest {
public static void main(String[] args) {
ArrayList<Object> a1 = new ArrayList<>();
a1.add("林青霞");
a1.add("林青霞");
a1.add("棋棋");
a1.add("王祖贤");
a1.add("刘岩");
a1.add("张无忌");
a1.add("张曼玉");
a1.stream().distinct().limit(3).forEach(System.out::println);
System.out.println("----------------------------");
a1.stream().distinct().skip(3).forEach(System.out::println);
System.out.println("----------------------------");
a1.stream().distinct().skip(2).limit(2).forEach(System.out::println);
System.out.println("----------------------------");
}
}
package StreamT;
import java.util.ArrayList;
import java.util.Comparator;
public class sortedTest {
public static void main(String[] args) {
ArrayList<String> a1 = new ArrayList<>();
a1.add("林青霞");
a1.add("林青霞");
a1.add("王祖贤");
a1.add("刘岩");
a1.add("棋棋");
a1.add("张无忌");
a1.add("张曼玉");
a1.stream().sorted().forEach(System.out::println);
System.out.println("---------------------");
a1.stream().sorted((s1,s2) -> s1.length()-s2.length()).forEach(System.out::println);
System.out.println("---------------------");
a1.stream().sorted((s1,s2)->{
int num =s1.length()-s2.length();
int num2 =num==0?s1.compareTo(s2):num;
return num2;
}).forEach(System.out::println);
}
}
package StreamT;
import java.util.ArrayList;
import java.util.stream.Stream;
public class concatdistinctTest {
public static void main(String[] args) {
ArrayList<String> a1 = new ArrayList<>();
a1.add("林青霞");
a1.add("林青霞");
a1.add("棋棋");
a1.add("王祖贤");
a1.add("刘岩");
a1.add("张无忌");
a1.add("张曼玉");
Stream<String> limit = a1.stream().limit(4);
Stream<String> skip = a1.stream().skip(2);
Stream.concat(limit,skip).forEach(System.out::println);
System.out.println("------------------------------------------");
Stream.concat(skip,limit).distinct().forEach(System.out::println);
}
}
package StreamT;
import java.util.ArrayList;
public class foreachcountTest {
public static void main(String[] args) {
ArrayList<String> a1 = new ArrayList<>();
a1.add("林青霞");
a1.add("林青霞");
a1.add("王祖贤");
a1.add("刘岩");
a1.add("棋棋");
a1.add("张无忌");
a1.add("张曼玉");
a1.stream().forEach(System.out::println);
System.out.println("---------");
long num = a1.stream().filter(s -> s.startsWith("张")).count();
System.out.println(num);
}
}
package StreamT;
import java.util.ArrayList;
import java.util.Objects;
public class mapmapTointTest {
public static void main(String[] args) {
ArrayList<String> a1 = new ArrayList<>();
a1.add("300");
a1.add("100");
a1.add("20");
a1.add("50");
a1.add("45");
a1.add("457");
a1.stream().map(Integer::parseInt).forEach(System.out::println);
System.out.println("----------------");
int sum = a1.stream().mapToInt(Integer::parseInt).sum();
System.out.println(sum);
}
}
package StreamT;
import java.util.ArrayList;
import java.util.stream.Stream;
public class StreamBigTest {
public static void main(String[] args) {
ArrayList<String> a1 = new ArrayList<>();
a1.add("林青霞");
a1.add("王祖贤");
a1.add("刘岩");
a1.add("棋棋");
a1.add("张无忌");
a1.add("张曼玉");
ArrayList<String> a2= new ArrayList<>();
a2.add("棋1");
a2.add("林青霞");
a2.add("林夕");
a2.add("林小姐");
a2.add("棋4");
a2.add("棋5");
a2.add("棋6");
a2.add("棋7");
Stream<String> maleStream = a1.stream().filter(s -> s.length() == 3);
Stream<String> femaleStream= a2.stream().filter(s -> s.startsWith("林")).skip(1);
Stream<String> concat = Stream.concat(maleStream, femaleStream);
concat.map(actor::new).forEach(s-> System.out.println(s.getName()));
System.out.println("-----------");
Stream.concat(a1.stream().filter(s -> s.length()==3),a2.stream().filter(s -> s.startsWith("林")).skip(1)).map(actor::new).forEach(s-> System.out.println(s.getName()));
}
}
class actor{
private String name;
public actor(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package StreamT;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectDemo {
public static void main(String[] args) {
ArrayList<String> a1 = new ArrayList<>();
a1.add("林青霞");
a1.add("王祖贤");
a1.add("刘岩");
a1.add("棋棋");
a1.add("张无忌");
a1.add("张曼玉");
Stream<String> stringStream = a1.stream().filter(s -> s.length() == 3);
List<String> collect = stringStream.collect(Collectors.toList());
for (String value : collect)
System.out.println(value);
HashSet<Integer> h1 = new HashSet<>();
h1.add(10);
h1.add(20);
h1.add(30);
h1.add(25);
h1.add(22);
h1.add(21);
System.out.println("--------------------");
Set<Integer> collect1 = h1.stream().filter(s -> s >= 25).collect(Collectors.toSet());
for (Integer a : collect1) {
System.out.println(a);
}
System.out.println("--------------------");
String[] arr ={"林青霞,25","王祖贤,30","刘岩,50","棋棋,21","张无忌,85","张曼玉,23"};
Stream<String> stringStream1 = Stream.of(arr).filter(s -> s.split(",")[0].length() == 3).filter(s -> Integer.parseInt(s.split(",")[1]) > 28);
Map<String, Integer> map = stringStream1.collect(Collectors.toMap(s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1])));
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String, Integer> a : entries) {
System.out.println(a.getKey()+","+a.getValue());
}
}
}