不知道我是否应该搜索或排序我的散列表 [英] not sure if I should search or sort my hashmap
问题描述
您好,我有一份与他们年龄相关的人名单,我需要找到30岁以上的人,
是否有可能在hashmap中搜索? (请注意,我可能需要寻找其他年龄段的人,所以为了代码简单起见,我不想使用两个不同的列表)
总之:我的目标是找到一种方法来在HashMap中搜索具有特定值的元素
示例列表是
element1 40
element2 4
element3 66
element4 5
我想找到值大于40的值和值大于或等于66的值。
我建议你使用 编辑。示例: Hi I have a list of people with their ages, I need to find those who are more than 30 years old,
is there any possibility to search in a hashmap ? (please note that I may need to look for those in other age ranges as well so I prefer not to use two different lists for the sake of simplicity of code) In short: My goal is to find a way to search for elements with specific values in HashMap Sample list is I want to find those with values more than 40 and those with values more than or equal to 66. I'd suggest you to use NavigableMap (Implemented as TreeSet). This implementation is a quite fast - O(log(N)), versus O(N) if you implement index based on lists. Edit. Example:
这篇关于不知道我是否应该搜索或排序我的散列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
class PersonsAgeIndex {
private NavigableMap< Integer,List< Person>> ageToPersons =
新的TreeMap< Integer,List< Person>>();
public void addPerson(Person p){
List< Person> personsWithSameAge = this.ageToPersons.get(p.age);
if(personsWithSameAge == null){
personsWithSameAge = new LinkedList< Person>();
this.ageToPersons.put(p.age,personsWithSameAge);
}
personsWithSameAge.add(p);
}
public List< Person> personsWithAgeLessThan(int age){
List< Person> persons = new LinkedList< Person>();
//年龄较小者
(List< Person> tmp:this.ageToPersons.headMap(age).values()){
persons.addAll(tmp) ;
}
退货人员;
}
public List< Person> personsWithAgeInInterval(int minAge,int maxAge){
List< Person> persons = new LinkedList< Person>();
//具有年龄的人,其中:(minAge <= age <= maxAge)
for(List< Person> tmp:this.ageToPersons.subMap(minAge,true,maxAge ,true).values()){
persons.addAll(tmp);
}
退货人员;
}
}
class Person {
public final int age;
public Person(int age){
this.age = age;
}
}
element1 40
element2 4
element3 66
element4 5
class PersonsAgeIndex {
private NavigableMap<Integer, List<Person>> ageToPersons =
new TreeMap<Integer, List<Person>>();
public void addPerson( Person p ) {
List<Person> personsWithSameAge = this.ageToPersons.get( p.age );
if ( personsWithSameAge == null ) {
personsWithSameAge = new LinkedList<Person>();
this.ageToPersons.put( p.age, personsWithSameAge );
}
personsWithSameAge.add( p );
}
public List<Person> personsWithAgeLessThan( int age ) {
List<Person> persons = new LinkedList<Person>();
// persons with less age
for (List<Person> tmp : this.ageToPersons.headMap( age ).values()) {
persons.addAll( tmp );
}
return persons;
}
public List<Person> personsWithAgeInInterval( int minAge, int maxAge ) {
List<Person> persons = new LinkedList<Person>();
// persons with age, which: (minAge <= age <= maxAge)
for (List<Person> tmp : this.ageToPersons.subMap( minAge, true, maxAge, true ).values()) {
persons.addAll( tmp );
}
return persons;
}
}
class Person {
public final int age;
public Person(int age) {
this.age = age;
}
}