如何比较Java中的两个原始缓冲区消息?
在package如何比较Java中的两个原始缓冲区消息?,java,protocol-buffers,Java,Protocol Buffers,在packagecom.google.protobuf中,我发现了一个消息界面,它声称将按内容进行比较: public interface Message extends MessageLite, MessageOrBuilder { // ----------------------------------------------------------------- // Comparison and hashing /** * Compares the specifie
com.google.protobuf
中,我发现了一个消息
界面,它声称将按内容进行比较:
public interface Message extends MessageLite, MessageOrBuilder {
// -----------------------------------------------------------------
// Comparison and hashing
/**
* Compares the specified object with this message for equality. Returns
* <tt>true</tt> if the given object is a message of the same type (as
* defined by {@code getDescriptorForType()}) and has identical values for
* all of its fields. Subclasses must implement this; inheriting
* {@code Object.equals()} is incorrect.
*
* @param other object to be compared for equality with this message
* @return <tt>true</tt> if the specified object is equal to this message
*/
@Override
boolean equals(Object other);
它给出false
那么,如何比较proto buffer消息呢?在Java中,您需要使用
equals
方法比较对象,而不是使用=
操作符。问题是=
比较它是否是同一个对象,而equals
方法比较它们是否与类的开发人员提供的实现相等
System.out.println(aBuilder.build().equals(aBuilder.build()));
有关更多详细信息,已经有很多问题了(例如。
=
比较对象引用,它检查两个操作数是否指向同一对象(不是等效对象,同一对象),因此可以确定.build()
每次创建一个新对象
要使用您发布的代码,您必须与equals
System.out.println(aBuilder.build().equals(aBuilder.build()));
如果您想调用
equals
,您应该完全这样做-您当前使用的是=
。。。
System.out.println(aBuilder.build().equals(aBuilder.build()));