异常
异常的体系
throws
自定义异常
//自定义个类,继承RuntimeException,这个类就是运行时异常
public class AgeaException extends RuntimeException{
public AgeaException() {
}
public AgeaException(String message) {
super(message);
}
}
public class Test {
public static void main(String[] args) {
try {
show(); //new AgeaException("输入年龄有问题");
} catch (Exception e) {
//Exception e =new AgeaException("输入年龄有问题");
String message = e.getMessage();
System.out.println(message);
}
}
//throw 抛出异常
//在方法内部使用,后面跟的是异常对象
public static void show() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入学生的年龄");
int age=sc.nextInt();
if(age<0||age>150){
throw new AgeaException("输入年龄有问题");
}
}
}
注意自定义异常继承编译时异常要求方法必须声明 throws
throws 和 throw 的区别
public class Test2 {
public static void main(String[] args) {
try {
a();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void a() throws NumberFormatException, ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=sdf.parse("2022-4-13");
b();
}
public static void b() throws NumberFormatException{
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个数字");
String s=sc.next();
int i=Integer.parseInt(s);
System.out.println(i);
}
}
集合
Collection集合特点
package com.itcast.exception;
import java.util.ArrayList;
import java.util.HashSet;
public class Test3 {
public static void main(String[] args) {
ArrayList<String> list =new ArrayList<>();
list.add("b");
list.add("a");
list.add("c");
list.add("d");
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
System.out.println(s);
}
System.out.println("--------------------------");
HashSet<String> set =new HashSet<>();
set.add("b");
set.add("a");
set.add("c");
set.add("a");
for (int i = 0; i < set.size(); i++) {
// set.get set系列集合无索引
}
System.out.println(set);//[a, b, c]
}
}
Collection的常见方法如下:
public class Test4 {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
//添加元素
c.add("刘亦菲");
c.add("黎姿");
c.add("朱茵");
System.out.println(c);
//清空操作
System.out.println("清空操作clear");
/* c.clear();
System.out.println(c);*/
boolean b = c.remove("刘亦菲");
System.out.println(b);
System.out.println(c);//[黎姿, 朱茵]
boolean d = c.contains("老王");
System.out.println(d);//false
//判断集合中是否没有元素存在,没有元素返回true
boolean b3 = c.isEmpty();
System.out.println(b3);
//将集合中的元素存储到指定的数组中
String [] arrStr=new String[c.size()];
c.toArray(arrStr);
System.out.println(Arrays.toString(arrStr));
Collection<String> c2 =new ArrayList<>();
c2.add("老田");
c2.add("小李");
//addAll:将指定集合中的元素一次性添加到另一个集合中
c.addAll(c2);//将c2这个元素一次性添加到c集合中去
System.out.println(c);
}
}
collection的遍历方式
public class Test5 {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
//添加元素
c.add("刘亦菲");
c.add("黎姿");
c.add("朱茵");
//通过集合中的iterator()方法,获取一个iterator类的对象
Iterator<String> it = c.iterator();
//判断集合中是否有元素,有返回true
boolean b = it.hasNext();
while (it.hasNext()) {
//获取集合中的元素
String s = it.next();
System.out.println(s);
}
}
}
增强for遍历集合
public class Test6 {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
//添加元素
c.add("刘亦菲");
c.add("黎姿");
c.add("朱茵");
//String 集合中的元素类型是什么,这就些什么
//变量名s: 代表集合中的每一个元素
//想要变量集合的名称
for (String s : c) {
System.out.println(s);
}
}
}
foreach遍历集合
public class Test7 {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
//添加元素
c.add("刘亦菲");
c.add("黎姿");
c.add("朱茵");
c.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
}
}
List集合的特有方法
LinkedList集合
LinkedHashSet的有序是存时候的顺序和取时候的顺序一致
HashSet集合的底层原理(面试重点)
哈希值及特点
HashSet集合去重复机制
LinkedHashSet底层原理(了解)
TreeSet
自定义排序规则
为什么o2-o1是降序,o1-o2是升序
当循环变量过程中删除元素时,只能使用迭代器中的remove()删除
import java.util.ArrayList;
import java.util.Iterator;
public class Test8 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("张柏芝");
list.add("黎姿");
list.add("刘亦菲");
Iterator<String> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
if((it.next()).equals("黎姿"))
{
it.remove();
}
}
System.out.println(list);
}
}
集合的并发异常
可变参数
Collections
public class Test9 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
//Collections工具类中addAll方法;将 一些零散可以方便存储集合中
//参数一:添加到哪个集合中
//参数二:可变参数,添加哪些数据到集合中
Collections.addAll(list,"刘亦菲","黎姿","张柏芝");
System.out.println(list);
System.out.println("_________________________________");
//随机打乱集合中的顺序
Collections.shuffle(list);
//排序 默认是升序排序
Collections.sort(list);
System.out.println(list);
//指定排序规则
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
System.out.println(list);
}
}
案例:斗地主
public class Card {
//点数
private String number;
//花色
private String color;
//序号
private String sort;
public Card() {
}
public Card(String number, String color, String sort) {
this.number = number;
this.color = color;
this.sort = sort;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
@Override
public String toString() {
return number + color + sort;
}
}
Map集合
Map集合遍历方式
方式一(键找值):
public class mapTest1 {
public static void main(String[] args) {
HashMap<String,String> map =new HashMap<>();
map.put("杨过","小龙女");
map.put("郭靖","黄蓉");
map.put("大雄","静香");
//1.通过key对应的value
Set<String> keySet = map.keySet();
//循环遍历所有的key
for (String key : keySet) {
System.out.println(key);
String value=map.get(key);
System.out.println(value);
}
}
}
方式二(键值对):
把“键值对”看成一个整体进行遍历
package com.itcast.maptest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class mapTest1 {
public static void main(String[] args) {
HashMap<String,String> map =new HashMap<>();
map.put("杨过","小龙女");
map.put("郭靖","黄蓉");
map.put("大雄","静香");
//方式二:将map集合中的每对数据封装到一个Entry对象中,set集合中存储了多个
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"----"+value);
}
}
}
Map集合遍历方式三:Lambda
package com.itcast.maptest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
public class mapTest1 {
public static void main(String[] args) {
HashMap<String,String> map =new HashMap<>();
map.put("杨过","小龙女");
map.put("郭靖","黄蓉");
map.put("大雄","静香");
//方式三:foreach
map.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
System.out.println(key+"----"+value);
}
});
//方式三替换成lambda表达式
map.forEach((key, value) -> System.out.println(key+"----"+value));
}
}
TreeMap
public class test1 {
public static void main(String[] args) {
HashMap<String, List<String>> map=new HashMap<>();
ArrayList<String> jxList=new ArrayList<>();
Collections.addAll(jxList,"南京","扬州");
ArrayList<String> hbList=new ArrayList<>();
Collections.addAll(hbList,"武汉","孝感市");
ArrayList<String> hHeiList=new ArrayList<>();
Collections.addAll(hHeiList,"石家庄","唐山市");
map.put("江苏省",jxList);
map.put("湖北省",hbList);
map.put("河北省",hHeiList);
//循环遍历
Set<Map.Entry<String, List<String>>> entries = map.entrySet();
for (Map.Entry<String, List<String>> entry : entries) {
String key = entry.getKey();
List<String> value = entry.getValue();
System.out.println(key+"-----------"+value);
}
}
}
Stream流
Stream流使用步骤
双列集合不能直接获取Stream类的对象
怎么办?先转换成单列集合,在调用stream()方法获取Stream类的对象
HashMap<String,String> map =new HashMap<>();
map.put("郭靖","黄蓉");
Set<Map.Entry<String,String>> entries =map.entrySet();
Stream<Map.Entry<String,String>> stream1=entries.stream();
数组如果获取Stream类的对象
String [] arrStr={"张无忌","周芷若","赵敏"};
Stream<String> stream2=Arrays.stream(arrStr);
Stream<String> stream3=Stream.of("张三丰","张无忌");
Stream流常见的中间方法
public class test {
public static void main(String[] args) {
ArrayList<String> list1=new ArrayList<>();
Collections.addAll(list1,"10","7","9","11");
//排序
list1.stream().map(new Function<String, Object>() {
@Override
public Object apply(String s) {
return Integer.valueOf(s);
}
});
//stream流表达方式
List<Integer> newList=list1.stream().map(s->Integer.valueOf(s)).sorted().collect(Collectors.toList());
System.out.println(newList);
}
}
Stream流常见的终结方法
Stream流的注意的事项
1.Stream流中无法直接修改原集合,原数组中的数据
2.同一个Stream类的对象只能使用一次
方法引用