26.Set

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了26.Set相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%95%b4%e6%95%b0' title='鏁存暟'>鏁存暟   start   ret   pac   get   姝ラ   integer   item   pareto   

1.Set闆嗗悎

1.1Set闆嗗悎姒傝堪鍜岀壒鐐广€愬簲鐢ㄣ€?/span>

  • 鍙互鍘婚櫎閲嶅

  • 瀛樺彇椤哄簭涓嶄竴鑷?nbsp;
  • 娌℃湁甯︾储寮曠殑鏂规硶锛屾墍浠ヤ笉鑳戒娇鐢ㄦ櫘閫歠or寰幆閬嶅巻锛屼篃涓嶈兘閫氳繃绱㈠紩鏉ヨ幏鍙栵紝鍒犻櫎Set闆嗗悎閲岄潰鐨勫厓绱?/span>

1.2Set闆嗗悎鐨勪娇鐢ㄣ€愬簲鐢ㄣ€?/span>

瀛樺偍瀛楃涓插苟閬嶅巻

package com.itheima.myset;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/*
 *Set闆嗗悎鐨勫熀鏈娇鐢? * */
public class MySet1 {
    public static void main(String[] args) {
        Set<String> set=new TreeSet<>();
        set.add("ccc");
        set.add("aaa");
        set.add("aaa");
        set.add("bbb");


        for (int i = 0; i < set.size(); i++) {
            //Set闆嗗悎鏃舵病鏈夌储寮曠殑锛屾墍浠ヤ笉鑳介€氳繃绱㈠紩鑾峰彇鍏冪礌鐨勬柟娉?        }

        Iterator<String> it=set.iterator();
        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);

        }
        System.out.println("=============");
        for (String s : set) {
            System.out.println(s);
        }
    }

}

2.TreeSet闆嗗悎

2.1TreeSet闆嗗悎姒傝堪鍜岀壒鐐广€愬簲鐢ㄣ€?/span>

  • 涓嶅寘鍚噸澶嶅厓绱犵殑闆嗗悎

  • 娌℃湁甯︾储寮曠殑鏂规硶

  • 鍙互灏嗗厓绱犳寜鐓ц鍒欒繘琛屾帓搴?nbsp;               鎯宠浣跨敤TreeSet锛岄渶瑕佸埗瀹氭帓搴忚鍒?/span>

    • TreeSet()锛氭牴鎹叾鍏冪礌鐨勮嚜鐒舵帓搴忚繘琛屾帓搴?/span>

    • TreeSet(Comparator comparator) 锛氭牴鎹寚瀹氱殑姣旇緝鍣ㄨ繘琛屾帓搴?/span>

2.2TreeSet闆嗗悎鍩烘湰浣跨敤銆愬簲鐢ㄣ€?/span>

瀛樺偍Integer绫诲瀷鐨勬暣鏁板苟閬嶅巻

public class TreeSetDemo01 {
    public static void main(String[] args) {
        //鍒涘缓闆嗗悎瀵硅薄
        TreeSet<Integer> ts = new TreeSet<Integer>();

        //娣诲姞鍏冪礌
        ts.add(10);
        ts.add(40);
        ts.add(30);
        ts.add(50);
        ts.add(20);

        ts.add(30);

        //閬嶅巻闆嗗悎
        for(Integer i : ts) {
            System.out.println(i);
        }
    }
}

