比较器

Arrays 类

主要功能:

  • 完成所有与数组有关的操作的工具类

二分查找:

  • 在一个有序的数字序列中进行二分查找
public static int binarySearch(数据类型 [] a , 数据类型 key)

案例实现

public class TestDemo {
public static void main(String [] args) throws ParseException {
int date [] = new int [] {1,4,2,5,7,4,3,8} ;
java.util.Arrays.parallelSort(date); // 排序
System.out.println(Arrays.binarySearch(date, 3)); // 二分查找 }
}

数组比较:

public static boolean equals(数据类型 [] a , 数据类型 [] b)

和Object.equals()没有任何关系,本次的arrays中的equals比较的是数组不是对象。

public class TestDemo {
public static void main(String [] args) throws ParseException {
int dateA [] = new int [] {1,4,2,5,7,4,3,8} ;
int dateB [] = new int [] {1,4,2,5,7,4,3,8} ;
System.out.println(Arrays.equals(dateA, dateB));
}
}

比较器:Comparable *

对象数组排序

public static void sort(Object [] a);

Arrays类可以直接利用 sort() 方法实现对象数组的排序

  • 测试代码 *
class Book implements Comparable<Book> { //使用比较器
private String title ;
private double price ;
public Book (String title , double price) {
this.title = title ;
this.price = price ; }
public String toString() {
return this.title + " " + this.price;
}
@Override
public int compareTo(Book o) {
// compareTo 方法由 Arrays.sort()方法自动调用
if (this.price > o.price) {
return 1 ;
} else if (this.price < o.price){
return -1 ;
} else {
return 0 ;
}
}
} public class TestDemo {
public static void main(String [] args) throws ParseException {
Book books [] = new Book [] {
new Book("java",23),
new Book("python",20),
new Book("php",11),
new Book("C/C++",44)
} ;
Arrays.parallelSort(books);// 排序
System.out.println(Arrays.toString(books));
}
}

要对某个对象进行数组排序,对象所在的类一定要实现 Comparable 接口,覆写compareTo()方法。

二叉树结构:BinaryTree

  • 数,是一种比链表更为复杂的概念,本质也属于动态对象数组,但是与链表相比,数更有利于数据进行排序。

数的操作原理

  • 选择一个数据作为根节点,而后比根节点小的数据放在根节点左节点,比左节点小的放在根节点的右节点。按照 中序 进行遍历。
class Book implements Comparable<Book> { //使用比较器
private String title ;
private double price ;
public Book (String title , double price) {
this.title = title ;
this.price = price ; }
public String toString() {
return this.title + " " + this.price;
}
@Override
public int compareTo(Book o) {
// compareTo 方法由 Arrays.sort()方法自动调用
if (this.price > o.price) {
return 1 ;
} else if (this.price < o.price){
return -1 ;
} else {
return 0 ;
}
}
} class BinaryTree {
private class Node{
private Comparable data ;
private Node left ;
private Node right ;
public Node (Comparable data) {
this.data = data ;
}
public void addNode(Node newNode) {
if (this.data.compareTo(newNode.data) < 0 ) {
if (this.left == null) {
this.left = newNode ;
}else {
this.left.addNode(newNode);
}
}else {
if (this.right == null) {
this.right = newNode ;
} else {
this.right.addNode(newNode);
}
}
}
public void toArrayNode () {
if (this.left != null) {
this.left.toArrayNode();
}
BinaryTree.this.retData[BinaryTree.this.foot ++] = this.data;
if (this.right != null) {
this.right.toArrayNode();
}
}
}
private Node root ; // 定义根节点
private int count = 0 ;
private Object [] retData;
private int foot;
public void add(Object obj) {
Comparable com = (Comparable) obj ;// 必须转为 Comparable
Node newNode = new Node(com); //创建新的Node节点
if (this.root == null) {
this.root = newNode ;
} else {
this.root.addNode(newNode);
}
this.count ++ ;
}
public Object [] toArray(){
if (this.root ==null) {
return null;
}
this.foot = 0 ;
this.retData = new Object [this.count] ;
this.root.toArrayNode();
return this.retData;
}
} public class TestDemo {
public static void main(String [] args) {
BinaryTree bt = new BinaryTree();
bt.add(new Book("java",23));
bt.add(new Book("python",20));
bt.add(new Book("php",11));
bt.add(new Book("C/C++",44));
Object obj [] = bt.toArray(); System.out.println(Arrays.toString(obj));
}
}

Comparator接口(下下策)

  • 该接口是一个函数式接口:即只有继承方法
@FunctionalInterface
public interface Comparator<T> {
public int compare(T o1 , T o2);
public boolean equals(Object obj);
}

我们可以借助该接口,将没有实现Comparable接口的类,进行改变;

实现该接口,创建一个“工具类”,实现Book类对象的排序需求

class Book {
private String title ;
private double price ;
public Book (String title , double price) {
this.title = title ;
this.price = price ;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
public void setTitle(String title) {
this.title = title;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return this.title + " " + this.price;
}
} class BookComparator implements Comparator<Book>{ // 比较器工具
@Override
public int compare(Book o1, Book o2) {
if (o1.getPrice() > o2.getPrice()) {
return 1;
} else if (o1.getPrice() > o1.getPrice()){
return -1;
}else {
return 0 ;
}
}
} public class TestDemo {
public static void main(String [] args) {
Book books [] = new Book [] {
new Book("java",23),
new Book("python",20),
new Book("php",11),
new Book("C/C++",44)
} ;
Arrays.parallelSort(books,new BookComparator()); System.out.println(Arrays.toString(books));
}
}
  • 区别:

