Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How do I compare two ObservableLists?  RSS feed

 
Greenhorn
Posts: 28
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't sure if this should be in the Beginning Java - though I'm a greenhorn

The Question:
How do I compare two ObservableList?

What I'm trying to do:
Since there can be many sources (e.g. book, video, magazine, etc) )with the same exact title in the database but possibly different authors. I'm attempting to compare a list of Authors for each existing source found in the database to the list of authors entered by a user. So this is my test code for planning and constructing this method in "DoSourceAuthorsExst()" which will most likely become a boolean method. The 'sourceAuthors' will be the result of retrieving all the authors of an existing source as Author objects and compare that list to the user's list of author objects, which the authors table is based.

My Problem:
The two ObservableList<Authors> objects are identical. I literally copied one and pasted it -- renaming the second as 'sourceAuthors'. It seems no matter what I do, the result is always false. I feel I'm overlooking something simple, I'm just too thick to see it. I could use a second or more set of eyes to see it.



the Authors Class:
 
Saloon Keeper
Posts: 5472
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to override equals() and hashCode() in your Authors class. That may be why "contains()" doesn't return true.
 
Master Rancher
Posts: 1061
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not the same list twice...
It might contain the same values, but you create a new object each time.
If you would implement your own toStringmethod and check that for equality on each object in the list it would return true.
Naming conventions would dictate to call your objectclass Author, not Authors and you can also initialize your list on creation instead of adding each object seperately

The toString():


Checking ObservableList:


And this would actually be a javaFx question
 
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got null as a middle name anywhere? Avoid nulls as much as possible. Work out how to create an array of middle names, so you can give somebody an array of different sizes starting from 0 depending on how many middle names they have.
 
Scott Vallery
Greenhorn
Posts: 28
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel and Campbell!

Good points and I totally agree with you both - So I appreciate this site and your patience and efforts.
This is the first application in Java for me and it is further compounded by needing JavaFX and Databases - and I've had 1 weeks worth of high-level training and hit-miss tutorials. -- Sorry for the wrong forum.

After continuing to research, I understand now why the object comparison will always return false. So I was attempting to build out an override of 'equals' as suggested but I was failing miserably. So I think the 'toString' override is a good suggestion too and I should try. I will refactor the naming the 'Authors' to Author.

I will reply with my success or failure! Thanks! It is greatly appreciated.
 
Daniel Demesmaecker
Master Rancher
Posts: 1061
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A way that wouldn't require any overide of methods would be to create your objects first and thenb assing them to both lists.
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the documentation for different kinds of observable List say about the equals() method? I thought there was an equals() method in AbstractList, so all different kinds of List return whether they have the same elements, so a linked list can be equal to an array list if they contain the same elements.
 
Carey Brown
Saloon Keeper
Posts: 5472
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that this will throw a null pointer exception if any field is null. I would suggest modifying your constructor to either: a) throw an exception if any argument is null, or b) convert nulls to empty Strings ("").

Also, your member variables (aka fields) should begin with a lower case letter.
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively you can use the long form of equality test:-or the medium version:-I bet you will go for the medium version.
 
Scott Vallery
Greenhorn
Posts: 28
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, Carey, Campbell!

These are some good thoughts. Let me try these. As a Greenhorn, I need to run them through my thick head a couple times to understand what is happening with each. I did construct the override of "toString" but it has to be too precise. The difficult piece for me every time is dealing with null vs "". I did create an Object compare method to help with this in another section of the code, which maybe I can incorporate.

Thanks! I should have something soon!
 
Scott Vallery
Greenhorn
Posts: 28
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh... and the table entries may be in any order, which I can handle with some looping. I forgot to mention that caveat.
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Scott Vallery wrote:. . . Thanks! . . .

That's a pleasure

I hope my mistake yesterday didn't confuse you; I have just noticed that I wrote || instead of &&. Please show us your compare() method, but there is a risk that I shall tell you to change its name as soon as I see it.
Remember that List equality includes both identity of elements and their order.And rhyme isn't a good name for a List at all.
 
Daniel Demesmaecker
Master Rancher
Posts: 1061
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Scott Vallery wrote:Oh... and the table entries may be in any order, which I can handle with some looping. I forgot to mention that caveat.


What tableentries are you talking about? In to a tableview? Cause there's a simpeler way to do it, setting the celValueFactory to the propertyValueFactory.
 
Scott Vallery
Greenhorn
Posts: 28
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone!

Daniel -- sorry, I wasn't as clear as I should have been, but if a user doesn't know a source exists, they may add the authors in a different order that listed on the database retrieval. However, I see what you are saying... and I could probably just us the 'sorted()' method on each ObservableArrayList... duh!. Like I said, I can be thick.

Right now the goal is to get something working then begin to refactor to be more efficient. I feel (obviously ) this application can be far more efficient and understandable. I'm just not experienced enough and naive at the present time.

Anyhow, this is working at present. I continued with the overriding of the "toString()" and applied part of Carey's suggestion to convert any 'null' value as a "". My Convert method may be in efficient, as I mentioned before, the null and "" and " " are what I'm trying to handle.




Author Class:

 
Daniel Demesmaecker
Master Rancher
Posts: 1061
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldnt check of the object is null, but if any of the fields is null
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the fact that it shouldn't start with a CapitalLetter, your convert method does some unnecessary things. Just about the only object to return "" from its toString() method is the empty String. I think you can reduce it to this:-You can probably reduce it to this, too:-I tried my list equality test again with a LinkedList:
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody has pointed out that convertToEmptyString() is a bad name for that method.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!