• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashmap getting the objects that have the same variable values  RSS feed

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have a DTO Cases



In the Test implementation file I have a Hashmap as follows. What I am looking for is to find out the objects of Cases that has the same SSN values in the Cases object. I have work these same SSN cases as a unit instead of individual cases. Please suggest.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you, you want a list of Cases that have the same SSN.
 
Luke Zechariah
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Carey that is right.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you understand the code snippet I provided?

You class Cases should be singular "Case".

You potentially set everything twice.

you should collect all of your set'ings together, as in
 
Luke Zechariah
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey, I understand the setXXX part and the coding. But that does not answer my problem of how to get the Case objects having the same SSN or am I missing something?
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a map who's key is a SSN and who's value is a list of cases.
This adds a Case to a List of cases mapped to a SSN.
To retrieve the List:
 
Luke Zechariah
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Carey, but that still does not fix my problem. Because

1. If I use SSN as the Key then since I will be having more the 2 objects with the same key SSN, the first object will replaced by the second object when we put the second object using the same key.
2. Ok I can make up a SSN+something unique as the key, still how can I get the Cases with the same SSN



 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luke Zechariah wrote:Thank you Carey, but that still does not fix my problem. Because
1. If I use SSN as the Key then since I will be having more the 2 objects with the same key SSN, the first object will replaced by the second object when we put the second object using the same key.
2. Ok I can make up a SSN+something unique as the key, still how can I get the Cases with the same SSN

You are not interpreting my suggestion correctly. The map, as I suggest, has a key that is the SSN. The value is NOT a Case, it is a LIST of cases. So, when you put an entry into the map you are passing in a list of Cases for the value, and yes, if you use the same SSN as you've used before the old list will be replaced with a new list which is why the new list should contain all the Cases from the old list plus the new Case to be added. This is what my suggested addCase() method will do for you.
 
Luke Zechariah
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Carey, glad that you are understanding better than what I really want.

It worked !!  One more question how to iterate a map. Is this right?



 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. That should work nicely.
 
Ranch Hand
Posts: 86
18
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sidenote: The addCase method can be shortened to something like
 
Luke Zechariah
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Carey, just a thought since there are 2 for loops so my time complexity is O(n*N), but is there way we could decrease it?

And Tobias thank you for your suggestion. I tried your code, I am not using Java 8, so "s ->" is showing compile time error? If I had Java 8, it would not right?

 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luke Zechariah wrote:...there are 2 for loops so my time complexity is O(n*N), but is there way we could decrease it?
The loops are pretty bare-boned, so, no.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Luke Zechariah wrote:...there are 2 for loops so my time complexity is O(n*N), but is there way we could decrease it?
The loops are pretty bare-boned, so, no.

I'm not up on my big O stuff. It seems that you really don't have an inner-loop-N times an outer-loop-N complexity. You really just have a list of Cases, the fact that you've subdivided the cases by SSN doesn't mean that there's any more cases.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey is correct. This algorithm is O(n), where n is the number of Cases in the map. The partition over different lists doesn't matter.
 
Luke Zechariah
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey and Stephan.  I have another question, I am trying to assign examiners to the list of Cases.

Requirements:
1. I have to assign each case to each examiner whose workload is below average workload, one case at a time.
2. If two cases have the same SSN then both cases should be assigned to the same examiner.
3. I cannot assign all cases to one case who workload is below average but assign one at a time and  then come back give them again.


For eg: lets say we have 10 cases with different SSNs and three examiners.
Case 1, 2, 3 should be given to Ex 1, 2, 3 respectively
Case 4, 5, 6 should be given to Ex 1, 2, 3 respectively
Case 7, 8, 9 should be given to Ex 1, 2, 3 respectively
Case 10 to Ex 1.

Here is the code





CaseAllocation.java







 
Luke Zechariah
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was typo in my previous posting for Item#3: 

3. I cannot assign all cases to one examiner who workload is below average but assign one at a time and  then come back give them again.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't an Examiner have a List<Case> of cases he/she is working on? And wouldn't the workload be derived from the number of cases in the list?
 
Luke Zechariah
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I got another question suppose I have list of case types. I want to rearrange the items in the Arraylist, Here are the rules:

C1 is processed first
then A1,
then B1,
then D1.

But the current order stored in the list is as follows: A1, B1, C1, D1.



Output prints [C1, A1, B1, D1] as desired.    Is there a better way than this using Hasp Map instead of this recursive thing.


 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't follow what you are trying to do with that method. Why are you explicitly handling 'A1'?

If you want to shuffle lib, why not just
Collections.shuffle( lib )
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!