日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

guava API整理

發(fā)布時間:2023/12/2 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 guava API整理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1,大綱

讓我們來熟悉瓜娃,并體驗下它的一些API,分成如下幾個部分:

  • Introduction
  • Guava Collection API
  • Guava Basic?Utilities
  • IO API
  • Cache API

2,為神馬選擇瓜娃?

  • 瓜娃是java API蛋糕上的冰激凌(精華)
  • 高效設計良好的API.
  • 被google的開發(fā)者設計,實現(xiàn)和使用。
  • 遵循高效的java這本書的好的語法實踐。
  • 使代碼更刻度,簡潔,簡單。
  • 使用java 1.5的特性,
  • 流行的API,動態(tài)的開發(fā)
  • 它提供了大量相關的應用類,集合,多線程,比較,字符串,輸入輸出,緩存,網(wǎng)絡,原生類型,數(shù)學,反射等等
  • 百分百的單元測試,被很多的項目使用,幫助開發(fā)者專注業(yè)務邏輯而不是寫java應用類
  • 節(jié)省時間,資源,提高生產(chǎn)力
  • 我的目的是為基本的java特征提供開源代碼的支持,而不是自己再寫一個
  • Apache Common庫-Apache是一個很好的成熟的庫,但是不支持泛型,Apache對早起的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簡化工作

可以簡化集合的創(chuàng)建和初始化;

類別原來的寫法guava的寫法
集合創(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,簡化集合的創(chuàng)建
List<Person> personList= Lists.newLinkedList();
Set<Person> personSet= Sets.newHashSet();
Map<String,Person> personMap= Maps.newHashMap();
Integer[] intArrays= ObjectArrays.newArray(Integer.class,10);

集合初始化?

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,簡化集合的初始化
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));

?

3.2 不變性

很大一部分是google集合提供了不變性,不變對比可變:

  • ?數(shù)據(jù)不可改變
  • 線程安全
  • 不需要同步邏輯
  • ?可以被自由的共享
  • 容易設計和實現(xiàn)
  • ?內(nèi)存和時間高效

不變對比不可修改

google的不變被確保真正不可改變,而不可修改實際上還是可以修改數(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 新的集合類型

The Guava API provides very useful new collection types that work very nicely with existing java collections.

guava API 提供了有用的新的集合類型,協(xié)同已經(jīng)存在的java集合工作的很好。

分別是?MultiMap, MultiSet, Table, BiMap, ClassToInstanceMap

種類寫的例子
MultiMap

一種key可以重復的map,子類有ListMultimap和SetMultimap,對應的通過key分別得到list和set


Multimap<String, Person> customersByType =ArrayListMultimap.create();customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 20));

customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 30));
customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 40));
customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 50));
customersByType.put("abcd", new Person(1, 1, "a", "46546", 1, 50));
customersByType.put("abcde", new Person(1, 1, "a", "46546", 1, 50));

for(Person person:customersByType.get("abc"))
{
System.out.println(person.getAge());
}

MultiSet

?

不是集合,可以增加重復的元素,并且可以統(tǒng)計出重復元素的個數(shù),例子如下:

private static void testMulitiSet() {
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
}

Table

?

相當于有兩個key的map,不多解釋

private static void testTable() {
Table<Integer,Integer,Person> personTable=HashBasedTable.create();
personTable.put(1,20,new Person(1, 1, "a", "46546", 1, 20));
personTable.put(0,30,new Person(2, 1, "ab", "46546", 0, 30));
personTable.put(0,25,new Person(3, 1, "abc", "46546", 0, 25));
personTable.put(1,50,new Person(4, 1, "aef", "46546", 1, 50));
personTable.put(0,27,new Person(5, 1, "ade", "46546",0, 27));
personTable.put(1,29,new Person(6, 1, "acc", "46546", 1, 29));
personTable.put(0,33,new Person(7, 1, "add", "46546",0, 33));
personTable.put(1,66,new Person(8, 1, "afadsf", "46546", 1, 66));

//1,得到行集合
Map<Integer,Person> rowMap= personTable.row(0);
int maxAge= Collections.max(rowMap.keySet());

}

BiMap是一個一一映射,可以通過key得到value,也可以通過value得到key;?

private static void testBitMap() {
//雙向map
BiMap<Integer,String> biMap=HashBiMap.create();

biMap.put(1,"hello");
biMap.put(2,"helloa");
biMap.put(3,"world");
biMap.put(4,"worldb");
biMap.put(5,"my");
biMap.put(6,"myc");

int value= biMap.inverse().get("my");
System.out.println("my --"+value);

}

ClassToInstanceMap? 有的時候,你的map的key并不是一種類型,他們是很多類型,你想通過映射他們得到這種類型,guava提供了ClassToInstanceMap滿足了這個目的。 除了繼承自Map接口,ClassToInstaceMap提供了方法?T getInstance(Class<T>)?和?T putInstance(Class<T>, T),消除了強制類型轉換。 該類有一個簡單類型的參數(shù),通常稱為B,代表了map控制的上層綁定,例如: ClassToInstanceMap<Number> numberDefaults = MutableClassToInstanceMap.create();
numberDefaults.putInstance(Integer.class, Integer.valueOf(0)); 從技術上來說,ClassToInstanceMap<B>?實現(xiàn)了Map<Class<? extends B>, B>,或者說,這是一個從B的子類到B對象的映射,這可能使得ClassToInstanceMap的泛型輕度混亂,但是只要記住B總是Map的上層綁定類型,通常來說B只是一個對象。 guava提供了有用的實現(xiàn),?MutableClassToInstanceMap?和?ImmutableClassToInstanceMap. 重點:像其他的Map<Class,Object>,ClassToInstanceMap?含有的原生類型的項目,一個原生類型和他的相應的包裝類可以映射到不同的值;

private static void testClass() {
ClassToInstanceMap<Person> classToInstanceMap =MutableClassToInstanceMap.create();

Person person= new Person(1,20,"abc","46464",1,100);

classToInstanceMap.putInstance(Person.class,person);


// System.out.println("string:"+classToInstanceMap.getInstance(String.class));
// System.out.println("integer:" + classToInstanceMap.getInstance(Integer.class));

Person person1=classToInstanceMap.getInstance(Person.class);

}

?

3.4 謂詞和篩選

謂詞(Predicate)是用來篩選集合的;

謂詞是一個簡單的接口,只有一個方法返回布爾值,但是他是一個很令人驚訝的集合方法,當你結合collections2.filter方法使用,這個篩選方法返回原來的集合中滿足這個謂詞接口的元素;

舉個例子來說:篩選出集合中的女人

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)置的篩選方法,比如說 in ,and ,not等,根據(jù)實際情況選擇使用。

3.5 功能和轉換

轉換一個集合為另外一個集合;

實例如下:

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一份非常靈活的比較類,可以被用來操作,擴展,當作比較器,排序提供了集合排序的很多控制;

實例如下:

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在結合部分的API使用記錄完畢,希望對廣大屌絲有所幫助。?

?

no pays,no gains!

轉載于:https://www.cnblogs.com/dzcWeb/p/6999694.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的guava API整理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。