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

那么,如何与原型(prototype)缓冲区消息进行比较?

最佳答案

...

要使用您发布的代码,您必须将其与==进行比较

System.out.println(aBuilder.build().equals(aBuilder.build()));

10-01 19:15