0

I wondering if there is anyway that if I know one part of an ArryList that I can find out the other. The problem I'm running into is my limited knowledge of java.

I have the list set up as:

spotsList = new ArrayList<HashMap<String, String>>();

The activity goes through and adds every spot(from a server) to the list in a forloop with PID and NAME as:

HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);
spotsList.add(map);

Now is there a way I can get the name if I know the PID?

Thank you in advance,

Tyler

0

2 Answers 2

3

It seems that you expect the PIDs to be unique (given a PID you can find the corresponding name). So instead of a list of maps you should probably just use one map:

Map<String, String> map = new HashMap<String, String>();
for (Spot s : spots) map.put(s.id, s.name);

Retrieving a name from the pid is then simply a matter of:

String name = map.get(pid);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks :D But when I posted the problem I simplified it by only using name and PID but 6 others that I would need to add to it. Would I go about that the same way?
The more specific your question, the best answers you will get! If all fields are linked to the pid, you could create a class that wraps the 6 fields and use a Map<String, YourClass> instead.
I tried using the class from below but got the error when I changed map<spot> to map<String,spot> even though it was saying it needed more variables there.
1

You should probably use a domain class instead of a HashMap to hold that data. If you did that you could easily search a collection for a particular value.

public class Spot {
    private final String pid;
    private final String name;

    public Spot(String pid, String name) {
        this.pid = pid;
        this.name = name;
    }

    // getters 
}

You'll want to add override equals() and hashCode() also.

And then use a map instead of a list:

Map<String,Spot> spots = new HashMap<String,Spot>();
spots.put(pid, new Spot(pid, name));

Then to find one:

Spot spot = spots.get(pid);

7 Comments

Thank you, but it underlines map and hashmap saying incorrect number of arguments. Also, how would I go about adding an override for equals() and hashcode()?
Make sure you import them first. For equals() and hashCode() check out the builders from Apache Commons: commons.apache.org/proper/commons-lang/apidocs/org/apache/… and commons.apache.org/proper/commons-lang/apidocs/org/apache/…
The Map and HashMap. If you did and you're still getting errors, what jdk version are you using?
Both are imported. How do I check jdk? I'm just using eclipse and don't remember changing the jdk so I guess the default
I also tried using map<string, spot> instead of just <spot> but that did not work either
|

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.