I have a Person
class:
public class Person{
private String name;
//pleaese think this 'id' simply as an attribute of Person, same as e.g. age, height
private long id;
public Person(String name, long id){
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public long getId(){
return id;
}
}
Then, I have a HashMap
instance which holds several Person
s get from server:
//key is String type, it is a unique name-like string assigned to each Person
//value is a Person object.
HashMap<String, Person> personsMap = GET_PERSONS_FROM_SERVER();
Then, I have an array of person IDs:
long[] ids = new long[]{1,2,3,4,5, …}
What I need is to generate another HashMap
which only contains persons whose id is listed in the ids
array:
// Only the person whose id is listed in ids array will be in the following Map
Map<String, Person> personNeeded = … ;
How to get personNeeded
in an efficient way?