Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何比较Java中的两个原始缓冲区消息?_Java_Protocol Buffers - Fatal编程技术网

如何比较Java中的两个原始缓冲区消息?

如何比较Java中的两个原始缓冲区消息?,java,protocol-buffers,Java,Protocol Buffers,在packagecom.google.protobuf中,我发现了一个消息界面,它声称将按内容进行比较: public interface Message extends MessageLite, MessageOrBuilder { // ----------------------------------------------------------------- // Comparison and hashing /** * Compares the specifie

在package
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()));