码迷,mamicode.com
首页 > 编程语言 > 详细

【java】javaSE知识梳理-java常用类

时间:2021-05-24 15:56:15      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:end   mic   直接   定义   else   get   sed   pareto   price   

比较器

技术图片

自定义类比较,直接使用(跟常用类一样)

技术图片
package javaUtil.comparable.method;

import javaLang.comparator.method.Goods;
import org.junit.Test;

import java.util.Arrays;

/**
 * 比较器<pre>
 *     Comparable接口
 *     常用类直接调用现成的compare即可实现
 *     如果不满意现有的比较器,可以自己调用对应接口,重写对应方法
 *     </pre>
 * @author ChenDan
 * @create 2021-05-15
 */
public class Methods {

    /**
     * <p>比较器(内比较器)<pre>
     *      自己定义类,将Comparable重写compare(obj)方法
     *          重写规则:[< == >] [<0 =0 >0]
     *          public static <T> void sort(T[] a, Comparator<? super T> c)</pre>
     */
    @Test
    public void test1() {
        Goods[] goods = new Goods[5];

        for (int i = goods.length - 1; i >= 0; i--) {
            goods[4 - i] = new Goods(Character.toString((char) (‘A‘ + i)), i);
        }

        System.out.println(Arrays.toString(goods));
        Arrays.sort(goods);
        System.out.println(Arrays.toString(goods));

    }

}
View Code

自定义类,调用Comparable接口,重写compare方法

技术图片
package javaLang.comparator.method;

/**
 * @author ChenDan
 * @create 2021-05-16
 * @desc
 */
public class Goods implements Comparable{
    private String name;
    private double price;

    public Goods(){}

    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "name=‘" + name + ‘\‘‘ +
                ", price=" + price +
                ‘}‘;
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof Goods) {
            Goods goods = (Goods) o;
            if(this.price > goods.price) {
                return 1;
            }else if(this.price < goods.price){
                return -1;
            }else{
                return 0;
            }
        }
        throw new RuntimeException("传入数据类型不一致!");
    }
}
View Code

调用类比较

技术图片
package javaLang.comparator.method;

import org.junit.Test;

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

/**
 * 比较器<pre>
 *     Comparator接口
 *     该接口不能直接调用,而是创建Comparator对象,重写compare方法
 *     </pre>
 * @author ChenDan
 * @create 2021-05-15
 */
public class Methods {

    /**
     * <p>实例化1
     */
    @Test
    public void test1(){
        Comparator com = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Goods && o2 instanceof Goods) {
                    Goods goods1 = (Goods) o1;
                    Goods goods2 = (Goods) o2;
                    return Double.compare(goods1.getPrice(),goods2.getPrice());
                }
                throw new RuntimeException("数据类型不一致!");
            }
        };

        TreeSet set = new TreeSet(com);
        set.add(new Goods("apple",12.3));
        set.add(new Goods("pear",32.3));
        set.add(new Goods("peach",2.3));

        System.out.println(set);

    }

    /**
     * <p>实例化2
     */
    @Test
    public void test2(){
        Goods[] goods = new Goods[5];

        for (int i = goods.length - 1; i >= 0; i--) {
            goods[4 - i] = new Goods(Character.toString((char) (‘A‘ + i)), i);
        }

        System.out.println(Arrays.toString(goods));

        Arrays.sort(goods, new Comparator<Goods>() {
            @Override
            public int compare(Goods o1, Goods o2) {
                if(o1 instanceof Goods && o2 instanceof Goods){
                    Goods goods1 = (Goods)o1;
                    Goods goods2 = (Goods)o2;
                    if(goods1.getName().equals(goods2.getName())) {
                        return -Double.compare(goods1.getPrice(),goods2.getPrice());
                    }else{
                        return -goods1.getName().compareTo(goods2.getName());
                    }
                }
                throw new RuntimeException("输入的数据类型不一致!");
            }
        });
        System.out.println(Arrays.toString(goods));
    }
}
View Code

 

【java】javaSE知识梳理-java常用类

标签:end   mic   直接   定义   else   get   sed   pareto   price   

原文地址:https://www.cnblogs.com/cdan134/p/14783969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!