Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compare two objects in Java

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to compare two objects in Java". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to compare two objects in Java".

The Builder package in Common Lang provides a DiffBuilder class that compares two objects and returns different parts.

First implement the Diffable interface in the class where you want to compare objects, and then implement the DiffResult diff (T obj) method.

In the DiffResult diff (T obj) method, create a new DiffBuilder object and put the class attributes that need to be compared into DiffBuilder one by one.

The constructor of DiffBuilder has three input parameters, lhs is the current object, rhs is the object to be compared, and style is the format of the output of the comparison results.

DiffBuilder (final T lhs, final T rhs, final ToStringStyle style)

Suppose you have a Person class that defines three properties, name, age, and smoker. The following is the output corresponding to using different ToStringStyle.

ToStringStyle description output SHORT_PREFIX_STYLE short Person [name=deniro,smoker=false] differs from Person [name=jack,smoker=true] DEFAULT_STYLE default org.apache.commons.lang3.builder.Person@9f70c54 [name=deniro,smoker=false] differs from org.apache.commons.lang3.builder.Person@234bef66 [name=jack,smoker=true]. MULTI_LINE_STYLE multiline org.apache.commons.lang3.builder.Person@9f70c54 [

Name=deniro

Smoker=false

] differs from org.apache.commons.lang3.builder.Person@234bef66 [

Name=jack

Smoker=true

] NO_FIELD_NAMES_STYLE does not contain the class attribute name org.apache.commons.lang3.builder.Person@9f70c54 [deniro,false] differs from org.apache.commons.lang3.builder.Person@234bef66 [jack,true]. SIMPLE_STYLE is concise and deniro,false differs from jack,true. NO_CLASS_NAME_STYLE does not contain the class name [name=deniro,smoker=false] differs from [name=jack,smoker=true]. JSON_STYLEJSON {"name": "deniro", "smoker": false} differs from {"name": "jack", "smoker": true}.

It feels like NO_CLASS_NAME_STYLE and JSON_STYLE can see more clearly.

Complete example:

Public class Person implements Diffable {private static final Logger log = LoggerFactory.getLogger (Person.class); String name; int age; boolean smoker Public DiffResult diff (Person obj) {/ / No need for null check, as NullPointerException correct if obj is null return new DiffBuilder (this, obj, ToStringStyle.SHORT_PREFIX_STYLE) .append ("name", this.name, obj.name) .append ("age", this.age, obj.age) .append ("smoker", this.smoker, obj.smoker) .build () } public static void main (String [] args) {Person a = new Person (); a.name = "deniro"; a.age = 22; a.smoker = false; Person b = new Person (); b.name = "jack"; b.age = 22; b.smoker = true; DiffResult result = a.diff (b) Log.info ("result-> {}." , result);}}

Output:

Result-> Person [name=deniro,smoker=false] differs from Person [name=jack,smoker=true].

If it is a large class, then you need a lot of append methods to put the properties of this large class into DiffBuilder, which is not convenient. You can write a program to generate DiffBuilder initialization code, or try to optimize this part of the code with reflection.

Reflection extension:

(1) get the Class method of the class to which the object belongs.

Class c = Class.forName ("package. Class name")

(2) get all the Filed of the class (excluding the parent class).

Field [] fields = c.getDeclaredFields ()

(3) set the access permissions for all properties to true.

Because all properties in JavaBean have access to private, it is not possible to get all properties directly, so we must obtain their access through the following methods:

SetAccessible (true)

(4) output object attributes

F.getField (object name)

(5) get the field name

Fields[ j] .getName ()

(6) get the field value

Fields.get (obj) above is all the content of the article "how to compare two objects in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report