JAVA基础: HashSet\TreeSet

HashSet

HashSet


public static void fun1() {
        //创建一个HashSet保存两个a\b\c\d
        HashSet<String> set = new HashSet<>();
        set.add("a");
        set.add("a");
        set.add("b");
        set.add("b");
        set.add("c");
        set.add("c");
        set.add("d");
        set.add("d");
        //使用增强for循环遍历
        for (String string : set) {
            System.out.println(string);

        }       
        System.out.println(set);
    }

        public static void fun2() {
            //创建一个HashSet,保存5个人并遍历.认为姓名年龄一样就是同一个  人并去重;
            //注意:如果你要使用hashCode去除重复对象
            //直接重写HashCode()和equals()方法
            HashSet<Person> hashSet = new HashSet<>();
            hashSet.add(new Person("张三",25));
            hashSet.add(new Person("张三",25));
            hashSet.add(new Person("张小三",25));
            hashSet.add(new Person("王二",26));
            hashSet.add(new Person("王二",26));
            hashSet.add(new Person("六大",22));
            hashSet.add(new Person("六大",22));
            for (Person person : hashSet) {
                System.out.println(person);
            }
        }

        public static void fun3() {
        //保存使用LinkedHashSet 保存两个abcd打印一下
        //linkedHashSet特点 :有序的(怎么存的怎么取出来),
        //是HashSet的子类
        //ArrayHashSet(从后向前分析)
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("a");
        linkedHashSet.add("a");
        linkedHashSet.add("b");
        linkedHashSet.add("b");
        linkedHashSet.add("c");
        linkedHashSet.add("c");
        linkedHashSet.add("d");
        linkedHashSet.add("d");
        System.out.println(linkedHashSet);
    }

    public static void fun4() {
            //编写一个程序,获取10个1至20的随机数,要求随机数不能重复
            //去重-->往Set集合上琢磨
            //1.创建一个 Set集合
            //2.随机10个数
            //3.把10个数添加到集合中 int(系统的类都实现了equals\hashCode的类)
            //4.当集合长度<10就添加>=10 就不添加

            HashSet<Integer> set = new HashSet<>();
                while (set.size() < 10 ) {
                    if (set.size() == 10) {
                        break;
                    }
                    //随机数 
                    int num = (int)(Math.random()*(20-1+1)+1);
                    //添加到Set中
                    set.add(num);//自动装箱
                }
                System.out.println(set);
        }

public static void fun5() {
        //输入一个字符串,去掉其中重复字符, 打印出不同的那些字符
        //打印出那些不同的字符:
        //输入一个字符串
        //1.创建set集合
        //2.把接收过来的字符串,转成字符串数组
        //3.遍历这个字符数组,把每一个字符存入set中
        //4.打印set集合
        System.out.println("输入一个字符串");
        Scanner scanner = new  Scanner(System.in);
        String string = scanner.nextLine();
        //转字符数组
        char[] charArray = string.toCharArray();
            HashSet<Character> set = new HashSet<>();
            //遍历存储
            for (int i = 0; i < charArray.length; i++) {
                set.add(charArray[i]);//自动装箱

            }
        System.out.println(set);
    }

public static void fun6() {
        //利用set集合,去除ArrayList集合中的重复元素(操作原ArrayList)
        ArrayList<String> list = new ArrayList<>();
        list.add("c");
        list.add("c");
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("b");

        //为了保持存进去的顺序 选用linkedHashSet
        LinkedHashSet<String> set = new LinkedHashSet<>();
        //把list中的所有元素,加到set中去重
        set.addAll(list);
        //清空list中原有的元素
        list.clear();
        //再把set中的元素,全不添加到list集合中
        list.addAll(set);
        System.out.println(list);
    }

**## TreeSet ##
public static void fun1() {
TreeSet set = new TreeSet<>();
set.add(2);
set.add(1);
set.add(2);
set.add(4);
set.add(3);
set.add(3);
set.add(5);
System.out.println(set);
}


