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

2021-05-28 11:06

阅读:408

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

比较器

技术图片

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

技术图片技术图片
package javaUtil.comparable.method;

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

import java.util.Arrays;

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

比较器(内比较器)

     *      自己定义类,将Comparable重写compare(obj)方法
     *          重写规则:[] [0]
     *          public static  void sort(T[] a, Comparator super T> c)
*/ @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;

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

实例化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); } /** *

实例化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() { @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


评论


亲,登录后才可以留言!