1. 程式人生 > >guava學校,集合專題

guava學校,集合專題

mage eat 次數 mpi ble world count info create

lists

//JDK
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

//Guava
List<String> list = Lists.newArrayList("a", "b", "c", "d");

Multiset(統計次數的set

Multiset<String> multiset = HashMultiset.create();
// 統計每次單詞出現的次數
for (String word : words) {
multiset.add(word);
}
// 輸出單詞的輸出次數
for (String word : multiset.elementSet()) {
multiset.count(word);
}

Multimap(多個value的map

Multimap<Integer, People> multimap = ArrayListMultimap.create();
// 統計同一年紀的 People 對象
for (People people : peopleList) {
multimap.put(people.getAge(), people);
}

// 輸出統計結果
for (Integer key : multimap.keySet()) {
List<People> peoples = Lists.newArrayList(multimap.get(key));
System.out.println(peoples);
}

//JDK
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

//Guava
List<String> list = Lists.newArrayList("a", "b", "c", "d");

Multiset(統計次數的set

Multiset<String> multiset = HashMultiset.create();
// 統計每次單詞出現的次數
for (String word : words) {
multiset.add(word);
}
// 輸出單詞的輸出次數
for (String word : multiset.elementSet()) {
multiset.count(word);
}

Multimap(多個value的map

Multimap<Integer, People> multimap = ArrayListMultimap.create();
// 統計同一年紀的 People 對象
for (People people : peopleList) {
multimap.put(people.getAge(), people);
}

// 輸出統計結果
for (Integer key : multimap.keySet()) {
List<People> peoples = Lists.newArrayList(multimap.get(key));
System.out.println(peoples);
}

Bimap(雙向map

BiMap<Integer, String> empIDNameMap = HashBiMap.create();

empIDNameMap.put(new Integer(101), "Mahesh");
empIDNameMap.put(new Integer(102), "Sohan");
empIDNameMap.put(new Integer(103), "Ramesh");

//Emp Id of Employee "Mahesh"
System.out.println(empIDNameMap.inverse().get("Mahesh"));


Table(像表一樣有(a,b)坐標,代替Map<key,Map<key,world>>

技術分享圖片

guava學校,集合專題