user2605421
user2605421

Reputation: 187

Java: Best way to store and access a list of objects

What i want to do is store some instances of my class on a list and get a specific instance from that list.

This is an example of a custom class

public class Person
{
    private String name;
    //Several unrelevant fields here

    public Person(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    //Several unrelevant methods here
}

And this is the code i'm currently using to get one of the instances on the list, that is on the main class.

public class Main
{
    private List<Person> people = new ArrayList<Person>();
    //More unrelevant fields here

    public Person getPerson(String name)
    {
        for (Person p : people)
            if (p.getName().equalsIgnoreCase(name))
                return p;
        return null;
    }
    //More unrelevant methods here
}

My question is if there's any other way to write this to increase the performance.

Upvotes: 8

Views: 16998

Answers (3)

Mike Clark
Mike Clark

Reputation: 10136

HashMap is case sensitive. If you wanted case-insensitive lookups, you could use a TreeMap. My example demonstrates that people with the same name (case insensitively) overwrite each other.

import java.util.Map;
import java.util.TreeMap;

public class SoMain {
    Map<String, Person> nameToPersonMap = 
            new TreeMap<String, Person>(String.CASE_INSENSITIVE_ORDER);

    public static void main(String[] args) {
        new SoMain().run(args);
    }

    private void run(String[] args) {
        addPerson(new Person("Jim McDonald", 1));
        addPerson(new Person("Jim Mcdonald", 2));
        addPerson(new Person("John Smith", 3));

        System.out.println("Number of people: " 
                    + nameToPersonMap.entrySet().size());
        System.out.println("Jim McDonald id: " 
                    + getPerson("Jim McDonald").getPersonId());
        System.out.println("John Smith id: " 
                    + getPerson("john smith").getPersonId());
    }

    private void addPerson(Person p) {
        nameToPersonMap.put(p.getName(), p);
    }

    private Person getPerson(String name) {
        return nameToPersonMap.get(name);
    }

    public static class Person {
        private String name;
        private int personId;

        public Person(String name, int personId) {
            this.name = name;
            this.personId = personId;
        }

        public int getPersonId() {
            return personId;
        }

        public String getName() {
            return name;
        }
    }
}

Upvotes: 3

Daniel Imms
Daniel Imms

Reputation: 50269

As Eric mentions, you should use a HashMap, the reasoning for this is because you can look up and add data to one very quickly (on average).

Here is a code example of how to use HashMap using Person.name as the key, this assumes that there is never a person with the same name.

public class Main
{
    private HashMap<String, Person> people = new HashMap<String, Person>();

    public void addPerson(Person person)
    {
        people.put(person.getName(), person);
    }

    public Person getPerson(String name)
    {
        // get returns null when not found
        return people.get(name);
    }
}

Upvotes: 1

Eric Stein
Eric Stein

Reputation: 13682

Use a Map whose keys are the names and whose values are the people.

Upvotes: 15

Related Questions