Guava的API使用

1、簡化工作,簡化集合的創建和初始化:

1)集合創建:

原來的寫法:

Map<String, Map<String, String>> map = new HashMap<String, Map<String,String>>();
List<List<Map<String, String>>> list = new ArrayList<List<Map<String,String>>>();
guava的寫法:

Map<String, Map<String, String>> map = Maps.newHashMap();
List<List<Map<String, String>>> list = Lists.newArrayList();

List<Person> personList= Lists.newLinkedList();
Set<Person> personSet= Sets.newHashSet();
Map<String,Person> personMap= Maps.newHashMap();
Integer[] intArrays= ObjectArrays.newArray(Integer.class,10);
2)集合初始化:

原來的寫法:

Set<String> set = new HashSet<String>();
set.add("one");
set.add("two");
set.add("three");
guava的寫法:

Set<String> set = Sets.newHashSet("one","two","three");
List<String> list = Lists.newArrayList("one","two","three");
Map<String, String> map = ImmutableMap.of("ON","TRUE","OFF","FALSE");

List<Person> personList2= Lists.newArrayList(new Person(1, 1, "a", "46546", 1, 20),
new Person(2, 1, "a", "46546", 1, 20));
Set<Person> personSet2= Sets.newHashSet(new Person(1,1,"a","46546",1,20),
new Person(2,1,"a","46546",1,20));
Map<String,Person> personMap2= ImmutableMap.of("hello",new Person(1,1,"a","46546",1,20),"fuck",new Person(2,1,"a","46546",1,20));

2、不變性,很大一部分是google集合提供了不變性。

不變對比可變:數據不可變、線程安全、不需要同步邏輯、可以被自由地共享、容易設計和實現、內存和時間高效。

不變對比不可修改:google的不變被確保真正不可變,而不可修改實際上還是可以修改數據的。

如下:

Set<Integer> data = new HashSet<Integer>();
data.addAll(Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80));
Set<Integer> fixedData = Collections.unmodifiableSet(data); // fixedData - [50, 70, 80, 20, 40, 10, 60, 30]
data.add(90); // fixedData - [50, 70, 80, 20, 40, 10, 90, 60, 30]
如何創建不可變的集合:

ImmutableSet<Integer> set1 = ImmutableSet.of(10, 20, 30, 40, 50);
// 使用copyOf方法
ImmutableSet<Integer> set2 = ImmutableSet.copyOf(set1);
// 使用builder方法:
ImmutableSet<Integer> set3 = ImmutableSet.<Integer>builder().addAll(set1) .add(60) .add(70).add(80).build();

ImmutableList<Integer> list1 = ImmutableSet.of(10, 20, 30, 40, 50);
// 使用copyOf方法
ImmutableList<Integer> list2 = ImmutableSet.copyOf(list1);
// 使用builder方法:
ImmutableList<Integer> set3 = ImmutableSet.<Integer>builder().addAll(list1) .add(60) .add(70).add(80).build();

ImmutableMap<Integer,Integer> map1 = ImmutableMap.of(1,10,2, 20,3, 30,4, 40,5, 50);
// 使用copyOf方法
ImmutableMap<Integer,Integer> map2 = ImmutableMap.copyOf(map1);
// 使用builder方法:
ImmutableMap<Integer,Integer> map3 = ImmutableMap.<Integer,Integer>builder().putAll(set1) .put(6,60) .put(7,70).put(8,80).build();

3、新的集合類型

guava API提供了有用的新的集合類型,協同已經存在的java集合工作得很好,分別是MultiMap、MultiSet、Table、BiMap、ClassToInstanceMap。

1)MultiMap:一種key可以重複的map,子類有ListMultimap和SetMultimap,對應地通過key分別得到list和set。

Multimap<String,Person> customersByType = ArrayListMultimap.create();
customersByType.put("abc", new Person("abc",0));
customersByType.put("abc", new Person("abc",1));
customersByType.put("abc", new Person("abc",0));
customersByType.put("abcd", new Person("abcd",4));
for(Person person:customersByType.get("abc")){
	System.out.println(person.getAge());
}
得到的結果將是0 1 0

2)MultiSet:可以增加重複的元素,並且可以統計出重複元素的個數

Multiset<Integer> multiSet = HashMultiset.create();
multiSet.add(10);
multiSet.add(30);
multiSet.add(30);
multiSet.add(40);
System.out.println( multiSet.count(30) ); // 2
System.out.println( multiSet.size() ); // 4
System.out.println( multiSet ); // [40, 10, 30 x 2]

3)Table:相當於有兩個key的map

Table<Integer,Integer,Person> PersonTable = HashBasedTable.create();
PersonTable.put(0, 0, new Person("a00",1));
PersonTable.put(0, 1, new Person("a01",12));
PersonTable.put(0, 2, new Person("a02",2));
PersonTable.put(1, 0, new Person("a10",10));
Map<Integer,Person> rowMap = PersonTable.row(1); // 得到行集合
Map<Integer,Person> colMap = PersonTable.column(0); // 得到列集合

4)BiMap:是一個一一映射,可以通過key得到value,也可以通過value得到key

BiMap<Integer,String> biMap = HashBiMap.create();
biMap.put(1, "hello");
biMap.put(2, "world");
biMap.put(3, "!!");
System.out.println(biMap);
System.out.println( biMap.inverse().get("!!") ); // 3

5)ClassToInstanceMap

ClassToInstanceMap提供了一種是用class作爲key,對應實例作爲value的途徑。它定義了T getInstance( Class<T> )和T putInstance( Class<T> T )兩個方法,這兩個方法消除了元素類型轉換的過程並保證了元素在Map中是類型安全的。

ClassToInstanceMap<Person> classToInstanceMap = MutableClassToInstanceMap.create();
classToInstanceMap.putInstance(Person.class, new Person("a1",1));
Person person1 = classToInstanceMap.getInstance(Person.class);



發佈了40 篇原創文章 · 獲贊 51 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章