Java:存储和访问对象列表的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19037262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Best way to store and access a list of objects
提问by user2605421
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.
我的问题是是否有任何其他方法可以编写它以提高性能。
采纳答案by Eric Stein
Use a Map whose keys are the names and whose values are the people.
使用一个 Map,它的键是名字,值是人。
回答by Daniel Imms
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).
正如 Eric 提到的,您应该使用 a HashMap
,这样做的原因是因为您可以非常快速地(平均)查找和添加数据。
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.
这是一个如何使用HashMap
usingPerson.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);
}
}
回答by Mike Clark
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.
HashMap 区分大小写。如果您想要不区分大小写的查找,您可以使用 TreeMap。我的例子表明同名的人(不区分大小写)会互相覆盖。
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;
}
}
}