0

I'm pretty new to Java so sorry if this is super basic. Basically I have some data in a array list and it works fine but in a later part of my program I need to read the list and it takes a long time because the list becomes very large. So I thought I could take certain parts of the array list(thats needed later) into a hash map that I can extract that section of the array list later from(hopefully that makes sense).

I first create the array list with all the data I want(I have a loop and call each data thats created in a single loop data_cache_local_loop):

HashMap hashmap_of_values = new HashMap(); //hashmap of arraylist
for (int i = 0; i<100; i++) {
....program logic and more loops
Set uniqie_T_cache = new HashSet(T_cache_local_loop); //make values unique
hashmap_of_values.put(i, new ArrayList(uniqie_T_cache));
}

Then later on when I need the data I extract it like this:

List t = new ArrayList();
t.add(hashmap_of_values.get(i));

But my program fails and I'm not sure exactly why. When I print out the size of hashmap_of_values I get the number of i's I'm expecting but when I print out the number of T it only shows one(when I run the program it says T_cache_local_loop has hundreds of items).

Can anyone suggest to me what I'm doing wrong? I'm not 100% but I think I maybe looking at the array list I add as a single item but I'm unsure of what I'm doing wrong.

1
  • Honestly, one thing I've never understood is that I have the right to edit but I alone can not review/accept an edit. Doesn't make too much sense IMO, I should just do it myself... But then since an edit is pending, I can't do it myself. Commented Apr 3, 2012 at 16:16

3 Answers 3

4

You should really be using generics -- it'd help you a lot, and make these issues much more obvious -- but for the moment your problem is that that line should be

List t = (List) hashmap_of_values.get(i);

or

List t = new ArrayList((List) hashmap_of_values.get(i));

to get a copy of the list in the map. What you were doing was creating a list of lists, because hashmap_of_values.get(i) was already a list.

Sign up to request clarification or add additional context in comments.

2 Comments

Because basically the code from the OP is creating a list of lists of values instead of a list of values. (in case that wasn't clear).
ahh your right. I thought I was doing that but wasn't sure how to stop it. Didn't realize I could simply cast the list when creating the t variable. Thank you so much.
1

trying t.addAll(hashmap_of_values.get(i));

Comments

0

When you add your HashSet to your ArrayList, you're adding the entire set as a single object to the first index of the ArrayList. You're not adding each element of the HashSet to the ArrayList. Instead, you should do something like:

List newList= new ArrayList();
newList.addAll( unique_T_cache );
hashmap_of_values.put(i, unique_T_cache );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.