java对象的比较
大家好,我是晓星航。今天为大家带来的是 java对象的比较 相关内容的讲解!😀
上节课我们讲了优先级队列,优先级队列在插入元素时有个要求:插入的元素不能是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()
{
PriorityQueue<Card> p = new PriorityQueue<>();
p.offer(new Card(1, "♠"));
p.offer(new Card(2, "♠"));
}
public static void main(String[] args) {
TestPriorityQueue();
}
}
优先级队列底层使用堆,而向堆中插入元素时,为了满足堆的性质,必须要进行元素的比较,而此时Card是没有办法直接进行比较的,因此抛出异常。
在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);
}
}
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指向的是同一个对象
}
}
c1、c2和c3分别是Card类型的引用变量,上述代码在比较编译时:
c1 > c2 编译失败
c1== c2 编译成功
c1 < c2 编译失败
从编译结果可以看出,Java中引用类型的变量不能直接按照 > 或者 < 方式进行比较。 那为什么==可以比较?
因为:对于用户实现自定义类型,都默认继承自Object类,而Object类中提供了equal方法,而==默认情况下调用的就是equal方法,但是该方法的比较规则是:没有比较引用变量引用对象的内容,而是直接比较引用变量的地址,但有些情况下该种比较就不符合题意。
// Object中equal的实现,可以看到:直接比较的是两个引用变量的地址
public boolean equals(Object obj) {
return (this == obj);
}
有些情况下,需要比较的是对象中的内容,比如:向优先级队列中插入某个对象时,需要对按照对象中内容来调整堆,那该如何处理呢?
public class Card {
public int rank; // 数值
public String suit; // 花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public 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 rank == c.rank
&& suit.equals(c.suit);
}
}
注意: 一般覆写 equals 的套路就是上面演示的
覆写基类equal的方式虽然可以比较,但缺陷是:equal只能按照相等进行比较,不能按照大于、小于的方式进行比较。
Comparble接口类的比较Comparble是JDK提供的泛型的比较接口类,源码实现具体如下:
public interface Comparable<E> {
// 返回值:
// < 0: 表示 this 指向的对象小于 o 指向的对象
// == 0: 表示 this 指向的对象等于 o 指向的对象
// > 0: 表示 this 指向的对象等于 o 指向的对象
int compareTo(E o);
}
对用用户自定义类型,如果要想按照大小与方式进行比较时:在定义类时,实现Comparble接口即可,然后在类中重写compareTo方法。
public class Card implements Comparable<Card> {
public int rank; // 数值
public String suit; // 花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
// 根据数值比较,不管花色
// 这里我们认为 null 是最小的
@Override
public 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 比较大
}
}
Compareble是java.lang中的接口类,可以直接使用。
按照比较器方式进行比较,具体步骤如下:
public interface Comparator<T> {
// 返回值:
// < 0: 表示 o1 指向的对象小于 o2 指向的对象
// == 0: 表示 o1 指向的对象等于 o2 指向的对象
// > 0: 表示 o1 指向的对象等于 o2 指向的对象
int compare(T o1, T o2);
}
注意:区分Comparable和Comparator。
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 Comparator<Card> {
// 根据数值比较,不管花色
// 这里我们认为 null 是最小的
@Override
public 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 包中的泛型接口类,使用时必须导入对应的包。
PriorityQueue的比较方式🐹集合框架中的PriorityQueue底层使用堆结构,因此其内部的元素必须要能够比大小,PriorityQueue采用了:Comparble和Comparator两种方式。
Comparble是默认的内部比较方式,如果用户插入自定义类型对象时,该类对象必须要实现Comparble接口,并覆写compareTo方法Comparator接口并覆写compare方法。// JDK中PriorityQueue的实现:
public class PriorityQueue<E> extends AbstractQueue<E>
implements java.io.Serializable {
// ...
// 默认容量
private static final int DEFAULT_INITIAL_CAPACITY = 11;
// 内部定义的比较器对象,用来接收用户实例化PriorityQueue对象时提供的比较器对象
private final Comparator<? super E> comparator;
// 用户如果没有提供比较器对象,使用默认的内部比较,将comparator置为null
public 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 compatibility
if (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);
else
siftUpComparable(k, x);
}
// 使用Comparable
@SuppressWarnings("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;
}
}
PriorityQueue🐰class LessIntComp implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
}
class GreaterIntComp implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}
// 假设:创建的是小堆----泛型实现
public class MyPriorityQueue<E> {
private Object[] hp;
private int size = 0;
private Comparator<? super E> comparator = null;
// java8中:优先级队列的默认容量是11
public MyPriorityQueue(Comparator<? super E> com) {
hp = new Object[11];
size = 0;
comparator = com;
}
public MyPriorityQueue() {
hp = new Object[11];
size = 0;
comparator = null;
}
// 按照指定容量设置大小
public MyPriorityQueue(int capacity) {
capacity = capacity < 1 ? 11 : capacity;
hp = new Object[capacity];
size = 0;
}
// 注意:没有此接口,给学生强调清楚
// java8中:可以将一个集合中的元素直接放到优先级队列中
public MyPriorityQueue(E[] array){
// 将数组中的元素放到优先级队列底层的容器中
hp = Arrays.copyOf(array, array.length);
size = hp.length;
// 对hp中的元素进行调整
// 找到倒数第一个非叶子节点
for(int root = ((size-2)>>1); root >= 0; root--){
shiftDown(root);
}
}
// 插入元素
public void offer(E val){
// 先检测是否需要扩容
grow();
// 将元素放在最后位置,然后向上调整
hp[size] = val;
size++;
shiftUp(size-1);
}
// 删除元素: 删除堆顶元素
public void poll(){
if(isEmpty()){
return;
}
// 将堆顶元素与堆中最后一个元素进行交换
swap((E[])hp, 0, size-1);
// 删除最后一个元素
size--;
// 将堆顶元素向下调整
shiftDown(0);
}
public int size(){
return size;
}
public E peek(){
return (E)hp[0];
}
boolean isEmpty(){
return 0 == size;
}
// 向下调整
private void shiftDown(int parent){
if(null == comparator){
shiftDownWithcompareTo(parent);
}
else{
shiftDownWithComparetor(parent);
}
}
// 使用比较器比较
private void shiftDownWithComparetor(int parent){
// child作用:标记最小的孩子
// 因为堆是一个完全二叉树,而完全二叉树可能有左没有有
// 因此:默认情况下,让child标记左孩子
int child = parent * 2 + 1;
// while循环条件可以一直保证parent左孩子存在,但是不能保证parent的右孩子存在
while(child < size)
{
// 找parent的两个孩子中最小的孩子,用child进行标记
// 注意:parent的右孩子可能不存在
// 调用比较器来进行比较
if(child+1 < size && comparator.compare((E)hp[child+1], (E)hp[child]) < 0 ){
child += 1;
}
// 如果双亲比较小的孩子还大,将双亲与较小的孩子交换
if(comparator.compare((E)hp[child], (E)hp[parent]) < 0) {
swap((E[])hp, child, parent);
// 小的元素往下移动,可能导致parent的子树不满足堆的性质
// 因此:需要继续向下调整
parent = child;
child = child*2 + 1;
}
else{
return;
}
}
}
// 使用compareTo比较
private void shiftDownWithcompareTo(int parent){
// child作用:标记最小的孩子
// 因为堆是一个完全二叉树,而完全二叉树可能有左没有有
// 因此:默认情况下,让child标记左孩子
int child = parent * 2 + 1;
// while循环条件可以一直保证parent左孩子存在,但是不能保证parent的右孩子存在
while(child < size)
{
// 找parent的两个孩子中最小的孩子,用child进行标记
// 注意:parent的右孩子可能不存在
// 向上转型,因为E的对象都实现了Comparable接口
if(child+1 < size && ((Comparable<? super E>)hp[child]).
compareTo((E)hp[child])< 0){
child += 1;
}
// 如果双亲比较小的孩子还大,将双亲与较小的孩子交换
if(((Comparable<? super E>)hp[child]).compareTo((E)hp[parent]) < 0){
swap((E[])hp, child, parent);
// 小的元素往下移动,可能导致parent的子树不满足堆的性质
// 因此:需要继续向下调整
parent = child;
child = child*2 + 1;
}
else{
return;
}
}
}
// 向上调整
void shiftUp(int child){
if(null == comparator){
shiftUpWithCompareTo(child);
}
else{
shiftUpWithComparetor(child);
}
}
void shiftUpWithComparetor(int child){
// 获取孩子节点的双亲
int parent = ((child-1)>>1);
while(0 != child){
// 如果孩子比双亲还小,则不满足小堆的性质,交换
if(comparator.compare((E)hp[child], (E)hp[parent]) < 0){
swap((E[])hp, child, parent);
child = parent;
parent = ((child-1)>>1);
}
else{
return;
}
}
}
void shiftUpWithCompareTo(int child){
// 获取孩子节点的双亲
int parent = ((child-1)>>1);
while(0 != child){
// 如果孩子比双亲还小,则不满足小堆的性质,交换
if(((Comparable<? super E>)hp[child]).compareTo((E)hp[parent]) < 0){
swap((E[])hp, child, parent);
child = parent;
parent = ((child-1)>>1);
}
else{
return;
}
}
}
void swap(E[] hp, int i, int j){
E temp = hp[i];
hp[i] = hp[j];
hp[j] = temp;
}
// 仿照JDK8中的扩容方式,注意还是有点点的区别,具体可以参考源代码
void grow(){
int oldCapacity = hp.length;
if(size() >= oldCapacity){
// Double size if small; else grow by 50%
int newCapacity = oldCapacity + ((oldCapacity < 64) ?
(oldCapacity + 2) :
(oldCapacity >> 1));
hp = Arrays.copyOf(hp, newCapacity);
}
}
public static void main(String[] args) {
int[] arr = {4,1,9,2,8,0,7,3,6,5};
// 小堆---采用比较器创建小堆
MyPriorityQueue<Integer> mq1 = new MyPriorityQueue(new LessIntComp());
for(int e : arr){
mq1.offer(e);
}
// 大堆---采用比较器创建大堆
MyPriorityQueue<Integer> mq2 = new MyPriorityQueue(new GreaterIntComp());
for(int e : arr){
mq2.offer(e);
}
// 小堆--采用CompareTo比较创建小堆
MyPriorityQueue<Integer> mq3 = new MyPriorityQueue();
for(int e : arr){
mq3.offer(e);
}
}
}
感谢各位读者的阅读,本文章有任何错误都可以在评论区发表你们的意见,我会对文章进行改正的。如果本文章对你有帮助请动一动你们敏捷的小手点一点赞,你的每一次鼓励都是作者创作的动力哦!😘
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir