目录
1.可变参数
一种特殊形参,定义在方法,构造器的形参列表里,格式是:数据类型...参数名称
在方法内部就是一个数组(1.一个列表中,只能有一个可变参数。2.可变参数必须放在形参列表的最后面, public static void test(int age,int...nums){}
2.Collections
用来操作集合的工具类
提供的常用静态方法:
public static void main(String[] args) {
List<String> names = new ArrayList<>();
//添加数据
Collections.addAll(names,"张三","王武","李四","李二");
System.out.println(names);
//打乱List集合中的元素顺序
Collections.shuffle(names);
System.out.println(names);
//升序排序
List<Integer> list = new ArrayList<>();
list.add(3);
list.add(5);
list.add(2);
Collections.sort(list);
System.out.println(list);
List<Student> students = new ArrayList<>();
students.add(new Student("乐快乐了",23,189));
students.add(new Student("王乐乐",23,159));
students.add(new Student("乐乐",26,169));
students.add(new Student("刘乐乐",22,179));
Collections.sort(students);
//System.out.println(students);
//按照规则排序
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Double.compare(o1.getHeight(),o2.getHeight());
}
});
System.out.println(students);
}
3.Map集合
双列集合,一次存一对数据,每个元素又称 键值对,由键决定,值不做要求,需要存储一 一对应的数据
HashMap 无序
Map<String,Integer> map = new HashMap<>();//经典代码
map.put("手表",22);
map.put("手机",20);
map.put("电脑",23);
map.put("相机",25);
map.put("相册",26);
System.out.println(map);
package d727_map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapTest1 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();//经典代码
map.put("手表",22);
map.put("手机",20);
map.put("电脑",23);
map.put("相机",25);
map.put("相册",26);
System.out.println(map);
//获取大小
System.out.println(map.size());
//清空集合
map.clear();
System.out.println(map);
//判断集合是否为空,为空返回true,反之
System.out.println(map.isEmpty());
//根据键获取对应值
int v1 =map.get("手表");
System.out.println(v1);
System.out.println(map.get("手机"));
//根据键删除整个元素(删除键返回键的值)
System.out.println(map.remove("手表"));
System.out.println(map);
//判断是否包含某个键,包含返回true
System.out.println(map.containsKey("手表"));
//获取Map集合的全部键
Set<String> keys = map.keySet();
System.out.println(keys);
//获取Map集合的全部值
Collection<Integer> values = map.values();
System.out.println(values);
//把其他Map集合的数据倒入到自己集合中来.
Map<String,Integer> mp1 = new HashMap<>();
map1.put("java1",10);
map1.put("java2",20);
Map<String,Integer> map2 = new HashMap<>();
map2.put("java3",10);
map2.put("java2",222);
map1.putAll(map2);//putAll:把map2集合中元素全部倒入一份到map1集合中去
map1.sout;
map2.sout;
}
}
LinkedHashMap 有序
TreeMap 按照大小默认升序排序
Map集合的遍历方式
1.键找值2.键值对3Lambda
public static void main(String[] args) {
//准备一个Map集合
Map<String,Double> map = new HashMap<>();
map.put("蜘蛛精",162.5);
map.put("蜘蛛精",169.8);
map.put("紫霞",165.8);
map.put("至尊宝",196.8);
map.put("牛魔王",183.6);
System.out.println(map);
//获取全部键
Set<String> keys = map.keySet();
//System.out.println(keys);
//2.遍历全部键,根据键获取其对应的值
for (String key : keys) {
//根据键获取对应值
double value = map.get(key);
System. out.println(key +"====>"+value);
}
//1.调用Map集合提供entrySet方法,把Map集合转换成键值对类型的Set集合
Set<Map.Entry<String, Double>> entries = map.entrySet(); //ctrl+alt+v
for (Map.Entry<String, Double> entry : entries) { //.for
String key = entry.getKey();
double value = entry.getValue();
System.out.println(key+"====>"+value);
}
//第三种遍历
map.forEach((k,y) ->{
System.out.println(k+"=====>"+v);
});
}
4.集合的嵌套
目标:理解集合的嵌套。
江苏省 ="南京市""扬州市""苏州市”“无锡市”"常州市"
湖北省=“武汉市","孝感市","十堰市"“宜昌市”,“鄂州市"
//1.定义一个Map集合存储全部的省份信息,和其对应的城市信息
Map<String, List<String>> map = new HashMap<>();
List<String> cities1 = new ArrayList<>();
Collections.addAll(cities1,"南京市","扬州市","苏州市","无锡市","常州");
map.put("江苏省",cities1);
System.out.println(map);
List<String> cities2 = new ArrayList<>();
Collections.addAll(cities2,"南京市","扬州市","苏州市","无锡市","常州");
map.put("江苏省",cities2);
System.out.println(map);
//根据键,输出值
List<String> cities = map.get("江苏省");
for (String city : cities) {
System.out.println(city);
}
//遍历
map.forEach((p ,c) ->{
System.out.println(p+"---->"+c);
});