胶南做网站,如何用php做电商网站,网页构建器,电子商务网站网络安全设计方案目录 1. PriorityQueue中插入对象2. 元素的比较2.1 基本类型的比较2.2 对象比较的问题 3. 对象的比较3.1 基于Comparable接口类的比较3.2 基于比较器比较3.3 三种方式对比 4. 集合框架中PriorityQueue的比较方式5. 使用PriorityQueue创建大小堆#xff0c;解决TOPK问题 【本节… 目录 1. PriorityQueue中插入对象2. 元素的比较2.1 基本类型的比较2.2 对象比较的问题 3. 对象的比较3.1 基于Comparable接口类的比较3.2 基于比较器比较3.3 三种方式对比 4. 集合框架中PriorityQueue的比较方式5. 使用PriorityQueue创建大小堆解决TOPK问题 【本节目标】
Java中对象的比较集合框架中PriorityQueue的比较方式模拟实现PriorityQueue
1. PriorityQueue中插入对象
优先级队列在插入元素时有个要求插入的元素不能是null或者元素之间必须要能够进行比较为了简单起见我们只是插入了Integer类型那优先级队列中能否插入自定义类型对象呢
class Card {public int rank; // 数值public String suit; // 花色public Card(int rank, String suit) {this.rank rank;this.suit suit;}
}
public class TestPriorityQueue {public static void TestPriorityQueue(){PriorityQueueCard p new PriorityQueue();p.offer(new Card(1, ♠));p.offer(new Card(2, ♠));}public static void main(String[] args) {TestPriorityQueue();}
}优先级队列底层使用堆而向堆中插入元素时为了满足堆的性质必须要进行元素的比较而此时Card是没有办法直接进行比较的因此抛出异常
2. 元素的比较
2.1 基本类型的比较
在Java中基本类型的对象可以直接比较大小
public class TestCompare {public static void main(String[] args) {int a 10;int b 20;System.out.println(a b);System.out.println(a b);System.out.println(a b);char c1 A;char c2 B;System.out.println(c1 c2);System.out.println(c1 c2);System.out.println(c1 c2);boolean b1 true;boolean b2 false;System.out.println(b1 b2);System.out.println(b1 ! b2);}
}2.2 对象比较的问题
class Card {public int rank; // 数值public String suit; // 花色public Card(int rank, String suit) {this.rank rank;this.suit suit;}
}
public class TestPriorityQueue {public static void main(String[] args) {Card c1 new Card(1, ♠);Card c2 new Card(2, ♠);Card c3 c1;//System.out.println(c1 c2); // 编译报错System.out.println(c1 c2); // 编译成功 ---- 打印false因为c1和c2指向的是不同对象//System.out.println(c1 c2); // 编译报错System.out.println(c1 c3); // 编译成功 ---- 打印true因为c1和c3指向的是同一个对象}
}从编译结果可以看出Java中引用类型的变量不能直接按照 或者 方式进行比较。 那为什么可以比较
因为对于用户实现自定义类型都默认继承自Object类而Object类中提供了equal方法而默认情况下调用的就是equal方法但是该方法的比较规则是没有比较引用变量引用对象的内容而是直接比较引用变量的地址但有些情况下该种比较就不符合题意。
// Object中equal的实现可以看到直接比较的是两个引用变量的地址
public boolean equals(Object obj) {return (this obj);
}3. 对象的比较
有些情况下需要比较的是对象中的内容比如向优先级队列中插入某个对象时需要对按照对象中内容来调整堆那该如何处理呢
public class Card {public int rank; // 数值public String suit; // 花色public Card(int rank, String suit) {this.rank rank;this.suit suit;} Overridepublic boolean equals(Object o) {// 自己和自己比较if (this o) {return true;} // o如果是null对象或者o不是Card的子类if (o null || !(o instanceof Card)) {return false;} // 注意基本类型可以直接比较但引用类型最好调用其equal方法Card c (Card)o;return this.rank c.rank suit.equals(c.suit);}
}注意 一般覆写 equals 的套路就是上面演示的
如果指向同一个对象返回 true如果传入的为 null返回 false如果传入的对象类型不是 Card返回 false按照类的实现目标完成比较例如这里只要花色和数值一样就认为是相同的牌注意调用其他引用类型的比较也需要 equals例如这里的 suit 的比较
覆写基类equal的方式虽然可以比较但缺陷是equal只能按照相等进行比较不能按照大于、小于的方式进行比较。
3.1 基于Comparable接口类的比较
Comparable是JDK提供的泛型的比较接口类源码实现具体如下
public interface ComparableE {// 返回值:// 0: 表示 this 指向的对象小于 o 指向的对象// 0: 表示 this 指向的对象等于 o 指向的对象// 0: 表示 this 指向的对象大于 o 指向的对象int compareTo(E o);
}对用用户自定义类型如果要想按照大小与方式进行比较时在定义类时实现Comparable接口即可然后在类中重写compareTo方法
public class Card implements ComparableCard {public int rank; // 数值public String suit; // 花色public Card(int rank, String suit) {this.rank rank;this.suit suit;} // 根据数值比较不管花色// 这里我们认为 null 是最小的Overridepublic int compareTo(Card o) {if (o null) {return 1;} return rank - o.rank;}public static void main(String[] args){Card p new Card(1, ♠);Card q new Card(2, ♠);Card o new Card(1, ♠);System.out.println(p.compareTo(o)); // 0表示牌相等System.out.println(p.compareTo(q)); // 0表示 p 比较小System.out.println(q.compareTo(p)); // 0表示 q 比较大}
}Comparable是java.lang中的接口类可以直接使用
3.2 基于比较器比较
按照比较器方式进行比较具体步骤如下 用户自定义比较器类实现Comparator接口 public interface ComparatorT {// 返回值:// 0: 表示 o1 指向的对象小于 o2 指向的对象// 0: 表示 o1 指向的对象等于 o2 指向的对象// 0: 表示 o1 指向的对象等于 o2 指向的对象int compare(T o1, T o2);
}注意区分Comparable和Comparator 覆写Comparator中的compare方法 import java.util.Comparator;
class Card {public int rank; // 数值public String suit; // 花色public Card(int rank, String suit) {this.rank rank;this.suit suit;}
}class CardComparator implements ComparatorCard {// 根据数值比较不管花色// 这里我们认为 null 是最小的Overridepublic int compare(Card o1, Card o2) {if (o1 o2) {return 0;} if(o1 null) {return -1;}if (o2 null) {return 1;} return o1.rank - o2.rank;}public static void main(String[] args){Card p new Card(1, ♠);Card q new Card(2, ♠);Card o new Card(1, ♠);// 定义比较器对象CardComparator cmptor new CardComparator();// 使用比较器对象进行比较System.out.println(cmptor.compare(p, o)); // 0表示牌相等System.out.println(cmptor.compare(p, q)); // 0表示 p 比较小System.out.println(cmptor.compare(q, p)); // 0表示 q 比较大}
}注意Comparator是java.util 包中的泛型接口类使用时必须导入对应的包
3.3 三种方式对比
覆写的方法说明Object.equals因为所有类都是继承自 Object 的所以直接覆写即可不过只能比较相等与否Comparable.compareTo需要手动实现接口侵入性比较强但一旦实现每次用该类都有顺序属于内部顺序Comparator.compare需要实现一个比较器对象对待比较类的侵入性弱但对算法代码实现侵入性强
4. 集合框架中PriorityQueue的比较方式
集合框架中的PriorityQueue底层使用堆结构因此其内部的元素必须要能够比大小PriorityQueue采用了 Comparable和Comparator两种方式
Comparable是默认的内部比较方式如果用户插入自定义类型对象时该类对象必须要实现Comparable接口并覆写compareTo方法用户也可以选择使用比较器对象如果用户插入自定义类型对象时必须要提供一个比较器类让该类实现Comparator接口并覆写compare方法
// JDK中PriorityQueue的实现
public class PriorityQueueE extends AbstractQueueE implements java.io.Serializable {// ...// 默认容量private static final int DEFAULT_INITIAL_CAPACITY 11;// 内部定义的比较器对象用来接收用户实例化PriorityQueue对象时提供的比较器对象private final Comparator? super E comparator;// 用户如果没有提供比较器对象使用默认的内部比较将comparator置为nullpublic PriorityQueue() {this(DEFAULT_INITIAL_CAPACITY, null);} // 如果用户提供了比较器采用用户提供的比较器进行比较public PriorityQueue(int initialCapacity, Comparator? super E comparator) {// Note: This restriction of at least one is not actually needed,// but continues for 1.5 compatibilityif (initialCapacity 1)throw new IllegalArgumentException();this.queue new Object[initialCapacity];this.comparator comparator;} // ...// 向上调整// 如果用户没有提供比较器对象采用Comparable进行比较// 否则使用用户提供的比较器对象进行比较private void siftUp(int k, E x) {if (comparator ! null)siftUpUsingComparator(k, x);elsesiftUpComparable(k, x);}// 使用ComparableSuppressWarnings(unchecked)private void siftUpComparable(int k, E x) {Comparable? super E key (Comparable? super E) x;while (k 0) {int parent (k - 1) 1;Object e queue[parent];if (key.compareTo((E) e) 0)break;queue[k] e;k parent;}queue[k] key;} // 使用用户提供的比较器对象进行比较SuppressWarnings(unchecked)private void siftUpUsingComparator(int k, E x) {while (k 0) {int parent (k - 1) 1;Object e queue[parent];if (comparator.compare(x, (E) e) 0)break;queue[k] e;k parent;}queue[k] x;}
}5. 使用PriorityQueue创建大小堆解决TOPK问题
//使用比较器创建小根堆
class LessIntComp implements ComparatorInteger{Overridepublic int compare(Integer o1, Integer o2) {return o1 - o2;}
}
//使用比较器创建大根堆
class GreaterIntComp implements ComparatorInteger{Overridepublic int compare(Integer o1, Integer o2) {return o2 - o1;}
}
public class TestDemoE {//求最小的K个数通过比较器创建大根堆public static int[] smallestK(int[] array, int k) {if(k 0) {return null;} GreaterIntComp greaterCmp new GreaterIntComp();PriorityQueueInteger maxHeap new PriorityQueue(greaterCmp);//先将前K个元素创建大根堆for(int i 0; i k; i) {maxHeap.offer(array[i]);} //从第K1个元素开始每次和堆顶元素比较for (int i k; i array.length; i) {int top maxHeap.peek();if(array[i] top) {maxHeap.poll();maxHeap.offer(array[i]);}} //取出前K个int[] ret new int[k];for (int i 0; i k; i) {int val maxHeap.poll();ret[i] val;} return ret;}public static void main(String[] args) {int[] array {4,1,9,2,8,0,7,3,6,5};int[] ret smallestK(array,3);System.out.println(Arrays.toString(ret));}
}