    comparable是在一个类定义阶段实现的接口类,而comparator则需要专门定义一直指定的类。

总结

  • 涉及到对象数组的排序,就使用Comparable接口
  • 根据实际情况掌握 二叉树代码

Java 比较器的更多相关文章

  1. Java比较器对数组,集合排序一

    数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...

  2. java比较器Comparable接口和Comaprator接口

    Comparable故名思意是比较,意思就是做比较的,然后进行排序. 1.什么是comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compar ...

  3. 黑马----JAVA比较器:Comparable和Comparator

    黑马程序员:Java培训.Android培训.iOS培训..Net培训 一.Comparable接口 1.public interface Comparable{ public int compare ...

  4. java比较器Comparator 使用

    PresonDemo package cn.stat.p5.person.demo; public class PresonDemo implements Comparable { private S ...

  5. java比较器 之compareable 和comparato比较

    compareable 测试类import java.util.Set; import java.util.TreeSet; public class Test { public static voi ...

  6. Java比较器

    导语 本节内容,比较器Comparable是核心内容. 主要内容 重新认识Arrays类 两种比较器的使用 具体内容 Arrays类 在之前一直使用的"java.util.Arrays.so ...

  7. TreeSet的两种实现方法:Comparable和Comparator(Java比较器)

    Comparable与Comparator实际上是TreeSet集合的两种实现方式,用来实现对象的排序.下边介绍一下两种比较器的使用方法和区别. Comparable称为元素的自然顺序,或者叫做默认顺 ...

  8. 小白养成记——Java比较器Comparable和Comparator

    一.使用情景 1.  调用Arrays.sort()方法或Collections.sort()方法对自定义类的对象排序 以Arrays.sort()为例.假定有如下自定义的Person类 1 publ ...

  9. java比较器Comparator

    1. 实现比较类 public class Comparator implements java.util.Comparator<TaskInfo>{ @Override public i ...

随机推荐

  1. ASP.NET Core 3.0 gRPC 配置使用HTTP

    前言 gRPC是基于http/2,是同时支持https和http协议的,我们在gRPC实际使用中,在内网通讯场景下,更多的是走http协议,达到更高的效率,下面介绍如何在 .NET Core 3.0 ...

  2. jvm虚拟机笔记<二> 垃圾回收与内存分配

    确定对象已废弃需要两步: 利用可达性分析算法(与GC roots有关联——虚拟机栈中的对象,方法区静态对象,方法区常量对象,本地方法引用的对象)判断是否需要回收. 是否覆盖过finalize方法并执行 ...

  3. Java生鲜电商平台-电商数据运营统计与分析

    Java生鲜电商平台-电商数据运营统计与分析 今天分享将会分为以下几个方面来阐述: 1. 作为运营我们需要统计与分析的几个核心数据是什么? 2. 核心数据对业务的指导价值在哪里呢? 3. 作为产品PM ...

  4. vue之虚拟DOM、diff算法

    一.真实DOM和其解析流程? 浏览器渲染引擎工作流程都差不多,大致分为5步,创建DOM树——创建StyleRules——创建Render树——布局Layout——绘制Painting 第一步,用HTM ...

  5. vue-cli引用vant使用rem自适应

    摘要 由于需要用到弹出层但是懒得造轮子所以使用vant 介绍 使用的node包管理器为yarn vue-cli版本4 rem计算方式为index.html的js脚本计算 安装vant yarn add ...

  6. 使用docker安装eolinker

    1.下载docker-enlinker git clone https://github.com/psvmc/eolinker-docker.git 2.进入目录 cd eolinker-docker ...

  7. Linux:LNMP环境的搭建

    LNMP环境的搭建 安装DNS服务器 安装DNS服务 yum install bind -y DNS的配置 创建正向解析 以创建一个名为"lsy.com"的正向查找区域为例: 第一 ...

  8. 好用的性能检测工具-性能监控工具- Glances

    平常我经常使用 htop 工具来进行对主机进行性能检测.但是它只能对 进行进行管理.并简要显示 进程和cpu和内存使用信息:性能监控工具: glances 是比较好的性能检测工具.相比较htop还能显 ...

  9. airtest自动化测试工具的环境安装与使用

    AirtestIDE的下载与安装 AirtestIDE已经帮你集成了所有的环境,自带录制脚本栏,自动生成脚本,自带编辑器还自带模拟器. 下载地址是Airtest的官网:http://airtest.n ...

  10. 【poj2661】Factstone Benchmark(斯特林公式)

    传送门 题意: 给出\(x,x\leq 12\),求最大的\(n\),满足\(n!\leq 2^{2^x}\). 思路: 通过斯特林公式: \[ n!\approx \sqrt{2\pi n}\cdo ...