Jump to content

Build Theme!
  •  
  • Infected?

WE'RE SURE THAT YOU'LL LOVE US!

Hey there! :wub: Looks like you're enjoying the discussion, but you're not signed up for an account. When you create an account, we remember exactly what you've read, so you always come right back where you left off. You also get notifications, here and via email, whenever new posts are made. You can like posts to share the love. :D Join 91865 other members! Anybody can ask, anybody can answer. Consistently helpful members may be invited to become staff. Here's how it works. Virus cleanup? Start here -> Malware Removal Forum.

Try What the Tech -- It's free!


Photo

Adding ArrayList to HashMap


  • Please log in to reply
5 replies to this topic

#1 Overbooked

Overbooked

    Silver Member

  • Authentic Member
  • PipPipPip
  • 347 posts

Posted 01 October 2010 - 01:46 PM

Hi all,

Just wanted to see if I can shorten the code to add a new ArrayList to a HashMap. This is what I currently have:

public void putStudent(Student newStudent) {
  ArrayList<Student> studentToAdd = new ArrayList<Student>();
  studentToAdd.add(newStudent);
  mySchools.put(newStudent.getName(), studentToAdd);
}

mySchool is a HashMap<String, ArrayList<Student>>. What I currently have works but I wanted to see if there's a more efficient way to accomplish this. Any ideas will be appreciated.
Thank you, Overbooked

    Advertisements

Register to Remove


#2 jpshortstuff

jpshortstuff

    Teacher Emeritus

  • Authentic Member
  • PipPipPipPipPipPip
  • 5,710 posts

Posted 02 October 2010 - 11:32 AM

Haven't compiled it, but something like this may do the trick:
import java.util.Arrays;

...

mySchools.put(newStudent.getName(), new ArrayList(Arrays.asList(studentToAdd));

However, I can't quite see why you want a whole ArrayList as your key value, since it looks like that method will mean every entry in your map will contain a key which is an ArrayList of a single student. Is there any reason why your HashMap can't be of type HashMap<string, Student>?

Proud Graduate of the TC/WTT Classroom

At weekends (GMT) I may not be able to reply promptly due to various commitments. Please be patient and I will respond as soon as I can.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Need help remembering those important computer maintenance tasks? Let SCars do it for you.

Posted Image

#3 Overbooked

Overbooked

    Silver Member

  • Authentic Member
  • PipPipPip
  • 347 posts

Posted 03 October 2010 - 02:46 AM

Hi jpshortstuff, Thanks for taking a look at this. The example was not really that well thought of. The example I had in mind was for a HashMap(String, ArrayList) but got confused in making up a scenario :smack: Looking back at my original example it does look silly. Maybe a better one would have been String studentName (key) and ArrayList studentGrades (value). The thing that was confusing me the most is if it was possible to create a new ArrayList, invoke the ArrayList.add() to put whatever in the ArrayList then add it into the HashMap in one go.
Thank you, Overbooked

#4 jpshortstuff

jpshortstuff

    Teacher Emeritus

  • Authentic Member
  • PipPipPipPipPipPip
  • 5,710 posts

Posted 03 October 2010 - 05:07 AM

Hi Overbooked,

OK, I wasn't aware that was just an example. Reading the example code I gave you again, I notice I gave you wrong impression. Here's what I meant to write:
import java.util.Arrays

public void putStudent(Student newStudent) {
  mySchools.put(newStudent.getName(), new ArrayList<Student>(Arrays.asList(newStudent)));
}

That will allow you to create the ArrayList and add the entry in one go. The asList() can take any number of values representing the entries you want to add to your list.

However, although this is one line of code, I expect it is probably less efficient than your current method, since it involves creating a List, then converting it to an ArrayList.

Hope that helps.

Proud Graduate of the TC/WTT Classroom

At weekends (GMT) I may not be able to reply promptly due to various commitments. Please be patient and I will respond as soon as I can.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Need help remembering those important computer maintenance tasks? Let SCars do it for you.

Posted Image

#5 Overbooked

Overbooked

    Silver Member

  • Authentic Member
  • PipPipPip
  • 347 posts

Posted 03 October 2010 - 07:44 PM

It looks like Arrays.asList() solves the puzzle. I was trying stuff like new ArrayList<Student>().add(newStudent) and couldn't get it to work :lol: Thanks jpshortstuff :notworthy:
Thank you, Overbooked

#6 jpshortstuff

jpshortstuff

    Teacher Emeritus

  • Authentic Member
  • PipPipPipPipPipPip
  • 5,710 posts

Posted 04 October 2010 - 04:32 AM

No problem :thumbup:

Proud Graduate of the TC/WTT Classroom

At weekends (GMT) I may not be able to reply promptly due to various commitments. Please be patient and I will respond as soon as I can.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Need help remembering those important computer maintenance tasks? Let SCars do it for you.

Posted Image

Related Topics



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users