• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using arraylist index and items as hashmap key and values?  RSS feed

 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am working on the Kevin Bacon - 6 degrees of bacon problem.

I want to take my Array List and use the index and Values that I have saved in it as the Key and Value of a Hashmap.
Below is my code.



I know it's sloppy...please don't judge me lol

The output from this snip is this:



saving each one in it's own spot.
I want to use the index of each one as the key to find it in the Hashmap....
or is there a better way to do this??

Thank you in advance for your help!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayumi yamada wrote:I want to take my Array List and use the index and Values that I have saved in it as the Key and Value of a Hashmap.

First: Welcome to JavaRanch, mayumi.

Second: Please DontWriteLongLines. They make you thread very hard to read. I've broken yours up this time.

Third: Your ArrayList should be defined as:
ArrayList<String> array = new ArrayList<String>();

Fourth: Since (I assume) you want each entry in your ArrayList to correspond with a single mapping in your Map, try:
Map<Integer, String> actors = new HashMap<Integer, String>();
instead.
(Also note the use of Map, rather than HashMap - it's generally a better way to do things.)

Fifth: Since you want each actor to have his/her own mapping. I would suggest that you add another loop to go through your ArrayList and put() each element into your Map.

Sixth: You can remove all that try...catch stuff by simply having the method that includes it declared with:
throws IOException

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why not use the index to find things in the List? Don't call a List array. It is confusing. You can put a number and a List into a Map, that is easy. But if you are adding things to a random access List, it is quicker to use the index on the List. You can have a List<List<String>> and find the List<String> with myList.get(123);
But there must be an easier way to do it. Why not add the Strings to a List<String> and split them later?

By the way, where does the question come from?
 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie - Sorry but I do not understand what you are saying. I did not call it an List Array...
it's an ArrayList. I will look into the Map.

Winston Gutkowski - My apologies for the long lines. As for the ArrayList<String> array = new ArrayList<String>();
when I do that I get an error and it wants me to change to add, to addall and when I do that - it does not put the items in by themselves but all in one spot.
My I ask for an example for the Map or the way the ArrayList<String> array = new ArrayList<String>(); will hold the items correctly?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayumi yamada wrote:when I do that I get an error and it wants me to change to add, to addall and when I do that - it does not put the items in by themselves but all in one spot.

Because you're using Arrays.asList().

I suspect you're overthinking this. You're adding to your ArrayList at this point, so all you need is:
array.add(line)
or possibly:
array.add(line.trim())
That will get each "actor" into your list, which you can then (after you have them all) put into your Map.

However, the split() suggests to me that you want to separate out parts of an actor's name, which is fine; but I would then advise that you create an Actor class and put those details in there.

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayumi yamada wrote:Campbell Ritchie - Sorry but I do not understand what you are saying. I did not call it an List Array... . . .
No, you didn't. You are quite right in that. You called it array. Look: here it is in this quote from your first post:
Anybody reading your code quickly will think that is an array. It is not an array, and an ArrayList is not an array; it is a List. you should have given it a different identifier, maybe:-Also, you should give that List a type parameter. Read about generics.
 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, So I have made some progress but it is still not doing what I would like :




The output


It prints that line about 16 times!

Could someone explain to me what I am doing wrong?
I want to save each one of the items after the second comma as a value and the count will be it's key found in the map.
I want to use the count as the key because the input file we have to use is all numbers and we have to match the numbers with the Actors.



This is how the original actors.txt file looked which is why I was doing a split("\t")

the movie and actor file looks like this:



Way longer than the snip above of course, I just cut it for example purposes.
The first number is the movie ref and the other numbers separated by spaces are the actor numbers.

Thank you in advance!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayumi yamada wrote:Could someone explain to me what I am doing wrong?

Yes. You're putting the entire list in every entry of your Map, which is almost certainly NOT what you want.

If you read my first post, you'll see that I suggested making your Map a
Map<Integer, String>
rather than a
Map<Integer, ArrayList>

To be honest, you could possibly just get rid of the list altogether and put your lines directly into your Map, viz:But if you need the List as well, then that might not be enough.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just FYI, you could make the above loop a for loop, viz:which is a little more self-contained.

for loops aren't just for processing lists and arrays.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually use that syntax with a while loop
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I usually use that syntax with a while loop...

I guess it's what you prefer. I rather like for loops for counting, simply because you don't have to define the counter outside it, but I certainly have no religious convictions about it...

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having the counter inside the loop may alter it from a 1‑based index to a 0‑based index, or vice versa, but agree the two do more or less the same.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still fail to see what this gains beyond what you would get with a List.
 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank y'all so much for your help! That worked perfectly.

I am now working on putting the number movie and actors list into an hashmap and then instead of printing the numbers from that list, have it print the real names from the other hashmaps.
I'm just writing them in the main, so I can test as I go before I turn them into methods.

Here is my idea:



Since the second value of appearances will be multiple ones, will it be okay to use ArrayList then? Is there a better way to do that or am I on the right track this time?

Thank you again!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayumi yamada wrote:Since the second value of appearances will be multiple ones, will it be okay to use ArrayList then? Is there a better way to do that or am I on the right track this time?

well, as Campbell said, in the first case the Integer doesn't really help you, since you could just store the actors in a List and use its indexes. In the second it might have a use if you have a list of movie names - but even if you do, why don't you just have a
Map<String, ArrayList<String>>
ie;
Map<Movie name, List of actors in the movie>

Winston
 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so to clairfy. I should just have a Map<String, ArrayList<String>>
ie;
Map<Movie name, List of actors in the movie> and this will be for the appearances? But how would this match up with the appearances file that is full of numbers:
first number the movie and the list of numbers after the actors in that movie?

I was going to bring in the files actors.txt, movies.txt (which are the string names of the actors and movies) and save them in their own hashmap.

I'm sorry I'm not following you all...


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayumi yamada wrote:first number the movie and the list of numbers after the actors in that movie?

What we're basically saying is: Why have all these numbers?

Are you likely to know that "Gone With The Wind" is movie #12? Or that "Pitt, Brad" is Actor #473?

Java objects are identifiable by their reference, and classes can define "equality" any way they like via their equals() and hashCode() methods; so if you have classes called Actor and Movie (rather than just Strings), you can create a List<Actor> (or possibly even better, a Set<Actor>, because a List can hold duplicates).

And isn't a:
Map<Movie, Set<Actor>>
much more obvious than a:
Map<String, List<String>>?

Furthermore, if you had a Movie class, you could put its cast in the object if you want, which might eliminate the need for a Map altogether.
You might want to have a look at the StringsAreBad page for more details.

However, if you are required to use Strings for this exercise, it's worth remembering that they are only equal if their contents are equal, so "Pitt, Brad" will never be the same as "Jolie, Angelina" and "Gone With The Wind" will not be the same as "Matrix 3". On the other hand, "Matrix 3" will be the same as another String containing "Matrix 3".

Also: There are two extremely useful classes called LinkedHashSet and LinkedHashMap that "sort of" emulate Lists.
Basically, they keep their elements (or entries) in the order they were added, so if you print them out with toString() that's how they'll appear. However, they are much faster than a List if you want to find something or check if it's already there.

HIH

Winston
 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for the explanation! I have come a long way since I've lasted posted.
I am now working on the Graph. I have 3 hashmaps. I want to be able to read the appearances file and use that to create edges between the actors that are in the same movie. With the movie being the index/ weight and the values being the arraylist of actors. BUT I want to have an arraylist of arraylist, the second arraylist will keep the edges of the actors that are seen in movies together...if that makes sense? This is how my tutor explained it to me...but I am still a bit confused on how to go about it.




I can read in the appearances and have it say :


But I don't really know how about putting this in the graph...So a little guidance would be extremely appreciated!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayumi yamada wrote:This is how my tutor explained it to me...but I am still a bit confused on how to go about it.

Me too, and I suspect you may be overthinking this.

I presume you have at least this:
1. A file containing Actors.
2. A file containing Movies.
3. Information about what Actors are in each Movie.

and I presume that for your program you will need:
a. A Set or List of Actors.
b. A Set or List of Movies.
c. A Map that holds information about what Actors are in each Movie (CastList?).
d. A Map that holds information about what Movies each Actor has been in (Appearance?).

(Let's leave the "6 degrees" game part out of it for now)

The problem you have is that the information you have been given for #3 above appears to be in the form of symbolic keys (your "numbers"), where each number corresponds to a line in one of your original files.

That does NOT mean, however, that you have to store it that way in your program, and personally, I'd advise against it (but you need to ask your tutor what s/he expects). You might need to store your Actors and Movies in lists to begin with, just in order to decipher the information in #3; but personally, I'd:
  • Store them as lists of Actor and Movie objects, rather than just Strings.
  • Get them moved from your initial Lists into Sets as soon as you possibly can (you can probably do this at the same time as you store the Lists). For one thing, this will highlight any duplications in your input (I'm pretty sure SAG requires all actors to have a unqiue name), but additionally, a HashSet (or LinkedHashSet) will be much faster for doing the searches you'll probably need later on.
  • Use Actor and Movie objects as the keys to your Maps, NOT their numbers.

  • It would be useful if you gave us samples (and just samples; not the entire input) of ALL your input files, in the exact form that you receive them, because I notice that some of your actors have "(I)" after their name, but I have no idea what it means.

    Once you get the information in a form you can use, then you can deal with how to play the game; but for right now, concentrate on storing the information properly.

    HIH

    Winston
     
    mayumi yamada
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much for y'all help. I got finished with the project! but now I have another and a question. Dealing with hashmaps and heaps.

    I want to read in this file, this is just a small piece.







    and I want the first value to be the key and the second to be the value. But I do not know how to do this with hashmap.
    I read in the numbers and got them in the key place and just left the value slot with an " " (space). Should I use something else...since I do not know how to do this with hashmap?
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Do you know how to read from a text file and get the number followed by the remainder of the line? It looks like something a Scanner would do nicely.

    Are you sure those numbers on the left are unique?
     
    mayumi yamada
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually no, I don't know how to do that...I was told a Buffered reader was the best option, which is why I was working with that. Do you mind giving an example of what you're talking about?
    And yes, the numbers on the left are suppose to be unique, though my professor did say to " You can assume the data files are formatted correctly. But give error messages as appropriate (inserting a donor already in the heap, changing a donor that doesn't exist, ...)"
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Buffered readers are less easy to use than Scanners. If you look in the Java Tutorials, you can find it under “Scanning and Formatting” under Scanning. Look through the different methods of Scanner; there should be something which reads a number and something to read the remainder of the line. Start by going through the file and printing something like
    number=723 name=Light, Matt


    Beware: Using Scanner like that requires the input file to have a particular format. If you have a line which is not in the form 1234 Ritchie, Campbell you will suffer exceptions or incorrect result. If you are using a Map, your numbers must be unique, otherwise an entry will vanish.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can find information from the Map which will tell you whether you have a duplicate, and then produce error messages, but get the reading from the file working first. Then consider the Map later.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually you appear to have got your BufferedReader working, so maybe you can go on to the Map and forget the Scanner. What I would have used would be something likeThere is an option you can put in the String#split method which will stop splitting, so you can split into exactly two parts.
     
    mayumi yamada
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    okay, thank you. I am reading up on it now!
     
    mayumi yamada
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    okay...now I am having problem creating my heap(priority queue).



    okay I understand that for each of the letters read them in and they will call the different parts of the heap that's needed.
    Example I and i means that ID first number and priority is the second number, so it needs to be added to the heap.

    but I don't know how to go about this...every example I see is with Arraylists. and I don't know how to go about having the Arraylists.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!