2.3鑷劧鎺掑簭Comparable鐨勪娇鐢?/span>銆愬簲鐢ㄣ€?/span>

  • 妗堜緥闇€姹?/span>

    • 瀛樺偍瀛︾敓瀵硅薄骞堕亶鍘嗭紝鍒涘缓TreeSet闆嗗悎浣跨敤鏃犲弬鏋勯€犳柟娉?/span>

    • 瑕佹眰锛氭寜鐓у勾榫勪粠灏忓埌澶ф帓搴忥紝骞撮緞鐩稿悓鏃讹紝鎸夌収濮撳悕鐨勫瓧姣嶉『搴忔帓搴?/span>

  • 瀹炵幇姝ラ

    1. 浣跨敤绌哄弬鏋勯€犲垱寤篢reeSet闆嗗悎

      • 鐢═reeSet闆嗗悎瀛樺偍鑷畾涔夊璞★紝鏃犲弬鏋勯€犳柟娉曚娇鐢ㄧ殑鏄嚜鐒舵帓搴忓鍏冪礌杩涜鎺掑簭鐨?/span>

    2. 鑷畾涔夌殑Student绫诲疄鐜癈omparable鎺ュ彛

      • 鑷劧鎺掑簭锛屽氨鏄鍏冪礌鎵€灞炵殑绫诲疄鐜癈omparable鎺ュ彛锛岄噸鍐檆ompareTo(T o)鏂规硶

    3. 閲嶅啓鎺ュ彛涓殑compareTo鏂规硶

      • 閲嶅啓鏂规硶鏃讹紝涓€瀹氳娉ㄦ剰鎺掑簭瑙勫垯蹇呴』鎸夌収瑕佹眰鐨勪富瑕佹潯浠跺拰娆¤鏉′欢鏉ュ啓

  • 浠g爜瀹炵幇

    瀛︾敓绫?/span>

package com.itheima.myset;

public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        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 compareTo(Student o) {
        //鎸夌収瀵硅薄鐨勫勾榫勮繘琛屾帓搴?        int result = this.age - o.age;
        return result;
    }
}

娴嬭瘯绫汇€€銆€

package com.itheima.myset;

import java.util.TreeSet;

/*
* TreeSet闆嗗悎鏉ュ瓨鍌⊿tudent绫诲瀷
*
* */
public class MySet2 {
    public static void main(String[] args) {
        TreeSet<Student> ts=new TreeSet<>();
        Student s1=new Student("灏忚姳",28);
        Student s2=new Student("灏忚姳鑺?,27);
        Student s3=new Student("灏忓皬鑺?,29);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);

        System.out.println(ts);
    }
}

 

鑷劧鎺掑簭绠€鍗曞師鐞嗗浘

濡傛灉杩斿洖鍊间负璐熸暟锛岃〃绀哄綋鍓嶅瓨鍏ョ殑鍏冪礌鏄緝灏忓€硷紝瀛樺乏杈?/span>

濡傛灉杩斿洖鍊间负0锛岃〃绀哄綋鍓嶅瓨鍏ョ殑鍏冪礌璺熼泦鍚堜腑棰濆厓绱犻噸澶嶄簡锛屼笉瀛樸€?/span>

濡傛灉杩斿洖鍊间负姝f暟锛岃〃绀哄綋鍓嶅瓨鍏ョ殑鍏冪礌鏄緝澶у€硷紝瀛樺彸杈?/span>

鎸夌収骞撮緞鎺掑簭

闇€姹傦細鏀瑰啓鍒氬垰鐨勫鐢熸渚嬶細

闇€姹傦細鎸夌収骞撮緞浠庡皬鍒板ぇ鎺掞紝濡傛灉骞撮緞涓€鏍凤紝鍒欐寜鐓у鍚嶉瀛楁瘝鎺掑簭

銆€銆€濡傛灉濮撳悕鍜屽勾榫勪竴鏍凤紝鎵嶈涓烘槸鍚屼竴涓鐢熷璞★紝涓嶅瓨鍏?/span>

Student

package com.itheima.myset;

public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        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 compareTo(Student o) {
        //鎸夌収瀵硅薄鐨勫勾榫勮繘琛屾帓搴?        //涓昏鍒ゆ柇鏉′欢
        int result = this.age - o.age;
        //娆¤鍒ゆ柇鏉′欢
        result = result == 0 ? this.name.compareTo(o.getName()) : result;
        return result;
    }
}

銆€銆€瀹炵幇绫?/p>

package com.itheima.myset;

import java.util.TreeSet;

/*
* TreeSet闆嗗悎鏉ュ瓨鍌⊿tudent绫诲瀷
*
* */
public class MySet2 {
    public static void main(String[] args) {
        TreeSet<Student> ts=new TreeSet<>();
        Student s1=new Student("zhangsan",28);
        Student s2=new Student("lisi",27);
        Student s3=new Student("wangwu",29);
        Student s4=new Student("zhaoliu",28);
        Student s5=new Student("qianqi",30);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);

