Java comparator

Comparators

Arrays class

The main function:

  • Complete all tools and an array of related operations

Binary search:

  • In an ordered binary search sequence of numbers in
public static int binarySearch(数据类型 [] a , 数据类型 key)

Case realization

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)); // 二分查找
        
    }
}

Array Comparisons:

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

And Object.equals () does not have any relationship, this equals comparisons of arrays of arrays is not an object.

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));
    }
}

Comparator: Comparable *

Sorting an array of objects

public static void sort(Object [] a);

Arrays class can directly use sort () method to achieve the sort of object array

  • Test code *
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));
    }
}

To sort the array to an object, the object class where we must implement the Comparable interface, override the compareTo () method.

Binary tree structure: BinaryTree

  • Number, is a more complicated concept than a linked list, also belong to the essence of dynamic array of objects, but compared with the list, the number is more conducive to sort the data.

The operating principle of the number of

  • A data select as the root node, and then data is smaller than the root node on the left node of the root node, smaller than the left node of the root node on the right. Follow the traversal order.
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 interface (at worst)

  • The interface is a function interface: that only inherited methods
@FunctionalInterface
public interface Comparator<T> {
    public int compare(T o1 , T o2);
    public boolean equals(Object obj);
}

We can use this interface, the class will not implement the Comparable interface, change;

Implement the interface, creating a "tools" to achieve Book class object sorting needs

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));
    }
}
  • the difference:

    comparable interface class is implemented in a class definition phase, while the comparator will need to define a special class has been specified.

to sum up

  • Sorting involves an array of objects, use Comparable interface
  • Master binary code based on the actual situation

Guess you like

Origin www.cnblogs.com/wangyuyang1016/p/11109725.html