0

Need some help: I am not familiar with Hashmaps. How would I compare List and arrays with a hash map? The List & array holds Integers.

The Map:

 Map<Integer, String> profMap = new HashMap<Integer, String>() {};
        while(profs.next()){
            Integer key= profs.getInt("profid");
            String name= profs.getString("profname");
            profMap.put(key,name);
        }
/* profs comes from my sql statement and pulls values from those columns*/

Array:

String [] profArray = request.getParameterValues("professor");

List:

List list= new ArrayList();

I get lost in the documentation and all the other examples I have seen. Hoping someone can help me out on here

6
  • Your map has two Collections, the keyset and the valueset. Which are you trying to compare to an array or Collection? Commented Mar 6, 2012 at 4:24
  • What is your question? Do you need to know what the difference between a List and a Map is? Basically a List works like an array it simply holds a set of values in it. A Map holds key value pairs so each value has a key associated with it. Commented Mar 6, 2012 at 4:24
  • 1
    what's profs? Know sscce. Commented Mar 6, 2012 at 4:24
  • I need come compare the keys but then store those values.. i really just don't know how to format the foreach Commented Mar 6, 2012 at 4:25
  • I still don't know what it is you want to do! Please focus on that and edit your post. Commented Mar 6, 2012 at 4:28

2 Answers 2

1

Based on your comment you want to compare the keys and store the values associated with keys that match.

You can do this.

foreach(Integer key in profMap.keyset()) {
    if(/* Check the key against your criteria */) {
        list.add(ProfMap.get(key));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Basically, the HashMap is a list of items which are indexed by a unique key. With an ArrayList or normal array you index by a position in the list, such as list[0] or list.get(0). With the HashMap, there isn't such ordering, and so you can't reference the items by an index. When you add an item to a HashMap, you provide a key, which can be any Object, and the value, which can be any Object. When you want to get the value back out of the map, you retrieve it by using the key value.

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.