        System.out.println(ts);
    }
}

銆€銆€comparTo锛堬級

package com.itheima.mytreeset;

public class MyTreeSet3 {
    public static void main(String[] args) {
        String s1="aaa";
        String s2="bcb";
        System.out.println(s1.compareTo(s2));
        //棣栧厛姣旇緝绗竴涓瓧姣嶏紝濡傛灉绗竴涓瓧姣嶆槸涓€鏍风殑锛岄偅涔堟瘮杈冨悗闈㈢殑瀛楁瘝
        //褰撲笉涓€鏍风殑鏃跺€欙紝灏辨嬁鐫€瀵瑰簲鐨勭爜琛ㄥ€?7锛屽噺鍘籦鐨勭爜琛ㄥ€?8
        //璁や负a鏄瘮b灏忕殑
    }
}

2.4姣旇緝鍣ㄦ帓搴廋omparator鐨勪娇鐢ㄣ€愬簲鐢ㄣ€?/span>

  • 妗堜緥闇€姹?/span>

    • 瀛樺偍鑰佸笀瀵硅薄骞堕亶鍘嗭紝鍒涘缓TreeSet闆嗗悎浣跨敤甯﹀弬鏋勯€犳柟娉?/span>

    • 瑕佹眰锛氭寜鐓у勾榫勪粠灏忓埌澶ф帓搴忥紝骞撮緞鐩稿悓鏃讹紝鎸夌収濮撳悕鐨勫瓧姣嶉『搴忔帓搴?/span>

  • 瀹炵幇姝ラ

    • 鐢═reeSet闆嗗悎瀛樺偍鑷畾涔夊璞★紝甯﹀弬鏋勯€犳柟娉曚娇鐢ㄧ殑鏄瘮杈冨櫒鎺掑簭瀵瑰厓绱犺繘琛屾帓搴忕殑

    • 姣旇緝鍣ㄦ帓搴忥紝灏辨槸璁╅泦鍚堟瀯閫犳柟娉曟帴鏀禖omparator鐨勫疄鐜扮被瀵硅薄锛岄噸鍐檆ompare(T o1,T o2)鏂规硶

    • 閲嶅啓鏂规硶鏃讹紝涓€瀹氳娉ㄦ剰鎺掑簭瑙勫垯蹇呴』鎸夌収瑕佹眰鐨勪富瑕佹潯浠跺拰娆¤鏉′欢鏉ュ啓

  • 浠g爜瀹炵幇

    鑰佸笀绫?/span>

package com.itheima.mytreeset;

public class Teacher {
    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        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 "Teacher{" +
                "name=鈥? + name + 鈥榎鈥樷€?+
                ", age=" + age +
                鈥榼鈥?
    }
}

娴嬭瘯绫汇€€銆€

package com.itheima.mytreeset;

import java.util.Comparator;
import java.util.TreeSet;

public class MyTreeSet4 {
    public static void main(String[] args) {
        TreeSet<Teacher> ts=new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                //o1琛ㄧず鐜板湪瑕佸瓨鍏ョ殑閭d釜鍏冪礌
                //o2琛ㄧず宸茬粡瀛樺叆鍒伴泦鍚堜腑鐨勫厓绱?
                //涓昏鏉′欢
                int result = o1.getAge() - o2.getAge();
                //娆¤鏉′欢
                result=result==0?o1.getName().compareTo(o2.getName()):result;
                return result;
            }
        });
        Teacher t1=new Teacher("zhangsan",23);
        Teacher t2=new Teacher("lisi",22);
        Teacher t3=new Teacher("wangwu",24);
        Teacher t4=new Teacher("zhaoliu" ,24);
        ts.add(t1);
        ts.add(t2);
        ts.add(t3);
        ts.add(t4);
        System.out.println(ts);
    }
}

