*
The moose likes Java in General and the fly likes Comparing Arraylist that is a value of Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing Arraylist that is a value of Hashmap" Watch "Comparing Arraylist that is a value of Hashmap" New topic
Author

Comparing Arraylist that is a value of Hashmap

Charles Mowad
Greenhorn

Joined: Jun 04, 2012
Posts: 4
This is going to be tough to explain, but I'm running through a rating's system.

I have a hashmap in which the key is a client's name, and the value is an ArrayList that is holding a bunch of numbers
An example:
1001: [1, 0, 0, -3, 0, -3, 0, 0, 0, 0, 0, 0, 0, 3, 1, -5, 0, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


The key, in this case, is "1001" and the value is the array list found above.

The program has about 400 pairs of key and value, and I want to compare each one of those 400 with a new arrayList that is not part of the hashmap.

i.e.
compare 1001's value with newUser's arrayList.

I thought the best way to approach this would be through an iterator that goes through all the elements and holds the value (the arraylist):



And then the arrayList left with the largest 50th element would be considered most similar and then further comparisons will take place.
How would I go approaching this? My head hurts from repetitively trying to attack this problem.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Welcome to the Java Ranch!

Your idea of using an iterator is fine (although you could hide it by using a for-loop). But where are you having problems now? What are current.get or current.add, or newUser.get?
Charles Mowad
Greenhorn

Joined: Jun 04, 2012
Posts: 4
Thanks!
Hopefully you can see how I am using current.get, current.add, and newUser.get in the program. current is the current ArrayList that is being traversed, and i am "get"ting the nth element so that I may compare it with the user's arrayList (I rushed and called it newUser in my example, but it is called ratings). Therefore, ratings.get allows me to retrieve an element from the same index to compare it to the currentArray. Finally, because I am looking at similarities, I was thinking that I could add one last index in the arrayLists that starts at 100, and decreases according to the differences between the arrayLists. The bigger the difference, the greater the decrease.


I managed to get it working:


I am outputting only to check if it is working.

So now, the 50th index has an element that shows how similar an arrayList is to the user's arrayList.

I have a new problem
How do I run through the previous arraylists, and pick out the 3 arrayLists with the largest 50th index from the hashmap? (the 3 most similar arraylists)?

i.e. Let's say I have the following arrays:

1323 [-5, 0, 0, 5, 3, 5, 3, 5, 0, 0, 0, 1, 0, 0, 3, 0, 3, 1, 3, 0, -3, 3, 0, -5, 0, 3, 5, 0, 0, 3, 0, 0, 1, 0, 3, 0, 3, 0, 0, 5, 1, 0, 0, 0, 0, 3, 1, 3, 0, 5, 12]
1322 [3, -3, 3, 5, 1, 1, 5, 1, -5, 3, 5, 5, 0, 5, 3, 1, 5, 0, 1, 0, 3, 5, 1, 5, 1, 5, 0, 3, -3, 1, 1, 0, 3, 5, 5, 0, -3, -3, 5, 0, -3, 1, 0, -5, 0, 3, 3, 5, 5, 5, 30]


You can see that 1322 has the larger element in the 50th index. How would I retrieve all the values and compare them from an Arraylist nested in a hashmap?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
Welcome again
What about the scores being recorded in a Scores class? What about a similarity method which calculates the similarity or dissimilarity to another Scores object? What about the containsValue method of the Map interface? What about the Scares class implementing Comparable<Scores>? What about enhancing the compareTo method to return the dissimilarity between two Scores objects?
There are all sorts of things you can do; those just occurred to me in the time it took me to enter some newlines in your post, because the long //comments inside code tags are difficult to read.
Charles Mowad
Greenhorn

Joined: Jun 04, 2012
Posts: 4
Hey, I understand what you mean, this is for a small project -- non-commercial, so I don't want to waste any more time on it.

I'm attacking this with only one class (I would restart and use multiple classes, but I'm almost done!)

I'm trying something out with regards to picking out the most similar arrayList, I'll post my results =)
Charles Mowad
Greenhorn

Joined: Jun 04, 2012
Posts: 4
Campbell Ritchie wrote:Welcome again
What about the scores being recorded in a Scores class? What about a similarity method which calculates the similarity or dissimilarity to another Scores object? What about the containsValue method of the Map interface? What about the Scares class implementing Comparable<Scores>? What about enhancing the compareTo method to return the dissimilarity between two Scores objects?
There are all sorts of things you can do; those just occurred to me in the time it took me to enter some newlines in your post, because the long //comments inside code tags are difficult to read.


Alright! Was pretty simple:



This ensures that the most similar user is chosen. I'll work on getting secondary choices later, but so far, so good!

Thanks for all your help. Asking the question is what actually got me to the solution!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Charles Mowad wrote:this is for a small project -- non-commercial, so I don't want to waste any more time on it.

I'm attacking this with only one class (I would restart and use multiple classes, but I'm almost done!)

Yeah, I hear this all the time. And this too:
Charles Mowad wrote:My head hurts from repetitively trying to attack this problem.

You don't have to restart, you have to refactor.

Poorly structured code is the biggest drain on time and effort (proportional to $ and inversely proportional to happiness / sanity ). It's also the breeding ground for bugs and despair (an early symptom of which: headaches).

One smell in the code is the data structure you're using. The list combines two concerns: a list of ratings and a similarity score at the end. You should separate those.

This way, you can refer to the data by their names and not by their positions in the structure (i.e. get(50) really means 'similarity').

Another code smell is that you're trying to do a lot of different things in one method. A method should do one and only one thing and it should do it well. The method name should be a declaration of what that method does, for example, 'findClosestMatches' or 'adjustSimilarityScore'. Any code that has nothing to do with that particular purpose/goal or is a subordinate goal to it should be extracted to a separate method.

These are just a couple of things you can do to make your code a better place. Well-structured code is easier to read, understand, and work with. This saves time and effort and therefore saves you money, increases happiness, and saves your sanity. Plus fewer headaches.

Junilu - [How to Ask Questions] [How to Answer Questions]
 
 
subject: Comparing Arraylist that is a value of Hashmap