for (People people : list) { if(people.getAge().equals("1")){ System.out.println("对象:"+people.getName()); } }这个需要高性能,你有多少数据啊...
binarySearch(List<? extends T> list, T key, Comparator<? super T> c) 使用二分搜索法搜索指定列表,以获得指定对象。 这个问题问的,楼主这样写,要是平常就是要for(){} ,比较性能的话,就是list 、map、最后这个使用二分的貌似我没怎么用过,不过应该效率很高。仁者见仁,智者见智
if(people.getAge().equals("1")){
System.out.println("对象:"+people.getName());
}
}这个需要高性能,你有多少数据啊...
使用二分搜索法搜索指定列表,以获得指定对象。 这个问题问的,楼主这样写,要是平常就是要for(){} ,比较性能的话,就是list 、map、最后这个使用二分的貌似我没怎么用过,不过应该效率很高。仁者见仁,智者见智
要是数据很多,应该优先考虑hashmap,而不是用list来存放数据
要是数据很多,应该优先考虑hashmap,而不是用list来存放数据
性能的话,你可以实际测量下
import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.functors.EqualPredicate;
1:list 你要在里面筛选的对象集合
存放格式例如
list.add(user1);
list.add(user2);
list.add(user3);
2:tableColumnName: user 里面的属性字段: 例如 name
3: agers: name字段的属性值,也就是你要筛选出 name 的名称为 “张三” 的所有list
public static Collection checkList(List list,String tableColumnName,String agers){
List templist = new ArrayList();
EqualPredicate parameter = new EqualPredicate(agers);
BeanPredicate tableCoulmn_paramerter = new BeanPredicate(tableColumnName, parameter);
Predicate[] allPredicateArray = {tableCoulmn_paramerter };
Predicate allPredicate = PredicateUtils.allPredicate(allPredicateArray);
Collection<Tache> filteredCollection = CollectionUtils.select(list, allPredicate);
return filteredCollection;
}
给大家分享下这个用法,具体原因还是不清楚 但是能实现我的需求
知其然,不知其所以然。
再次求问如果是多条件要怎么弄呢?项目紧张暂时没有空研究
以上如果有什么错误请指教
Map<Integer,List<Peple>>,就是map中添加记录时需要你自己判断一下,该key是否以存在
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class Test {
public static void main(String[] args) throws Exception {
Map<Integer,List<People>> map=new HashMap<Integer,List<People>>();
People p1=new People(1,"2");
addPersonToMap(map,p1);
People p2=new People(3,"4");
addPersonToMap(map,p2);
People p3=new People(1,"2");
addPersonToMap(map,p3);
System.out.println(map.get(1));
}
private static boolean addPersonToMap(Map<Integer,List<People>> map,People p1){
boolean flag;
int key=p1.getAge();
if(map.containsKey(key)){
List<People> value=map.get(1);
value.add(p1);
map.put(key, value);
flag=true;
}else{
List<People> value=new ArrayList<People>();
value.add(p1);
map.put(key, value);
flag=false;
}
return flag;
}
}
class People{
int age;
String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public People() {
super();
}
public People(int age, String name) {
super();
this.age = age;
this.name = name;
}
}