Set-Map以及異常處理

Set-Map

HashSet類(lèi)和LinkedList類(lèi)以及TreeSet類(lèi)都是屬于Set集合里面的
Set集合都是無(wú)序的(存儲(chǔ)數(shù)據(jù)和取出數(shù)據(jù)不同,相同只是偶然),元素是唯一的,不能重復(fù)

實(shí)現(xiàn)類(lèi)

HashSet
HashSet和TreeSet差不多,而且list里面的方法在Set里面都可以使用,可以參考上一篇,這里就不重復(fù)了,例如這里都可add,也可使用lambda表達(dá)式

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

TreeSet
它是可以排序的集合
例如我們要有一個(gè)人的集合,對(duì)年齡排序,就可以用到TreeSet,但是會(huì)用到comepareTo的方法,注意
使用的對(duì)象必須實(shí)現(xiàn)Comparable接口的compareTo方法
在compareTo里面實(shí)現(xiàn)具體如何比較
具體代碼實(shí)現(xiàn)

 class Person implements Comparable {
        String name;
        int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }


        @Override
        public int compareTo(Object o) {
            if (o instanceof Person) {
                Person o1 = (Person) o;
                //自己規(guī)定比較的策略
                if (this.age != o1.age) {
                    return this.age - o1.age;
                } else {
                    //年齡相同的情況下 再比姓名的字母
                    return this.name.compareTo(o1.name);
                }

            } else {
                return -1;


            }

        }
    }

下面的就是在main函數(shù)里面實(shí)現(xiàn)

 TreeSet<Person>score =new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person person, Person t1) {
                return person.compareTo(t1);
            }
        });
         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);
        System.out.println(score);

HashSet里面還有一個(gè)HashMap
HashMap里面就是一個(gè)集合,不過(guò)是通過(guò)鍵key-值value存儲(chǔ)數(shù)據(jù)
下面的代碼中我標(biāo)注了如何使用

HashMap<String, Integer> score = new HashMap<>();
        //添加對(duì)象
        score.put("Chinese", 89);
        score.put("Math", 94);
        score.put("English", 92);
        score.size();
        //或取所有的key
        System.out.println(score.keySet());
        //獲取所有的value
        System.out.println(score.values());
        //獲取Entry:key-value
        System.out.println(score.entrySet());
        //獲取一個(gè)鍵key對(duì)應(yīng)的值
        System.out.println(score.get("English"));

鍵值對(duì)的遍歷
1.通過(guò)遍歷key來(lái)得到每一個(gè)key對(duì)應(yīng)的值

for (String key : score.keySet()) {
            //通過(guò)key得到值
            int s = score.get(key);
            System.out.println("key" + key + "value:" + s);
        }
  1. 通過(guò)EntrySet 得到Entry對(duì)象的集合 一個(gè)Entry管理一個(gè)鍵值對(duì) getkey getvalue
Set<Map.Entry<String, Integer>> entrys = score.entrySet();
        for (Map.Entry entry : entrys) {
            //得到Entry對(duì)應(yīng)的key
            String key = (String) entry.getKey();

            //獲取Entry對(duì)應(yīng)的值
            Integer value = (Integer) entry.getValue();

            System.out.println("key:" + key + " value:" + value);


        }

異常處理

異常處理 處理運(yùn)行過(guò)程中出現(xiàn)的不可控的錯(cuò)誤 使程序更健壯
Exception
try{
執(zhí)行的代碼
可能出現(xiàn)異常

  • 一旦出現(xiàn)異常 系統(tǒng)自動(dòng)為我們創(chuàng)建一個(gè)異常對(duì)象 并拋出
    }catch(NullPointerException e){
    如果需要自己處理異常就catch
    }catch(IOEException e){
    如果有多個(gè)異常 可以使用多個(gè)catch來(lái)捕獲
    如果有多個(gè)異常 catch的順序是從小到大
    }finally{
    不管有沒(méi)有異常Finally都會(huì)被執(zhí)行
    處理資源回收 網(wǎng)絡(luò)連接 數(shù)據(jù)庫(kù)連接 I/O流
    }
public class Exception1 {
    public static void main(String[] args) {
        FileReader fr = null;
        int a = 0;
        int b = 0;
        try {
            int c = b / a;
            fr = new FileReader("");

        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (IOException i) {
            }


        }

如果異常出現(xiàn) 后面的代碼將不會(huì)執(zhí)行

  • try代碼塊 不要抓太多代碼
  • 使用throws拋出異常 經(jīng)外部處理
  • 當(dāng)特殊情況出現(xiàn)了 自己可以選擇拋出異常
  • throw
  • throw new IllegalAccessException()
    *當(dāng)然也可以自定義異常類(lèi)
class TException{
    public static void test() throws FileNotFoundException,NullPointerException{
        FileReader fr=new FileReader("");
    }
    public static void test2() throws IllegalAccessException{
        //......
        if (2 > 1){
            throw new IllegalAccessException();
        }
    }

    public static void test3() throws hmlException{
        throw new hmlException("無(wú)所作為");
    }
}
class hmlException extends Exception{
    //1.提供一個(gè)無(wú)參構(gòu)造方法
    public hmlException(){

    }
    //2.提供一個(gè)有參構(gòu)造方法 參數(shù)是一個(gè)字符串
    public hmlException(String desc){

    }
}

代碼還是要經(jīng)??纯辞懊娴模拍苋跁?huì)貫通。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容