How to compare objects by multiple fields

后端 未结 22 2592
暖寄归人
暖寄归人 2020-11-22 00:43

Assume you have some objects which have several fields they can be compared by:

public class Person {

    private String firstName;
    private String lastN         


        
22条回答
  •  后悔当初
    2020-11-22 01:40

    I think it'd be more confusing if your comparison algorithm were "clever". I'd go with the numerous comparison methods you suggested.

    The only exception for me would be equality. For unit testing, it's been useful to me to override the .Equals (in .net) in order to determine if several fields are equal between two objects (and not that the references are equal).

提交回复
热议问题