java学习日记 比较器
一、回顾Arrays类(了解)
import java.util.Arrays; public class ArraysDemo1 { public static void main(String[] args) { int [] arr = new int[]{5,3,2,4,3,6}; Arrays.sort(arr); System.out.println(Arrays.binarySearch(arr,3)); int [] arr2 = new int[]{3,2,4,3,6,5}; int [] arr3 = new int[]{2,3,3,4,5,6}; System.out.println(Arrays.equals(arr,arr2)); System.out.println(Arrays.equals(arr,arr3)); } }
运行结果:
2 false true
2、Comparable接口
import java.util.Arrays; class ArrayCom implements Comparable<ArrayCom>{ private String title; private double price; public ArrayCom(String title,double price){ this.title = title; this.price = price; } @Override public String toString() { return "title="+this.title+",price="+this.price+"\n"; } @Override public int compareTo(ArrayCom o) { //Arrays.sort会自动调用 if (this.price>o.price){ return 1; }else if (this.price<o.price){ return -1; }else { return 0; } } } public class ComparableDemo1 { public static void main(String[] args) { ArrayCom arr1 = new ArrayCom("java",78.3); ArrayCom arr2 = new ArrayCom("oracle",52.2); ArrayCom arr3 = new ArrayCom("c",99.99); ArrayCom [] arr =new ArrayCom[]{arr1,arr2,arr3}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); } }
运行结果:
[title=oracle,price=52.2 , title=java,price=78.3 , title=c,price=99.99 ]
3、数据结构——二叉树(BinaryTree)
class BinaryTree{ class Node{ private Node left; private Node right; private Comparable data; public void addNode(Node node){ //确定是放左子树还是右子树 if (node.data.compareTo(this.data)<0){ if (this.left == null){ this.left = node; }else { this.left.addNode(node); } } if (node.data.compareTo(this.data)>=0){ if (this.right == null){ this.right = node; }else { this.right.addNode(node); } } if (node == null){ return; } } public void printNode(){ if (this.left!=null){ this.left.printNode(); } System.out.println(this.data+"\t"); if (this.right!=null){ this.right.printNode(); } } } private Node root; public void add(Comparable data){ Node newNode = new Node(); newNode.data = data; if (root == null){ root = newNode; }else { root.addNode(newNode); } } public void print(){ this.root.printNode(); } } class ArrayCom implements Comparable<ArrayCom>{ private String title; private double price; public ArrayCom(String title,double price){ this.title = title; this.price = price; } @Override public String toString() { return "title="+this.title+",price="+this.price+"\n"; } @Override public int compareTo(ArrayCom o) { //Arrays.sort会自动调用 if (this.price>o.price){ return 1; }else if (this.price<o.price){ return -1; }else { return 0; } } } public class ComparableDemo1 { public static void main(String[] args) { ArrayCom arr1 = new ArrayCom("java",78.3); ArrayCom arr2 = new ArrayCom("oracle",52.2); ArrayCom arr3 = new ArrayCom("c",99.99); ArrayCom arr4 = new ArrayCom("jsp",66.6); BinaryTree binaryTree = new BinaryTree(); binaryTree.add(arr1); binaryTree.add(arr2); binaryTree.add(arr3); binaryTree.add(arr4); binaryTree.print(); } }
运行结果:
title=oracle,price=52.2 title=jsp,price=66.6 title=java,price=78.3 title=c,price=99.99
4、挽救的比较器Comparator
class ArrayCom { private String title; private double price; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public ArrayCom(String title, double price){ this.title = title; this.price = price; } @Override public String toString() { return "title="+this.title+",price="+this.price+"\n"; } } class ArrayComparator implements Comparator<ArrayCom>{ @Override public int compare(ArrayCom o1, ArrayCom o2) { if (o1.getPrice()>o2.getPrice()){ return 1; }else if (o1.getPrice()<o2.getPrice()){ return -1; }else { return 0; } } } public class ComparableDemo1 { public static void main(String[] args) { ArrayCom arr1 = new ArrayCom("java",78.3); ArrayCom arr2 = new ArrayCom("oracle",52.2); ArrayCom arr3 = new ArrayCom("c",99.99); ArrayCom arr4 = new ArrayCom("jsp",66.6); ArrayCom [] arr = new ArrayCom[]{arr1,arr2,arr3,arr4}; Arrays.sort(arr,new ArrayComparator()); System.out.println(Arrays.toString(arr)); } }
Comparable和Comparator的区别?
Comparable是一个类定义的时候实现好的接口,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,依赖compareTo方法的实现。
Comparator是专门定义一个指定类的比较规则,有两个方法一个是compare(),一个是equals()。