也叫Stream流,是Jdk8开始新增的一套API (java.util.stream.*),可以用于操作集合或者数组的数据。
Stream流大量的结合了Lambda的语法风格来编程,提供了一种更加强大,更加简单的方式操作
public class Demo1 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("张无忌");
list.add("周芷若");
list.add("赵敏");
list.add("张强");
list.add("张三丰");
//需求:把集合中所有以"张"开头,且是3个字的元素存储到一个新的集合。
//实现2: 使用Stream流方式实现
List list1 = list.stream().filter(name -> name.startsWith("张") ).
filter(name -> name.length() == 3).collect(Collectors.toList());
System.out.println(list1);
}
}
1. 2. 3.
如何获取Stream流: Collection集合: 单列集合都支持一个stream()方法,它可以直接获取集合的Stream流 数组: Arrays.stream(数组) 零散数据: Stream.of(T... values) Map 双列集合并没有提供直接获取Stream流的方法,他需要间接获取
public class Demo2 {
public static void main(String[] args) {
//"玄奘", "悟空", "悟能", "悟净"
List list = List.of("玄奘", "悟空", "悟能", "悟净");
//Collection集合: 单列集合都支持一个stream()方法,它可以直接获取集合的Stream流
list.stream().forEach(System.out::println);
//数组:Arrays.stream(数组)
System.out.println("-------------------------------------");
String[] arr = {"玄奘", "悟空", "悟能", "悟净"};
Arrays.stream(arr).forEach(System.out::println);
//零散数据:Stream.of(T... values)
System.out.println("-------------------------------------");
Stream.of("玄奘", "悟空", "悟能", "悟净").forEach(System.out::println);
//Map:双列集合并没有提供直接获取Stream流的方法,他需要间接获取
System.out.println("-------------------------------------");
Map map = new HashMap<>();
map.put("001","玄奘");
map.put("002","悟空");
map.put("003","悟能");
map.put("004","悟净");
//返回一个Set集合
map.keySet().stream().forEach(System.out::println);
map.values().stream().forEach(System.out::println);
System.out.println("-------------------------------------");
//这是一个整体EntrySetkeyvalue -> e
map.entrySet().stream().forEach(e -> System.out.println(e.getValue() + e.getKey()));
}
}
public class Demo3 {
public static void main(String[] args) {
List list = List.of(61, 57, 66, 77, 88, 44, 100, 89, 97, 47, 70);
//需求1: 找出所有及格的分数,并打印
System.out.println("=================");
list.stream()
.filter(e -> e > 60)
.forEach(System.out::println);
//需求2: 找出所有及格的分数, 正序排列, 打印输出
System.out.println("=================");
list.stream()
.filter(e -> e > 60)
.sorted((o1, o2) -> o1 - o2)
.forEach(System.out::println);
//需求3: 找出所有及格的分数, 倒序排列, 打印输出
System.out.println("=================");
list.stream()
.filter(e -> e > 60)
.sorted((o1, o2) -> o2 - o1)
.forEach(System.out::println);
//需求4: 找出所有及格的分数, 倒序排列, 取前3名, 打印输出
System.out.println("=================");
list.stream().filter(e -> e > 60)
.sorted((o1, o2) -> o2 - o1)
.limit(3)
.forEach(System.out::println);
//需求5: 找出所有及格的分数, 倒序排列, 取前4-6名, 打印输出
System.out.println("=================");
list.stream().filter(e -> e > 60)
.sorted((o1, o2) -> o2 - o1)
.skip(3)
.limit(3)
.forEach(System.out::println);
//需求6: 找出所有及格的分数, 倒序排列, 取前4-6名, 将每个人的分数加10分, 打印输出
System.out.println("=================");
list.stream().filter(e -> e > 60)
.sorted((o1, o2) -> o2 - o1)
.skip(3)
.limit(3)
.map(e -> e + 10)
.forEach(System.out::println);
//需求7: 将下面两个集合中的元素进行合并去重
System.out.println("=================");
List list1 = List.of("1","2","3","4");
List list2 = List.of("1","5","7","4");
Stream.concat(list1.stream(),list2.stream())
.distinct()
.forEach(System.out::println);
}
}
调用完成后,不会返回新Stream了,没法继续使用流了。
public class Demo4 {
public static void main(String[] args) {
List list = List.of(
new Student("玄奘", 60, 165.5),
new Student("悟空", 50, 175.5),
new Student("悟能", 55, 145.5),
new Student("悟净", 40, 185.5)
);
//1. 打印出集合中所有元素
list.stream().forEach(System.out::println);
//2. 统计出身高不足170的人数
Stream stream = list.stream().filter(e -> e.getHeight() < 170);
long count = stream.count();
System.out.println(count);
//3. 请找出年龄最大的对象, 并输出(了解)
Student student = list.stream()
.max((o1, o2) -> o1.getAge() - o2.getAge())
.get();
System.out.println(student);
//4. 请找出身高最低的对象, 并输出(了解)
Student student2 = list.stream()
.min((o1, o2) -> Double.compare(o1.getHeight(),o2.getHeight()))
.get();
System.out.println(student2);
}
}
class Student {
private String name;
private int age;
private double height;
public Student() {
}
public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
public class Demo5 {
public static void main(String[] args) {
List list = List.of(
new Teacher("玄奘", 60, 165.5),
new Teacher("悟空", 50, 175.5),
new Teacher("悟空", 50, 175.5),
new Teacher("悟能", 55, 145.5),
new Teacher("悟净", 40, 185.5));
//1. 请找出身高超过170的教师, 并放到一个新数组中
Object[] objects = list.stream()
.filter(e -> e.getHeight() > 170)
.toArray();
System.out.println(Arrays.toString(objects));
Teacher[] teacher = list.stream()
.filter(e -> e.getHeight() > 170)
//len代表元素中的个数
.toArray(len -> new Teacher[len]);
System.out.println(Arrays.toString(teacher));
//2. 请找出身高超过170的教师, 并放到一个新List集合中
List list1 = list.stream()
.filter(e -> e.getHeight() > 170)
.collect(Collectors.toList());
System.out.println(list1);
//3. 请找出身高超过170的教师, 并放到一个新Set集合中
Set list2 = list.stream()
.filter(e -> e.getHeight() > 170)
.collect(Collectors.toSet());
System.out.println(list2);
//4. 请找出所有的教师的姓名和身高, 放到一个新Map集合中
Map map = list.stream()
.distinct()
.collect(Collectors.toMap(e -> e.getName(), e-> e.getHeight()));
System.out.println(map);
}
}
class Teacher {
private String name;
private int age;
private double height;
public Teacher() {
}
public Teacher(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Teacher teacher = (Teacher) o;
return age == teacher.age && Double.compare(teacher.height, height) == 0 && Objects.equals(name, teacher.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, height);
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
1.Map集合称为双列集合,一次需要存一对数据做为一个元素, 格式:{key1=value1 , key2=value2 , key3=value3 , ...}
2.Map集合的所有键是不允许重复的,但值可以重复,键和值是一一对应的,每一个键只能找到自己对应的值
1.HashMap: 无序,不重复
2.LinkedHashMap: 有序,不重复
3.TreeMap:排序,不重复
public class Demo2 {
public static void main(String[] args) {
//1. 创建map
HashMap map = new HashMap<>();
map.put("001", "玄奘");
map.put("002", "悟空");
map.put("003", "悟能");
map.put("004", "悟净");
//2. 各种方式进行遍历
test1(map);
System.out.println("=================");
test2(map);
System.out.println("=================");
test3(map);
}
//遍历方式1: 先获取Map集合全部的键,再通过遍历键来找值
private static void test1(HashMap map) {
Set set = map.keySet();
for (String s : set) {
System.out.println(s + map.get(s));
}
}
//遍历方式2: 将map中的所有键值对放入一个set集合中, 然后遍历set集合拿到每个键值对, 再取里面的键值
private static void test2(HashMap map) {
Set> entries = map.entrySet();
for (Map.Entry entry : entries) {
System.out.println(entry.getKey() + entry.getValue());
}
}
//遍历方式3: Lambda, 使用foreach(BiConsumer bc)
private static void test3(HashMap map) {
map.forEach((k,v) -> System.out.println(k+v));
}
}
现有字符串数组如下: String[] bookArr = {"《红楼梦》-曹雪芹","《西游记》-吴承恩","《三国演义》-罗贯中","《水浒传》-施耐庵"}; 需求: 请将字符串中的书名提取为Map集合的键,将作者提取为Map集合的值 并使用三种不同方式,遍历Map集合打印键值对元素内容
public class Demo3 {
public static void main(String[] args) {
Map map = new HashMap<>();
String[] bookArr = {"《红楼梦》-曹雪芹","《西游记》-吴承恩","《三国演义》-罗贯中","《水浒传》-施耐庵"};
for (String book : bookArr) {
String[] split = book.split("-");
map.put(split[0],split[1] );
}
System.out.println(map);
Set set = map.keySet();
for (String key : set) {
System.out.println(key + map.get(key));
}
System.out.println("----------------------------");
Set> entries = map.entrySet();
for (Map.Entry entry : entries) {
System.out.println(entry.getKey()+entry.getValue());
}
System.out.println("----------------------------");
map.forEach((key , value) -> System.out.println(key+value));
}
}
和HashSet一样 :Day06List接口&Set接口&树-CSDN博客https://blog.csdn.net/m0_60388241/article/details/133930070?spm=1001.2014.3001.5501
底层数据结构依然是基于哈希表实现的,只是每个键值对元素又额外的多了一个双链表的机制记录元素顺序(保证有序)。
public class Demo6 {
private String put;
public static void main(String[] args) {
//创建集合
Map map = new TreeMap<>((o1, o2) -> o1.getAge() - o2.getAge());
map.put(new Teacher("张三", 21), "河北");
map.put(new Teacher("李四", 20), "山东");
map.put(new Teacher("王五", 19), "山西");
map.put(new Teacher("赵六", 21), "河南");
map.forEach((k, v) -> {
System.out.println(k + "-----" + v);
});
}
}
class Teacher {
private String name;
private int age;
public Teacher() {
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
集合嵌套 要求在程序中记住如下省份和其对应的城市信息,记录成功后,要求可以查询出湖北省的城市信息。 数据 江苏省 = "南京市","扬州市","苏州市","无锡市","常州市" 湖北省 = "武汉市","孝感市","十堰市","宜昌市","鄂州市" 河北省 = "石家庄市","唐山市","邢台市","保定市","张家口市" 分析: 定义一个Map集合,键用表示省份名称,值表示城市名称,注意:城市会有多个。 Map> 根据“湖北省”这个键获取对应的值展示即可。
public class Demo7 {
public static void main(String[] args) {
Map> map = new HashMap<>();
List list = List.of("南京市","扬州市","苏州市","无锡市","常州市");
List list2 = List.of("石家庄市","唐山市","邢台市","保定市","张家口市");
map.put("江苏省",list);
map.put("河北省",list2);
System.out.println(map);
}
}
可变参数 就是一种特殊形参,定义在方法、构造器的形参列表里,格式是:数据类型... 参数名称 优点 特点:可以不传数据给它;可以传一个或者同时传多个数据给它;也可以传一个数组给它。 好处:常常用来灵活的接收数据。 注意事项 1. 可变参数在方法内部就是一个数组 2. 一个形参列表中可变参数只能有一个 3. 可变参数必须放在形参列表的最后面
public class Demo {
public static void main(String[] args) {
sum(1546);
}
//计算2个整数的和
//计算3个整数的和
//计算4个整数的和
//计算n个整数的和
public static void sum(int... a){
for (int i : a) {
System.out.println(i);
}
}
}
public class Demo {
public static void main(String[] args) {
List list = new ArrayList<>();
//static boolean addAll(单列集合,可变参数) 批量添加元素
Collections.addAll(list,1,2,100,3,45);
System.out.println(list);
//static void shuffle(List集合) 打乱List集合元素顺序,每次调用都会打乱
Collections.shuffle(list);
System.out.println(list);
//static void sort(List集合) List集合进行自然排序
Collections.sort(list);
System.out.println(list);
//排自定义类对象,需要指定排序规则
List stuList = new ArrayList<>();
stuList.add(new Student("zhangsan", 18));
stuList.add(new Student("wangwu", 22));
stuList.add(new Student("zhaoliu", 21));
stuList.add(new Student("lisi", 19));
stuList.add(new Student("qianqi", 20));
//static void sort(List集合,比较器);List集合进行比较器排序
Collections.sort(stuList,(o1, o2) -> o1.getAge() - o2.getAge());
System.out.println(stuList);
}
}
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}