//map集合是一个双列集合
//认识map集合的特点:是由键决定的,值只是一个附属品,值没有要求
// Map<String,Integer> map=new HashMap<>();//按照 无序 不重复 无索引
Map<String,Integer> map=new LinkedHashMap<>();//按照 有序 不重复 无索引
Map<Integer,String> map1=new TreeMap<>();//可排序 不重复 无索引
//map集合的常见方法
//map集合的常见方法
Map<String,Integer>map=new HashMap<>();
map.put("噶胡",12);
map.put("噶胡",11);
map.put("合法iu",123);
map.put("法海u",122);
System.out.println(map);
//1.size():集合的大小
System.out.println(map.size());
System.out.println("----------------------");
//2.clear():清空集合
// map.clear();
// System.out.println(map);
//3.isEmpty():判断集合是否为空
System.out.println(map.isEmpty());
System.out.println("--------------------");
//4.get():根据键获取对应的值
int s=map.get("噶胡");
System.out.println(s);
System.out.println(map.get("合法iu"));
System.out.println(map.get("安徽省分行"));
System.out.println("---------------------------");
//5.remove():根据键删除某个元素(返回某个元素的元素值)
System.out.println(map.remove("噶胡"));
System.out.println(map);
System.out.println("------------------------");
//6.cotainkey():判断是否包含某个键
System.out.println(map.containsKey("噶胡"));
System.out.println(map.containsKey("合法iu"));
System.out.println("-----------------");
//7.containValue():判断是否包含某个值
System.out.println(map.containsValue(123));
System.out.println("--------------");
//8.keyset():拿到map集合全部的键
Set<String> set=map.keySet();
System.out.println(set);
System.out.println("------------------");
//9.values():拿到map集合的全部值
Collection<Integer> values=map.values();
System.out.println(values);
//10.putAll():将其他map集合添加到这个集合
Map<String,Integer> map1=new HashMap<>();
map1.put("安徽",1212);
map1.put("合法ui",1212);
System.out.println(map);
map.putAll(map1);
System.out.println(map);
System.out.println(map1);
//map集合的遍历方法
//map集合的遍历方法
Map<String,Double> map=new HashMap<>();
map.put("规划",12.23);
map.put("啊维护边境",121.12);
map.put("黄瓜",122.23);
System.out.println(map);
//1.通过键找值
Set<String> keys=map.keySet();
for (String key : keys) {
double values=map.get(key);
System.out.println(key+"====>"+values);
}
Map<String,Double> map=new HashMap<>();
map.put("规划",12.23);
map.put("啊维护边境",121.12);
map.put("黄瓜",122.23);
System.out.println(map);
//2.调用map的entryset()方法,把map集合的键值对转化成对应类型的set集合
Set<Map.Entry<String,Double>> entrys=map.entrySet();
for (Map.Entry<String, Double> entry : entrys) {
String key=entry.getKey();
double values=entry.getValue();
System.out.println(key+"----->"+values);
}
//map集合的遍历
//map集合的遍历方法
Map<String,Double> map=new HashMap<>();
map.put("规划",12.23);
map.put("啊维护边境",121.12);
map.put("黄瓜",122.23);
System.out.println(map);
//3.lamda表达式
// map.forEach(new BiConsumer<String, Double>() {
// @Override
// public void accept(String s, Double aDouble) {
// System.out.println(s+"--->"+aDouble);
// }
// });
map.forEach((key,value)->{
System.out.println(key+"-->"+value);
});
//掌握map下的是实现类、】 //Hashmap:无序 无索引 不重复的 //Hashmap集合是一种,增删改查数据性能较好的集合 //hashmap的键是通过hashcode方法和equals方法来保证键的唯一性 //如果键是对象,需要重写hasshcode方法和equals方法
//TreeMap:不重复 无索引 可排序(进行升序排序) //底层原理和treeset集合一样,都是基于红黑树进行排序g //比较规则 //1.用treemap自定义的比较规则 // Map<Student,String> map=new TreeMap<>(new Comparator<Student>() { // @Override // public int compare(Student o1, Student o2) { // return Double.compare(o1.getHeight(),o2.getHeight()); // } // }); Map<Student,String> map=new TreeMap<>(( o1, o2)-> Double.compare(o1.getHeight(),o2.getHeight()));
/集合的嵌套:集合中的一个元素又是一个集合
//集合的嵌套:集合中的一个元素又是一个集合
Map<String, List<String>> map=new HashMap<>();
List<String> cities1=new ArrayList<>();
Collections.addAll(cities1,"南京市","苏州市","无锡市");
map.put("江苏省",cities1);
List<String>cities2=new ArrayList<>();
Collections.addAll(cities2,"武汉市","孝感市","十堰市");
map.put("河北省",cities2);
map.forEach((p,c)->{
System.out.println(p+"----->"+c);
});
Stream流
//认识Stream:用于操作集合或数组中的数据
//优势:Stream流结合了大量的Lamda表达式进行编程,使代码跟简洁,可读性更好
List<String> list=new ArrayList<>();
Collections.addAll(list,"张三","张数数","张神魔","张是数说数");
List<String> list1=new ArrayList<>();
for (String s : list) {
if (s.startsWith("张")&&s.length()==3){
list1.add(s);
}
}
System.out.println(list);
System.out.println(list1);
//使用stream流来解决
List<String> name1=list.stream().filter( s ->s.startsWith("张"))
.filter(s->s.length()==3).collect(Collectors.toList());
System.out.println(name1);
获取Stream流
//1.获取list集合的stream流
List<String> name=new ArrayList<>();
Collections.addAll(name,"向上删除","和经销商","给哈苏");
Stream<String> stream1= name.stream();
stream1.filter(s->s.contains("和")).forEach(s-> System.out.println(s));
//2.获取set的Stream流
Set<String> name1=new HashSet<>();
Collections.addAll(name1,"向上删除","和经销商","给哈苏");
Stream<String> stream2=name1.stream();
stream2.filter(s->s.contains("给")).forEach(s-> System.out.println(s));
//3.map集合获取Stream流
Map<String,Double> map=new HashMap<>();
map.put("哈尽快",12.23);
map.put("环境树洞",23.232);
map.put("忽视了是",23.23);
map.put("哈苏是",12.2323);
//1)分别对键和值或获取他们的Stream流
Set<String> key=map.keySet();
Stream<String> ks=key.stream();
Collection<Double> values=map.values();
Stream<Double> vs=values.stream();
//2)直接对他们获取Stream流
Set<Map.Entry<String,Double>> maps=map.entrySet();
Stream<Map.Entry<String,Double>> kvs=maps.stream();
kvs.filter(s->s.getKey().contains("是")).forEach(s-> System.out.println(s.getKey()+"----->"+s.getValue()));
//4.数组获取stream流
String s[]={"向上删除","和经销商","给哈苏"};
//1)通过Arraylist集合获取
Stream<String> s1= Arrays.stream(s);
//2)通过stream类提供的方法
Stream<String>s2=Stream.of(s);
//Stream流的中间方法
//Stream流的中间方法
//1.找出60分以上的
List<Double> score=new ArrayList<>();
Collections.addAll(score,12.12,1.3,1212.3,60.4,5.67);
score.stream().filter(s->s>60).forEach(s-> System.out.println(s));
List<Student> students=new ArrayList<>();
Student s1=new Student("数是的接口",23,12.2);
Student s2=new Student("数手机",22,12.12);
Student s3=new Student("爱喝酒",12,31.23);
Student s4=new Student("爱喝酒",12,31.23);
Collections.addAll(students,s1,s2,s3,s4);
//2.找出年龄大于20,小于30的学生对象,进行降序排序
students.stream().filter(s->s.getAge()>20&&s.getAge()<30).sorted(((o1, o2) ->o2.getAge()-o1.getAge() )).forEach(s-> System.out.println(s));
System.out.println("-----------------");
//3.取出身高较高的前三名 limit(3)
students.stream().sorted(((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()))).limit(3).forEach(System.out::println);
//简便写法// students.stream().sorted(Comparator.comparingDouble(Student::getHeight)).limit(3).forEach(System.out::println);
System.out.println("-----------------------------------");
//4.取出倒数两名 skip(students.size()-2) 跳过
students.stream().sorted(((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()))).skip(students.size()-2).forEach(System.out::println);
System.out.println("------------------");
//5.取出身高超过12的学生的名字,去除重复的名字 map()映射,将身高映射到map里 distinct()去重复
students.stream().filter(s->s.getHeight()>12).map(s->s.getName())
.distinct().forEach(System.out::println);
//distinct去重复,自定义类型的对象(需要自己去重写hashcode和equals方法)
students.stream().filter(s->s.getHeight()>12).distinct().forEach(System.out::println);
//concat()将两个流合并在一起
Stream<String> str1=Stream.of("张山","历史","哈维");
Stream<String> str2=Stream.of("山","历史","维");
Stream<String> str3=Stream.concat(str1,str2);
str3.forEach(System.out::println);
//Stream常见的终结方法
//Stream常见的终结方法
List<Student> students=new ArrayList<>();
Student s1=new Student("数是的接口",23,12.2);
Student s2=new Student("数手机",22,12.12);
Student s3=new Student("爱喝酒",13,31.23);
Student s4=new Student("爱喝酒",13,31.23);
Collections.addAll(students,s1,s2,s3,s4);
//1.count() 统计数量
//统计一下身高超过30的人数
long count=students.stream().filter(s->s.getHeight()>30).count();
System.out.println(count);
//2.max(): 自定义比较规则,找出最大值
//身高最高的学生
Student s5=students.stream().max(((o1, o2) -> Double.compare(o1.getHeight(),o2.getHeight()))).get();
System.out.println(s5);
//3.min() 最小值
//身高最小值
Student a=students.stream().min(((o1, o2) -> Double.compare(o1.getHeight(),o2.getHeight()))).get();
System.out.println(a);
//4。collect() 收集
//将流收集到集合中去
//流只能收集一次
//收集超过30的学生对象
List<Student> students1=students.stream().filter(s->s.getHeight()>30).collect(Collectors.toList());
System.out.println(students1);
Set<Student> students2= students.stream().filter(s->s.getHeight()>30).collect(Collectors.toSet());
System.out.println(students2);
//收集超过30的学生对象存入map集合
Map<String,Double> map=students.stream().filter(s->s.getHeight()>30).distinct().collect(Collectors.toMap(s->s.getName(), s->s.getHeight()));
System.out.println(map);
//将流收集到数组中去
Object[]arr=students.stream().filter(s->s.getHeight()>30).toArray();
//具体一点,收集学生对象
Student[] arr1=students.stream().filter(s->s.getHeight()>30).toArray(len->new Student[len]);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr1));