利用apache Collections和google guava对list和map进行过滤和排序
http://www.jiancool.com/article/50723356810/
在工作中,我们常常需要对List或者Map等集合结果过滤或者排序,这里我通过使用apachecommons和googleguava中的方法,简单实现了一下,方便以后使用。
例子中使用的是apachecommons3.1和google-guava14,最新版本是18
apache项目下载地址:http://projects.apache.org/indexes/quick.html
apacheCollectoins下载地址:http://commons.apache.org/proper/commons-collections/
apache代码如下:
对List中的对象按不同属性排序,请看此
package com.umapp.test; public class UserGuava { private String name; private int age; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; UserGuava other = (UserGuava) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public UserGuava(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package com.umapp.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.collections.Predicate; import org.apache.commons.collections.iterators.FilterIterator; import org.apache.commons.collections.iterators.UniqueFilterIterator; public class TestApache { /** * @param args */ public static void main(String[] args) { List<UserGuava> list = new ArrayList<UserGuava>(); UserGuava user = new UserGuava(); user.setAge(1); user.setName("Apple"); list.add(user); user = new UserGuava(); user.setAge(2); user.setName("Facebook"); list.add(user); user = new UserGuava(); user.setAge(3); user.setName("Twitter"); list.add(user); user = new UserGuava(); user.setAge(4); user.setName("Youtube"); list.add(user); user = new UserGuava(); user.setAge(5); user.setName("Telerik"); list.add(user); user = new UserGuava(); user.setAge(6); user.setName("Google"); list.add(user); user = new UserGuava(); user.setAge(6); user.setName("Google"); list.add(user); Predicate predicate=new Predicate(){ @Override public boolean evaluate(Object arg0) { UserGuava u = (UserGuava)arg0; return u.getAge()>2; } }; Iterator<?> iterator=new FilterIterator(list.iterator(),predicate); while(iterator.hasNext()){ UserGuava u = (UserGuava)iterator.next(); System.out.println(u.getName()+","+u.getAge()); } System.out.println("-------------------你懂的------------------------"); Iterator iterator1=new UniqueFilterIterator(list.iterator()); while(iterator1.hasNext()){ UserGuava u = (UserGuava)iterator1.next(); System.out.println(u.getName()+","+u.getAge()); } } }
打印结果:
Twitter,3
Youtube,4
Telerik,5
Google,6
Google,6
-------------------你懂的------------------------
Apple,1
Facebook,2
Twitter,3
Youtube,4
Telerik,5
Google,6
googleguava代码:
package com.umapp.test; import java.util.Comparator; public class NameComparator implements Comparator<UserGuava> { @Override public int compare(UserGuava user, UserGuava user1) { return user.getName().compareTo(user1.getName()); } }
package com.umapp.test; import java.util.Comparator; public class AgeComparator implements Comparator<UserGuava> { @Override public int compare(UserGuava user, UserGuava user1) { return user.getAge() > user1.getAge() ? 1 : (user.getAge() == user1.getAge() ? 0 : -1); } }
package com.umapp.test; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Ordering; public class TestGuava { public static void main(String[] args) { Map<String, UserGuava> map = Maps.newHashMap(); List<UserGuava> list = Lists.newArrayList(); UserGuava user = new UserGuava(); user.setAge(1); user.setName("JetBrains"); map.put("1", user); list.add(user); user = new UserGuava(); user.setAge(2); user.setName("Apple"); list.add(user); map.put("2", user); user = new UserGuava(); user.setAge(3); user.setName("Telerik"); list.add(user); map.put("3", user); user = new UserGuava(); user.setAge(4); user.setName("Google"); list.add(user); map.put("4", user); user = new UserGuava(); user.setAge(5); user.setName("Twitter"); list.add(user); map.put("5", user); user = new UserGuava(); user.setAge(2); user.setName("Facebook"); list.add(user); map.put("2", user); user = new UserGuava(); user.setAge(1); user.setName("Youtube"); map.put("1", user); list.add(user); Predicate<UserGuava> preFilter = new Predicate<UserGuava>(){ @Override public boolean apply(UserGuava user) { return user.getAge()>2; } }; for (Entry<String, UserGuava> e : map.entrySet()) { if (preFilter.apply(e.getValue())) { UserGuava u= (UserGuava)e.getValue(); System.out.println("Map-UserGuava="+u.getName()+","+u.getAge()); } } System.out.println("-------------------你懂的------------------------"); for (UserGuava u:list) { if (preFilter.apply(u)) { System.out.println("List-UserGuava="+u.getName()+","+u.getAge()); } } System.out.println("-------------------你懂的------------------------"); Predicate<UserGuava> p1 = new Predicate<UserGuava> () { @Override public boolean apply(UserGuava user) { if (user == null) { return false; } if (Objects.equals(user.getAge(), 1)) { return false; } return true; } }; Predicate<UserGuava> p2 = new Predicate<UserGuava> () { @Override public boolean apply(UserGuava user) { if (user == null) { return false; } if (Objects.equals(user.getAge(), 2)) { return false; } return true; } }; Predicate<UserGuava> unionPredicate = Predicates.and(p1, p2); //两种处理方式 // List<UserGuava> newUserList = Lists.newArrayList(Iterators.filter(list.iterator(), unionPredicate)); Collection<UserGuava> newUserList =(Collection<UserGuava>) Collections2.filter(list, unionPredicate); for (UserGuava u:newUserList) { System.out.println("List-IN-UserGuava="+u.getName()+","+u.getAge()); } System.out.println("-------------------你懂的------------------------"); //先按年龄,再按名称排序 Comparator<UserGuava> userComparator = Ordering .from(new AgeComparator()).compound(new NameComparator()); Collections.sort(list, userComparator); for (UserGuava u:list) { System.out.println("List-Sort-UserGuava="+u.getName()+","+u.getAge()); } } }
打印结果:
Map-UserGuava=Telerik,3
Map-UserGuava=Twitter,5
Map-UserGuava=Google,4
-------------------你懂的------------------------
List-UserGuava=Telerik,3
List-UserGuava=Google,4
List-UserGuava=Twitter,5
-------------------你懂的------------------------
List-IN-UserGuava=Telerik,3
List-IN-UserGuava=Google,4
List-IN-UserGuava=Twitter,5
-------------------你懂的------------------------
List-Sort-UserGuava=JetBrains,1
List-Sort-UserGuava=Youtube,1
List-Sort-UserGuava=Apple,2
List-Sort-UserGuava=Facebook,2
List-Sort-UserGuava=Telerik,3
List-Sort-UserGuava=Google,4
List-Sort-UserGuava=Twitter,5