• Java 比较器


    比较器

    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接口
    • 根据实际情况掌握 二叉树代码
  • 相关阅读:
    Leetcode 130. 被围绕的区域(中等)DFS||并查集
    Leetcode 207. 课程表 && 210. 课程表 II
    Leetcode 355. 设计推特(中等) 多路归并&面向对象设计
    Leetcode 277.搜索名人(中等) 图相关名流问题
    Leetcode 785. 判断二分图(中等)& 886. 可能的二分法(中等) 二分图判断
    Leetcode 295. 数据流的中位数(困难)
    Leetcode 990. 等式方程的可满足性(中等) 并查集
    Leetcode 743. 网络延迟时间(中等) 1514. 概率最大的路径(中等) 1631. 最小体力消耗路径(中等)Dijkstra求两点间的最小权重和
    Leetcode 261. 以图判树(中等) 1135. 最低成本联通所有城市(中等) 1584. 连接所有点的最小费用(中等) 并查集&Kruskal最小生成树
    java创建线程池
  • 原文地址:https://www.cnblogs.com/wangyuyang1016/p/11109725.html
Copyright © 2020-2023  润新知