Map集合
键值对应关系
实现类
1)HashMap集合
底层哈希表,元素唯一,无序。(允许插入null值null键)
举例说明
package practice;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class Prac04 {
public static void main(String[] args){
HashMap<Integer,String> hm=new HashMap<Integer,String>();
String s1=hm.put(1, "张三");//存储数据用put()方法
String s2=hm.put(1, "张一");
hm.put(2, "李四");
hm.put(3, "王五");
System.out.println(s1+" "+s2);//第一次采用put放置数据返回null。第二次放置键相同的数据,返回上一次的值。键相同,值覆盖。
//遍历
//方式一
Set<Integer> keySet = hm.keySet();
for(Integer key:keySet){
System.out.println(key+"----------"+hm.get(key));
}
//方式二
Set<Entry<Integer, String>> entrySet = hm.entrySet();
for(Entry<Integer, String> en:entrySet){
System.out.println(en.getKey()+"-----------"+en.getValue());
}
}
}
map集合数据结构只跟腱有关,当键是自定义对象时:
重写 equals方法和HashCode 方法的,如果不重写就无法保证元素的唯一性
举例:
package practice;
import java.io.Serializable;
import java.util.HashMap;
public class Prac06 {
public static void main(String[] args) {
HashMap<Student1, Integer> hm = new HashMap<Student1, Integer>();
hm.put( new Student1("s",21),1);
hm.put( new Student1("a",22),2);
hm.put( new Student1("d",23),3);
hm.put( new Student1("g",24),4);
System.out.print(hm);
}
}
//必须重写hashCode()和equals()方法
class Student1 implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Student1() {
super();
}
public Student1(String name, int age) {
super();
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;
}
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student1 other = (Student1) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
LinkedHashMap
底层的数据结构是链表和哈希表 元素有序 并且唯一
元素的有序(有序:指的是存的顺序跟取的顺序一致)性由链表数据结构保证 唯一性由 哈希表数据结构保证
TtreeMap
数据结构是红黑树 特点:元素唯一,而且还能对元素进行排序 :自然排序和比较器排序(不允许插入null键)
用TreeMap 来存 键是 Student 类型的 值是String 类型的 数据
TreeMap 底层数据结构是红黑树 它能够保证元素唯一,能够对元素进行排序,排序要有条件 排序的元素必须实现Comparable接口 重写 CmcompareTo(T)
举例:
package practice;
import java.io.Serializable;
import java.util.Set;
import java.util.TreeMap;
public class Prac07 {
public static void main(String[] args){
TreeMap< Student2,Integer> map = new TreeMap<Student2,Integer>();
//按照年龄从小到大排序
map.put( new Student2("a",21),2);
map.put( new Student2("b",22),3);
map.put( new Student2("c",20),4);
map.put( new Student2("d",11),5);
map.put( new Student2("a",21),5);//键相同。值覆盖
map.put( new Student2("b",21),5);
// 遍历
Set<Student2> keySet = map.keySet();
for (Student2 key : keySet) {
System.out.println(key.getName()+key.getAge() + "---" + map.get(key));
}
}
}
//实现 Comparable接口,重写compareTo()方法
class Student2 implements Serializable, Comparable<Student2> {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Student2() {
super();
}
public Student2(String name, int age) {
super();
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 + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student2 other = (Student2) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Student2 stu) {
// 年龄相同不能说名是同一个人
int num = this.age - stu.age;
// 如果年龄相同,我们还得比较一下姓名
int result = num == 0 ? this.name.compareTo(stu.name) : num;
return result;
}
}
异常类
大的方向:Throwable
两个具体的子类:
Error Exception
Exception的分类:异常类
子类:
1)编译时期异常:只要不是RuntimeException中的异常都属于编译时期异常:比如:IOException(IO流中的),ParseException(解析异常)
出现的原因:要么语法有问题,要么使用的是Sun公司提供的一些类的中的方法,(这个方法本身调用的时候就会异常存在),调用者必须处理,不处理不行,需要编译通过
2)运行时期异常:RuntimeException
可能由于我们代码的逻辑不够严谨导致的问题举例,NullPointerException:空指针异常!
需要个对象进行非空判断,来防止该问题的出现!
处理异常方式:
1)try..catch
package practice;
public class Prac08 {
public static void main(String[] args){
int a=2;
int b=0;
int[] arr={0,1,2};
//这种方式只能处理第一次出现的异常,随后结束
try {
System.out.println(a/b);
System.out.println(arr[3]);
} catch (ArithmeticException e ) {
System.out.println("除数不能为0");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("角标不存在");
}catch(Exception e){
System.out.println("代码可能出问题了");
}
System.out.println("over");//除数不能为0 over
}
}
常用的方法:
public String getMessage()
消息字符串
public String toString()
描述字符串:
1)当前类的全路径名称(指的是当前异常类名所在的包的全路径名称)
2)”: “(冒号和一个空格)
public void printStackTrace():
该方法是里面包含了消息字符串以及当前出现错误的异常的时候所描述哪个包下以及代码中具体的错误出现第几行
返回值是void,也就直接在控制台输出
2)throws抛出异常
package practice;
public class Prac08 {
public static void main(String[] args) {
//要调用此方法,必须对异常进行处理
try {
method();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("over");
}
//在方法中抛出异常
private static void method() throws Exception{
//定义变量及数组
int a = 10 ;
int b = 0 ;
System.out.println(a/b);
}
}
throws和throw的区别?
throws:抛出
后面跟的异常类名,可以跟多个异常类名,中间用逗号隔开
throws在方法声明上抛出,表示异常的一种可能性
由调用者去处理
throws表示抛出异常的一宗可能性
throw:抛出
后面跟的异常对象(匿名对象),只能跟具体的一个异常对象
throw在方法中的语句中抛出,表示异常的绝对性
有方法中某些语句处理
final,finalize,和finally的区别?
final:表示最终的,终态的意思
可以修饰类:类不能继承
可以修饰成员方法:成员方法被重写
可以修饰成员变量:此变量是一个常量 :自定义常量: public static final int ORANGLE = 100 ;
finalize:它表示通过gc垃圾回收器回收不用的对象或者是变量,System.gc():实质,调用的是重写了Object类中的
finalize()方法,表示回收没有跟多引用对象或者变量等等…
finally:不能单独使用,和try…catch…finally中一块使用,(异常,IO,数据库中中使用的),是用来释放资源
finally中的代码一定会执行,并且除非Java虚拟机Jvm退出了,才不会执行
package practice;
public class Prac08 {
public static void main(String[] args) {
System.out.println(getInt());
}
//方法
private static int getInt() {
int a = 10 ;
try{
System.out.println(a/0);
a = 20 ;
}catch(ArithmeticException e){
a = 30 ;
return a ;
/**
* 代码走到这里,return a,return 30 ,在这里形成了一个方法返回路径
* 但是,finally中代码只有在Jvm退出了,才不会执行,所以a = 40 ;
* 但是,由于之前已经返回路径,代码就要执行到return 前;
*/
}finally{
a = 40 ;
}
return a;
}
}
File类
public class FileDemo {
public static void main(String[] args) {
//创建一个File实例
//方式1
File file = new File("E:\\demo\\a.txt") ;
System.out.println(file);
//方式2
File file2 = new File("E:\\demo", "a.txt") ;
System.out.println(file2);
//方式3
File file3 = new File("E:\\demo") ;
File file4 = new File(file3, "a.txt") ;
System.out.println(file4);
}
}
删除
举例:
public class FileDemo {
public static void main(String[] args) {
//在当前项目下创建aaa\\bbb\\ccc
File file = new File("aaa\\bbb\\ccc") ;
System.out.println("mkdirs:"+file.mkdirs());
//需求:要删除当前项目下的a.txt文件
File file2 = new File("a.txt") ;
System.out.println("delete:"+file2.delete());
//需求:删除aaa\\bbb\\ccc
File file3 = new File("aaa\\bbb\\ccc") ;
System.out.println("delte:"+file3.delete());
File file4 = new File("aaa\\bbb") ;
System.out.println(file4.delete());
File file5 = new File("aaa") ;
System.out.println(file5.delete());
}
}