2.4涓ょ姣旇緝鏂瑰紡鎬荤粨銆愮悊瑙c€?/span>

  • 涓ょ姣旇緝鏂瑰紡灏忕粨

    • 鑷劧鎺掑簭: 鑷畾涔夌被瀹炵幇Comparable鎺ュ彛,閲嶅啓compareTo鏂规硶,鏍规嵁杩斿洖鍊艰繘琛屾帓搴?/span>

    • 姣旇緝鍣ㄦ帓搴? 鍒涘缓TreeSet瀵硅薄鐨勬椂鍊欎紶閫?span style="color: rgba(255, 0, 0, 1)">Comparator鐨勫疄鐜扮被瀵硅薄,閲嶅啓compare鏂规硶,鏍规嵁杩斿洖鍊艰繘琛屾帓搴?/span>

    • 鍦ㄤ娇鐢ㄧ殑鏃跺€?榛樿浣跨敤鑷劧鎺掑簭,褰撹嚜鐒舵帓搴忎笉婊¤冻鐜板湪鐨勯渶姹傛椂,蹇呴』浣跨敤姣旇緝鍣ㄦ帓搴?/span>

  • 涓ょ鏂瑰紡涓叧浜庤繑鍥炲€肩殑瑙勫垯

    • 濡傛灉杩斿洖鍊间负璐熸暟锛岃〃绀哄綋鍓嶅瓨鍏ョ殑鍏冪礌鏄緝灏忓€硷紝瀛樺乏杈?/span>

    • 濡傛灉杩斿洖鍊间负0锛岃〃绀哄綋鍓嶅瓨鍏ョ殑鍏冪礌璺熼泦鍚堜腑鍏冪礌閲嶅浜嗭紝涓嶅瓨

    • 濡傛灉杩斿洖鍊间负姝f暟锛岃〃绀哄綋鍓嶅瓨鍏ョ殑鍏冪礌鏄緝澶у€硷紝瀛樺彸杈?/span>

妗堜緥锛氭寜鐓у瓧绗︿覆鐨勯暱鐭帓搴?/p>

闇€姹傦細璇疯嚜琛岄€夋嫨姣旇緝鍣ㄦ帓搴忓拰鑷劧鎺掑簭涓ょ鏂瑰紡

闇€姹傦細瀛樺叆鍥涗釜瀛楃涓诧紝鈥渃鈥?"ab","df","qwer"   鎸夌収闀垮害鎺掑簭锛屽鏋滀竴鏍烽暱鍒欐寜鐓ч瀛楁瘝鎺掑簭

package com.itheima.mytreeset;

import java.util.Comparator;
import java.util.TreeSet;

public class MyTreeSet5 {
    public static void main(String[] args) {
//        TreeSet<String> ts=new TreeSet<>(new Comparator<String>() {
//            @Override
//            public int compare(String o1, String o2) {
//                int result = o1.length() - o2.length();
//                result=result==0?o1.compareTo(o2):result;
//                return result;
//            }
//        });

        TreeSet<String> ts = new TreeSet<>(
                (String o1, String o2) -> {
                    int result = o1.length() - o2.length();
                    result = result == 0 ? o1.compareTo(o2) : result;
                    return result;
                }
        );

        ts.add("c");
        ts.add("ab");
        ts.add("df");
        ts.add("qwer");

        System.out.println(ts);
    }
}

銆€//[c, ab, df, qwer]銆€

銆€銆€

銆€銆€

銆€銆€

 

 

 

 

 

 

 

 

 

以上是关于26.Set的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数