利用apache Collections和google guava對list和map進行過濾和排序
[url]http://www.jiancool.com/article/50723356810/[/url]
在工作中,我們常常需要對List或者Map等集合結果過濾或者排序,這裡我通過使用apache commons和google guava中的方法,簡單實現了一下,方便以後使用。
例子中使用的是apache commons3.1和google-guava14,最新版本是18
apache專案下載地址:http://projects.apache.org/indexes/quick.html
apache Collectoins下載地址:http://commons.apache.org/proper/commons-collections/
[color=red][b] apache程式碼如下:[/b][/color]
對List中的物件按不同屬性排序,請看此
列印結果:
Twitter,3
Youtube,4
Telerik,5
Google,6
Google,6
-------------------你懂的------------------------
Apple,1
Facebook,2
Twitter,3
Youtube,4
Telerik,5
Google,6
[color=red][b] google guava程式碼: [/b][/color]
列印結果:
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
在工作中,我們常常需要對List或者Map等集合結果過濾或者排序,這裡我通過使用apache commons和google guava中的方法,簡單實現了一下,方便以後使用。
例子中使用的是apache commons3.1和google-guava14,最新版本是18
apache專案下載地址:http://projects.apache.org/indexes/quick.html
apache Collectoins下載地址:http://commons.apache.org/proper/commons-collections/
[color=red][b] apache程式碼如下:[/b][/color]
對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
[color=red][b] google guava程式碼: [/b][/color]
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
相關文章
- 使用Collections對list的內容進行排序排序
- Map按照key和value進行排序排序
- PHP 對資料進行驗證和過濾PHP
- map和set對vector排序排序
- 將Json載入到.NET物件並對結果進行過濾和排序JSON物件排序
- C# 中使用Linq和Lambda表示式對List進行排序C#排序
- Django(67)drf搜尋過濾和排序過濾Django排序
- SpringBoot整合Jpa對資料進行排序、分頁、條件查詢和過濾Spring Boot排序
- Java Map和List常見操作Java
- google guava的BiMap:雙向MapGoGuava
- Google guava工具類的介紹和使用GoGuava
- 使用Java和Google Guava簡化開發JavaGoGuava
- 如何在dhtmlxGantt網格中對任務進行排序和重新排序HTML排序
- Collections工具類,可以使用collections工具類對程式碼中的list進行分組
- arrilist陣列和collections儲存學生排序陣列排序
- c++map 查詢元素和list查詢元素速度對比C++
- List排序用Collections.sort and Comparator排序
- 對list集合中的物件進行排序(轉載)物件排序
- Java使用Collections對中文字元進行首字母排序Java字元排序
- 監聽器,過濾器,攔截器的執行過程和對比過濾器
- TreeMap排序及使用Collections對List排序(主鍵可能重複的處理方法)排序
- java中list、set和map 的區別(轉)Java
- List排序Collections.sort 重寫compare排序
- Linux中ls對檔案進行按大小排序和按時間排序Linux排序
- Guava的布隆過濾器Guava過濾器
- Isotope-jQuery神奇的分類過濾和排序佈局外掛jQuery排序
- 利用X.509證書對XML進行加密和簽名XML加密
- 使用Apache Flink和Apache Ignit進行資料流分析Apache
- guava之Joiner 和 SplitterGuava
- Guava 之 Joiner 和 SplitterGuava
- 監聽器和過濾器過濾器
- Filter不過濾CSS和JSFilterCSSJS
- 利用uWSGI和nginx進行伺服器部署Nginx伺服器
- lambda匿名函式sorted排序函式filter過濾函式map對映函式函式排序Filter
- 在Windows和UNIX下利用PHP和LDAP進行身份驗證(轉)WindowsPHPLDA
- PHP對錶單提交特殊字元的過濾和處理PHP字元
- Java List集合去重、過濾、分組、獲取資料、求最值、合併、排序、跳資料和遍歷Java排序
- Android 濾鏡效果和顏色通道過濾Android