Java学习笔记之----TreeSet练习

练习一

将Person对象存储到TreeSet集合中,同姓名同年龄视为一个人,不存,姓名升序排序

package com.rqy.exercise;

import com.rqy.day18.Students;
import java.util.Comparator;
//实现Comparator
public class CompByName implements Comparator {
    //覆盖compare方法
    @Override
    public int compare(Object o1, Object o2) {
        //强制转换,如果使用了泛型该步骤可以省略
        Students s1 = (Students)o1;
        Students s2 = (Students)o2;
        //先比较姓名,如果姓名相同,则比较年龄
        int temp = s1.getName().compareTo(s2.getName());
        return temp == 0?s1.getAge()-s2.getAge():temp;
    }
}
package com.rqy.exercise;

import com.rqy.day18.CompaByName;
import com.rqy.day18.Students;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author rqyboss
 * 练习一:将Person对象存储到TreeSet集合中,同姓名同年龄视为一个人,不存,姓名升序排序
 * 思路:
 * 使用姓名排序,那么只能使用比较器
 * 需要实现comparator方法
 */

public class Exercise_1 {
    public static void main(String[] args) {
        //初始化TreeSet集合明确一个比较器
        Set set = new TreeSet(new CompaByName());
        //增加元素
        set.add(new Students("vincy",20));
        set.add(new Students("susan",25));
        set.add(new Students("angle",28));
        set.add(new Students("zero",18));
        set.add(new Students("susan",28));
        set.add(new Students("mary",27));
        //增强for循环,也可以使用迭代器
        for(Object obj : set){
            System.out.println(obj);
        }
    }
}

在这里插入图片描述

练习二

基于练习一,实现对Person对象按照年龄升序排序

package com.rqy.day18;

import java.util.Objects;

public class Students implements Comparable{
    private String name;
    private int age;
    //构造函数
    public Students() {
        super();
    }
    public Students(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 "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
        /*实现comparable接口,实现compareTo方法
        //学生就具备了比较功能,该功能是自然排序使用的方法
        自然排序就年龄的升序排序为主*/
        @Override
        public int compareTo(Object o) {
            Students stu = (Students)o;
            int temp = this.age - stu.age;
            //如果temp等于0就使用字符串的compareTo方法比较姓名,若不等于0,返回temp
            return  temp == 0?this.name.compareTo(stu.name):temp;
        }
}
package com.rqy.exercise;

import com.rqy.day18.Students;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author rqyboss
 * 练习二:基于练习一,实现对Person对象按照年龄升序排序
 * 思路:
 * 需要排序,那么容器选择Set
 * 可以直接使用TreeSet默认排序,即自然排序
 * 实现comparable,覆盖compareTo方法
 */

public class Exercise_2 {
    public static void main(String[] args) {
        //初始化TreeSet集合明确一个比较器
        Set set = new TreeSet();
        //增加元素
        set.add(new Students("vincy",20));
        set.add(new Students("susan",25));
        set.add(new Students("angle",28));
        set.add(new Students("zero",18));
        set.add(new Students("susan",28));
        set.add(new Students("mary",27));
        //迭代器
        for(Iterator it = set.iterator();it.hasNext();){
            System.out.println(it.next());
        }
    }
}

在这里插入图片描述

练习三

对多个字符串(不重复)按照长度排序,由短到长

package com.rqy.exercise;

import java.util.Comparator;

public class CompByLength implements Comparator {

    @Override
    public int compare(Object o1, Object o2) {
        String s1 = (String)o1;
        String s2 = (String)o2;
        //比较长度
        int temp = s1.length() - s2.length();
        return temp == 0?s1.compareTo(s2):temp;
    }
}
package com.rqy.exercise;

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

/**
 * @author rqyboss
 * 练习三:对多个字符串(不重复)按照长度排序,由短到长
 * 思路:
 * 不允许重复,需要排序,容器选择TreeSet
 * 要按照指定的比较方式,那么需要实现comparator,覆盖compare方法
 */

public class Exercise_3 {
    public static void main(String[] args) {

        Set set = new TreeSet(new CompByLength());
        set.add("abc");
        set.add("rqyboss");
        set.add("dna");
        set.add("cctv");
        set.add("woaijava");
        for(Iterator it = set.iterator();it.hasNext();){
            System.out.println(it.next());
        }
    }
}

在这里插入图片描述

练习四

对多个字符串(重复)按照长度排序,由短到长

package com.rqy.exercise;

import java.util.Comparator;
/**
 * @author rqyboss
 * 练习四:对多个字符串(重复)按照长度排序,由短到长
 * 思想:
 * 重复的字符串可以使用List或者数组
 * 使用数组
 */
public class Exercise_4 {
    public static void main(String[] args) {
        sortStringByLength();
    }
    public static void sortStringByLength(){
        //定义一个按照长度排序的比较器对象
        Comparator com = new CompByLength();
        //定义字符串数组
        String[] str = {"abc","rqyboss","cctv","nba","rqyboss","dna","https"};
        //排序需要嵌套,位置置换
        for(int x = 0 ; x < str.length-1;x++){
            for(int y = x+1 ; y < str.length ; y++){
                //按照长度排序,所以无法使用自然排序中的compareTo方法
//                if(str[x].compareTo(str[y]) > 0){
//                    swap(str, x, y);
//                }
                if(com.compare(str[x],str[y]) > 0)
                    swap(str, x, y);
            }
        }
        for(String s : str){
            System.out.println(s);
        }
    }
    private static void swap(String[] str, int x, int y) {
        String temp = str[x];
        str[x] = str[y];
        str[y] = temp;
    }
}

在这里插入图片描述

练习五

通过LinkedList,定义一个堆栈数据结构

// 下回写,哈哈
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值