• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Solving a Problem with Sets  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am having a hard time figuring out which is the best way to implement the following problem:

I am connecting to two different databases and I want to compare the elements using java.
For example DatabaseA is a database of users that have an ID Number and a Social Care Number. The ID Number of a user may be null, but the Social Care Number is always not null.
DatabaseB has users that have an ID Number and a Social Care Number and both are never null.
I want to find out which users of Database A do not exist in DatabaseB.


        Database A
------------------------------
ID Number      Social Care Number
123                 abc
                def

       Database B
------------------------------
ID Number      Social Care Number
123                 abc
456                 def




I have done the following:



I do a query and add in usersA users that have empty IDNumber. These users have only Social Care Number. If a user exists in B then I add him in the usersA set with the ID Number that matches in B.

If a user does not exist in B then I add him in another in another Set as follows:







Then I do another query and get all users from Database A that have an ID Number. And try to find which users exist in A and do not exist in B:

         


In the end the usersThatExistInAandDoNotExistInB has some users that have only IDNumber and no Social Care Number,
and users that have only Social Care Number and I get something like this:


[{Social Care Number=, ID Number=123}, {Social Care Number=, ID Number=456}, {Social Care Number=, ID Number=789}, {Social Care Number=abc, ID Number= }, {Social Care Number=edf, ID Number= }]

I feel that this got very complicated and there is a better solution.
I want for users that have ID Number to fill also the Social Care Number. For users that only have a Social Care Number its like this.
Does anyone has a better solution to this?


Thank you
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should look at the API documentation for Set ... It has a removeAll() method that should do what you need with a lot less fuss.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to use the removeAll and retainAll methods with a HashMap instead of a HashSet?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked in the documentation for Map?
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok yes sorry my mind just stopped , I will use a Map with the remove methods and my problem will be solved
Thanks
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:

That is a strange way of using a Map. It doesn't look right either. I would think you'd do something like this instead:
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you !!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!