集合Collection->Set 和异常处理

集合Collection

->Set-> HashSet
-------->treeSet
-> HashMap->Key值不能相同
HashSet

1.集合里面对象不能重复
如果重复 就加不进去
如果要加 内部使用HashMap来实现 键值对 键Key不能重复 值可以相等
HashSetnames=new HashSet<>();
names.add("jack");
names.add("jack");

2.集合里面是无序的 添加的顺序和存值的顺序无关 使用了默认排序
哈希算法
计算这个Key对应的对象的Hash值
整数:在对象的地址的基础上按照一定的算法计算出的一个整数

例子 HashSet name= new HashSet<>();
name.add("jack");
name.add("merry");
name.add("abc");
name.removeIf(ele -> {
return ele.compareTo("c") > 0;
});

TreeSet
  • 集合的排序
第一种排序方式

TreeSetscore=new TreeSet()
{
@Override
pubilc int compara(Person person:person t1)
{
teturn person.comparaTo(t1) ;
}
}

第二种排序方式

TreeSet score = new TreeSet<>((Person p1,Person p2)
->p1.compareTo(p2));
Person p1 = new Person("jack",20);
Person p2 = new Person("jack",30);
Person p3 = new Person("rose",20);
score.add(p1);
score.add(p2);
score.add(p3);
equals 比较的是对象内部的内容
使用的两个对象必须实现comperable接口的compareTo方法
在comparaTo里面实现具体如何去做
System.out.println(score);
if(p1.hashCode()==p1.hashCode())
{
System.out.println("相同");
}else{
System.out.println("不相同");

    }
定义类person
class Person implements Comparable {
    String name;
    int age;
public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        //1. 判断o对象是不是peron的一个对象
        if (o instanceof Person) {
            Person o1 = (Person) o;
            //自己规定比较的策略
            if (this.age != o1.age) {
                return this.age - o1.age;
            } else {
                //年龄相同的情况下 再比姓名的字母
                return this.name.compareTo(o1.name);
            }
        } else {
            return -1;

        }
    }
}
HashMap
  • 集合 存储数据的特点:键Key-值value
  • Key不能重复 可以是任意的对象类型 通常使用字符串String
添加对象:键值对

HashMap score =new HashMap<>();
score.put("chinese",89);
score.put("Math",94);
score.put("English",92);
若已经有了,这是更改
score.put("chinese",91);

获取键值对的个数

score.size();

获得所有的Key

score.keySet();
System.out.println(score.keySet());

获取所有的值value

System.out.println(score.values());

获取Entry:Key-value

System.out.println(score.entrySet());

获取一个键对应的值

System.out.println(score.get("Math"));

键值对的遍历

1.通过遍历Key来得到每一个Key对应的值

for (String Key:score.keySet())
{
//通过key得到value
int a=score.get(Key);
System.out.println(" key "+ Key + " value "+a);
}

2.通过EntrySet 得到Entry对象的集合
  • 一个Entry 管理一个键值对 getKey getValue

Set> entrys=score.entrySet();
for (Map.Entry entry:entrys)
{
String Key=(String)entry.getKey();
Integer value=(Integer)entry.getValue();
System.out.println(" key "+ Key + " value "+value);
}

异常处理

  • 异常处理 处理运行过程中出现的不可控的错误 使程序更健壮:error
  • Exception(异常)
  • 处理
  • 第一种处理异常的方式

try{
执行的代码
可能出现异常
一旦出现异常 系统自动为我们创建一个异常对象 并抛出
}catch( 异常对象NullPointerException){
如果需要自己处理异常 就catch
}catch(IOException){
如果有多个异常 可以使用多个catch来捕获
如果有多个异常 catch的顺序从小到大
}finally{
不管有没有异常finally 都会执行
处理资源回收 网络连接 数据库连接 I/O流
}

  • 如果异常出现 后面的代码将不会执行
  • try 代码块 不是抓太多代码
  • 第二种处理异常的方式
  • 使用throws抛出异常 给外部使用
  • 第三种 当特殊情况出现了 就自己抛出异常
  • throw
  • throw new IllegalAccessException
第一种处理方式

int a=0;
int b=20;
FileReader fr=null;
try{
int c=b/a;
System.out.println("hollow");
fr=new FileReader("");
}catch (ArithmeticException e) {
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try{
fr.close();
}catch (IOException){
}
}

第二种处理方式使用throws 抛出异常给外部使用

public static void test ()throws FileNotFoundException{
FileReader fr=new FileReader("");
}

第三种处理方式使用throws抛出自己创建的异常对象

public static void test2() throws IllegalAccessException
{
if (2>3)
{
throw new IllegalAccessException();
}
}

自己定义异常类

class PXDException extends Exception{
1.提供一个无参构造方法
public PXDException()
{
}
2.提供一个有参构造方法 参数是String字符串
public PXDException(String abc){
super(abc);
}
}

输出自定义的内容

public static void test3()throw PXDException
{
StackTraceElement [] stackTrace=Thread.currentThread().getStackTrace();
StackTraceElement e=stackTrace[2];
String detile =e.getFileName()+"->"+e.getMethodName()+"->"+e.getLineNumber();
new PXDException("自己的异常类"+detile);
}

try{
TEexception.test3();
}catch (PXDException E){
E.printStackTrace();
}

class TEexception {
public static void test3() throws PXDException
{
throw new PXDException("异常");
}
}
感悟:今天我们学习了集合中的Set和异常处理机制。Set不难理解,都是关于它的各种功能应用,难的是异常处理。一开始讲三种处理方式还能理解,到了自己定义异常类就有难度了,自己定义可以,自己输出就不行了,因为用到的系统方法太多,有点看不懂,不知道系统方法如何用,这么用的原理是什么。

你可能感兴趣的:(集合Collection->Set 和异常处理)