gpt4 book ai didi

java - 如何比较 Java 中的两个原型(prototype)缓冲区消息?

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:34 24 4
gpt4 key购买 nike

com.google.protobuf 包中,我找到了一个 Message 接口(interface),它声称它将按内容进行比较:

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

但是我写测试代码:

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

它给出 false

那么,如何比较 proto buffer 消息呢?

最佳答案

== 比较对象引用,它检查两个操作数是否指向同一个对象(不是等效对象,同一个对象),因此您可以确定 .build() 每次都会创建一个新对象...

要使用您发布的代码,您必须与 equals

进行比较
System.out.println(aBuilder.build().equals(aBuilder.build()));        

关于java - 如何比较 Java 中的两个原型(prototype)缓冲区消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30433162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com