Say*_*iss 5 java protocol-buffers
在包中com.google.protobuf我发现了一个Message界面,它声称它将按内容进行比较:
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);
Run Code Online (Sandbox Code Playgroud)
但我写测试代码:
public class Test {
public static void main(String args[]) {
UserMidMessage.UserMid.Builder aBuilder = UserMidMessage.UserMid.newBuilder();
aBuilder.setQuery("aaa");
aBuilder.setCateId("bbb");
aBuilder.setType(UserMidMessage.Type.BROWSE);
System.out.println(aBuilder.build() == aBuilder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
它给出了false.
那么,如何与proto缓冲区消息进行比较?
==比较对象引用,它检查两个操作数是否指向同一个对象(不是等效对象,同一个对象),因此可以确保.build()每次都创建一个新对象 ...
要使用您发布的代码,您必须与之比较 equals
System.out.println(aBuilder.build().equals(aBuilder.build()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6055 次 |
| 最近记录: |