Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Comparing two Objects

DZone's Guide to

Comparing two Objects

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

I recently wanted to compare two objects to see if they were the same. After looking through the cfml ref I couldn't find anything, but I did find that you can use ArrayFind to do it.

Here's a quick test I wrote:

Foo.cfc

component 
{ 
  this.uuid = CreateUUID(); 
}

test.cfm

<cfset foo = new foo()>
<cfset bar = new foo()>
<cfoutput>

is foo equal to foo? #ArrayFind( [foo], foo ) gt 0#<br />
is foo equal to bar? #ArrayFind( [foo], bar ) gt 0#<br />
</cfoutput>

The output when I run this script is:

is foo equal to foo? YES

is foo equal to bar? NO

It also works with persistent entities, this is a test I wrote using the cfcodeexplorer datasource:

<cfset NewArtist = EntityNew( "Artist" )>
<cfset Artist = EntityLoadByPK( "Artist", 1 )>
<cfset Artist2 = EntityLoadByPK( "Artist", 1 )>

<cfset foo = new foo()>

<cfoutput>
is Artist equal to Artist? #ArrayFind( [Artist], Artist ) gt 0#<br />
is NewArtist equal to Artist? #ArrayFind( [NewArtist], Artist ) gt 0#<br />
is Artist equal to Artist2? #ArrayFind( [Artist], Artist2 ) gt 0#<br />
is foo equal to Artist? #ArrayFind( [foo], Artist ) gt 0#
</cfoutput>

The output when I run this script is:

is Artist equal to Artist? YES
is NewArtist equal to Artist? NO
is Artist equal to Artist2? YES
is foo equal to Artist? NO

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of John Whish, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}