public static void fun2() {
        TreeSet<Person> set = new TreeSet<>();
        set.add(new Person("奥巴马",11));
        set.add(new Person("克林顿",21));
        set.add(new Person("杜鲁门",12));
        set.add(new Person("华盛顿",12));
        set.add(new Person("罗斯福",31));

        System.out.println(set);

    }

    public static void fun3() {
            //比较器
            //集合中保存字符串,按字符串长度排序         初始化TreeSet的同时把比较器有参构造方法传入TreeSet中
            TreeSet<String> set = new TreeSet<>(new Comparatorimpl() {
            });
            set.add("aaa");
            set.add("asd");
            set.add("ww");
            set.add("zz");
            set.add("cc");
            set.add("a");
            set.add("ww");
            //遍历
            for (String string : set) {
                System.out.println(string);
            }
        }

        public static void fun4() {
                //在一个集合ArrayList中存储了无序并且重复的字符串,排序,而且还不能去除重复
                //要求 排序,而且还不能去除重复(用比较器)
                ArrayList<String> list = new ArrayList<>();
                list.add("aaa");
                list.add("cbb");
                list.add("cdc");
                list.add("af");
                list.add("af");
                list.add("baa");
                //存储规则
                TreeSet<String> set = new TreeSet<>(new Comparatorimpl() {
                });
                //把list塞进set里
                set.addAll(list);
                //清空list
                list.clear();
                //再把set 整个放入list里
                list.addAll(set);
                System.out.println(list);
            }

            public static void fun5() {
                    //键盘接收一个字符串,程序对其中所有字符进行排序,要求保留重复的.
                    System.out.println("请输入一个字符串");
                    Scanner scanner = new Scanner(System.in);
                    String string = scanner.nextLine();
                    //接收字符串
                    //转字符数组
                    char[] charArray = string.toCharArray();
                    //创建TreeSet                 //写一个比较器比较规则
                    TreeSet<Character> set = new TreeSet<>(new CharImpl());
                    //把字符数组中的每一个元素 都添加到set中
                    for (int i = 0; i < charArray.length; i++) {
                        set.add(charArray[i]);

                    }
                    System.out.println(set);
                }

public static void fun6() {
        /*程序启动后, 可以从键盘输入接收多个整数, 
         * 直到输入quit时结束输入. 把所有输入的整数倒序排列打印.
         * 
         */
        //创建TreeSet
        //接收键盘输入的数
        TreeSet<Integer> set = new TreeSet<>(new IntegerImpl());


        System.out.println("请输入一个字符串");
        Scanner scanner = new Scanner(System.in);

        //循环输入
        while (true) {
            String string = scanner.nextLine();
            //写一个出口 输入quit停止循环
            if (string.equals("quit")) {
                break;

            }
            //字符串转成int(这个字符串必须是个int值才能转)
            int num = Integer.parseInt(string);
            //把数字添加到TreeSet 中 进行排序
            set.add(num);//自动装箱
        }
        System.out.println(set);

//键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),
        //录入的学生信息格式(姓名,语文成绩,数学成绩,英语成绩)
        //按照总分从高到低输出到控制台,输出学生的所有信息;
        TreeSet<Student> set = new TreeSet<>(new StudentSumComp());
        System.out.println("录入5个学生信息"+"(姓名,语文成绩,数学成绩,英语成绩)");
        Scanner scanner = new Scanner(System.in);

        //集合中的保存的学生小于5个就继续输入
        while (set.size()<5) {
            //接收输入
            String string = scanner.nextLine();
            //string = 王龙,100,10,15
            //按逗号切割
            String[] strings = string.split(",");
            //字符串转数字
            int chinese = Integer.parseInt(strings[1]);
            int math = Integer.parseInt(strings[2]);
            int english = Integer.parseInt(strings[3]);


            //创建学生

            Student student = new Student(strings[0],chinese,math,english);
            set.add(student);

        }
        System.out.println(set);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值