guava API整理
1,大綱
讓我們來(lái)熟悉瓜娃,并體驗(yàn)下它的一些API,分成如下幾個(gè)部分:
- Introduction
- Guava Collection API
- Guava Basic?Utilities
- IO API
- Cache API
2,為神馬選擇瓜娃?
- 瓜娃是java API蛋糕上的冰激凌(精華)
- 高效設(shè)計(jì)良好的API.
- 被google的開(kāi)發(fā)者設(shè)計(jì),實(shí)現(xiàn)和使用。
- 遵循高效的java這本書(shū)的好的語(yǔ)法實(shí)踐。
- 使代碼更刻度,簡(jiǎn)潔,簡(jiǎn)單。
- 使用java 1.5的特性,
- 流行的API,動(dòng)態(tài)的開(kāi)發(fā)
- 它提供了大量相關(guān)的應(yīng)用類(lèi),集合,多線(xiàn)程,比較,字符串,輸入輸出,緩存,網(wǎng)絡(luò),原生類(lèi)型,數(shù)學(xué),反射等等
- 百分百的單元測(cè)試,被很多的項(xiàng)目使用,幫助開(kāi)發(fā)者專(zhuān)注業(yè)務(wù)邏輯而不是寫(xiě)java應(yīng)用類(lèi)
- 節(jié)省時(shí)間,資源,提高生產(chǎn)力
- 我的目的是為基本的java特征提供開(kāi)源代碼的支持,而不是自己再寫(xiě)一個(gè)
- Apache Common庫(kù)-Apache是一個(gè)很好的成熟的庫(kù),但是不支持泛型,Apache對(duì)早起的java版本很有用,(1.5之前的)
- java7,java8 最新的java支持一些guava的API
maven:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
3,集合API的使用
3.1簡(jiǎn)化工作
可以簡(jiǎn)化集合的創(chuàng)建和初始化;
| 類(lèi)別 | 原來(lái)的寫(xiě)法 | guava的寫(xiě)法 |
| 集合創(chuàng)建 | Map<String, Map<String, String>> map = new HashMap<String, Map<String,String>>(); List<List<Map<String, String>>> list = new ArrayList<List<Map<String,String>>>(); | Map<String, Map<String, String>> map = Maps.newHashMap(); List<List<Map<String, String>>> list = Lists.newArrayList(); //1,簡(jiǎn)化集合的創(chuàng)建 |
| 集合初始化 | ? Set<String> set = new HashSet<String>(); set.add("one"); set.add("two"); set.add("three"); | ? 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"); //2,簡(jiǎn)化集合的初始化 new Person(2, 1, "a", "46546", 1, 20)); new Person(2,1,"a","46546",1,20)); |
?
3.2 不變性
很大一部分是google集合提供了不變性,不變對(duì)比可變:
- ?數(shù)據(jù)不可改變
- 線(xiàn)程安全
- 不需要同步邏輯
- ?可以被自由的共享
- 容易設(shè)計(jì)和實(shí)現(xiàn)
- ?內(nèi)存和時(shí)間高效
不變對(duì)比不可修改
google的不變被確保真正不可改變,而不可修改實(shí)際上還是可以修改數(shù)據(jù);如下所示:
?
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]
如何創(chuàng)建不可變的集合:
ImmutableSet<Integer> numbers =?ImmutableSet.of(10, 20, 30, 40, 50);
使用copyOf方法
ImmutableSet<Integer> another =?mmutableSet.copyOf(numbers);
使用Builder方法
ImmutableSet<Integer> numbers2 =?ImmutableSet.<Integer>builder().addAll(numbers) .add(60) .add(70).add(80).build();
//3,創(chuàng)建不可變的集合
ImmutableList<Person> personImmutableList=
ImmutableList.of(new Person(1, 1, "a", "46546", 1, 20), new Person(2, 1, "a", "46546", 1, 20));
ImmutableSet<Person> personImmutableSet=ImmutableSet.copyOf(personSet2);
ImmutableMap<String,Person> personImmutableMap=ImmutableMap.<String,Person>builder()
.put("hell",new Person(1,1,"a","46546",1,20)).putAll(personMap2) .build();
?
3.3 新的集合類(lèi)型
The Guava API provides very useful new collection types that work very nicely with existing java collections.
guava API 提供了有用的新的集合類(lèi)型,協(xié)同已經(jīng)存在的java集合工作的很好。
分別是?MultiMap, MultiSet, Table, BiMap, ClassToInstanceMap
| 種類(lèi) | 寫(xiě)的例子 |
| MultiMap | 一種key可以重復(fù)的map,子類(lèi)有ListMultimap和SetMultimap,對(duì)應(yīng)的通過(guò)key分別得到list和set
customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 30)); for(Person person:customersByType.get("abc")) |
| MultiSet | ? 不是集合,可以增加重復(fù)的元素,并且可以統(tǒng)計(jì)出重復(fù)元素的個(gè)數(shù),例子如下: private static void testMulitiSet() { System.out.println( multiSet.count(30)); // 2 |
| Table | ? 相當(dāng)于有兩個(gè)key的map,不多解釋 private static void testTable() { //1,得到行集合 } |
| BiMap | 是一個(gè)一一映射,可以通過(guò)key得到value,也可以通過(guò)value得到key;? private static void testBitMap() { biMap.put(1,"hello"); int value= biMap.inverse().get("my"); } |
| ClassToInstanceMap | ? 有的時(shí)候,你的map的key并不是一種類(lèi)型,他們是很多類(lèi)型,你想通過(guò)映射他們得到這種類(lèi)型,guava提供了ClassToInstanceMap滿(mǎn)足了這個(gè)目的。 除了繼承自Map接口,ClassToInstaceMap提供了方法?T getInstance(Class<T>)?和?T putInstance(Class<T>, T),消除了強(qiáng)制類(lèi)型轉(zhuǎn)換。 該類(lèi)有一個(gè)簡(jiǎn)單類(lèi)型的參數(shù),通常稱(chēng)為B,代表了map控制的上層綁定,例如: ClassToInstanceMap<Number> numberDefaults = MutableClassToInstanceMap.create(); numberDefaults.putInstance(Integer.class, Integer.valueOf(0)); 從技術(shù)上來(lái)說(shuō),ClassToInstanceMap<B>?實(shí)現(xiàn)了Map<Class<? extends B>, B>,或者說(shuō),這是一個(gè)從B的子類(lèi)到B對(duì)象的映射,這可能使得ClassToInstanceMap的泛型輕度混亂,但是只要記住B總是Map的上層綁定類(lèi)型,通常來(lái)說(shuō)B只是一個(gè)對(duì)象。 guava提供了有用的實(shí)現(xiàn),?MutableClassToInstanceMap?和?ImmutableClassToInstanceMap. 重點(diǎn):像其他的Map<Class,Object>,ClassToInstanceMap?含有的原生類(lèi)型的項(xiàng)目,一個(gè)原生類(lèi)型和他的相應(yīng)的包裝類(lèi)可以映射到不同的值; private static void testClass() { Person person= new Person(1,20,"abc","46464",1,100); classToInstanceMap.putInstance(Person.class,person);
Person person1=classToInstanceMap.getInstance(Person.class); } |
?
3.4 謂詞和篩選
謂詞(Predicate)是用來(lái)篩選集合的;
謂詞是一個(gè)簡(jiǎn)單的接口,只有一個(gè)方法返回布爾值,但是他是一個(gè)很令人驚訝的集合方法,當(dāng)你結(jié)合collections2.filter方法使用,這個(gè)篩選方法返回原來(lái)的集合中滿(mǎn)足這個(gè)謂詞接口的元素;
舉個(gè)例子來(lái)說(shuō):篩選出集合中的女人
public static void main(String[] args)
{
Optional<ImmutableMultiset<Person>> optional=Optional.fromNullable(testPredict());
if(optional.isPresent())
{
for(Person p:optional.get())
{
System.out.println("女人:"+p);
}
}
System.out.println(optional.isPresent());
}
public static ImmutableMultiset<Person> testPredict()
{
List<Person> personList=Lists.newArrayList(new Person(1, 1, "a", "46546", 1, 20),
new Person(2, 1, "ab", "46546", 0, 30),
new Person(3, 1, "abc", "46546", 0, 25),
new Person(4, 1, "aef", "46546", 1, 50),
new Person(5, 1, "ade", "46546",0, 27),
new Person(6, 1, "acc", "46546", 1, 29),
new Person(7, 1, "add", "46546",0, 33));
return ImmutableMultiset.copyOf(Collections2.filter(personList,new Predicate<Person>() {
@Override
public boolean apply( Person input) {
return input.getSex()==0;
}
}));
}
Predicates含有一些內(nèi)置的篩選方法,比如說(shuō) in ,and ,not等,根據(jù)實(shí)際情況選擇使用。
3.5 功能和轉(zhuǎn)換
轉(zhuǎn)換一個(gè)集合為另外一個(gè)集合;
實(shí)例如下:
public static void main(String[] args)
{
Optional<ImmutableMultiset<String>> optional=Optional.fromNullable(testTransform());
if(optional.isPresent())
{
for(String p:optional.get())
{
System.out.println("名字:"+p);
}
}
System.out.println(optional.isPresent());
}
public static ImmutableMultiset<String> testTransform()
{
List<Person> personList=Lists.newArrayList(new Person(1, 1, "a", "46546", 1, 20),
new Person(2, 1, "ab", "46546", 0, 30),
new Person(3, 1, "abc", "46546", 0, 25),
new Person(4, 1, "aef", "46546", 1, 50),
new Person(5, 1, "ade", "46546",0, 27),
new Person(6, 1, "acc", "46546", 1, 29),
new Person(7, 1, "add", "46546",0, 33));
return ImmutableMultiset.copyOf(Lists.transform(personList,new Function<Person, String>() {
@Override
public String apply( Person input) {
return input.getName();
}
}));
}
3.6 排序
?是guava一份非常靈活的比較類(lèi),可以被用來(lái)操作,擴(kuò)展,當(dāng)作比較器,排序提供了集合排序的很多控制;
實(shí)例如下:
Lists.newArrayList(30, 20, 60, 80, 10);
Ordering.natural().sortedCopy(numbers); //10,20,30,60,80
Ordering.natural().reverse().sortedCopy(numbers); //80,60,30,20,10
Ordering.natural().min(numbers); //10
Ordering.natural().max(numbers); //80
Lists.newArrayList(30, 20, 60, 80, null, 10);
Ordering.natural().nullsLast().sortedCopy(numbers); //10, 20,30,60,80,null
Ordering.natural().nullsFirst().sortedCopy(numbers); //null,10,20,30,60,80
?
public static void testOrdering()
{
List<Person> personList=Lists.newArrayList(
new Person(3, 1, "abc", "46546", 0, 25),
new Person(2, 1, "ab", "46546", 0, 30),
new Person(5, 1, "ade", "46546",0, 27),
new Person(1, 1, "a", "46546", 1, 20),
new Person(6, 1, "acc", "46546", 1, 29),
new Person(4, 1, "aef", "46546", 1, 50),
new Person(7, 1, "add", "46546",0, 33)
);
Ordering<Person> byAge=new Ordering<Person>() {
@Override
public int compare( Person left, Person right) {
return right.getAge()-left.getAge();
}
};
for(Person p: byAge.immutableSortedCopy(personList))
{
System.out.println(p);
}
}
guava在結(jié)合部分的API使用記錄完畢,希望對(duì)廣大屌絲有所幫助。?
?
no pays,no gains!轉(zhuǎn)載于:https://www.cnblogs.com/dzcWeb/p/6999694.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的guava API整理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 美团优选怎么加盟
- 下一篇: OpenGL中的Shader