『壹』 【JAVA】【懸賞100】 有兩個 list 我想把他 改為一個map 或者其他的
//得到keys
List<String>keys=newArrayList<String>();
//得到values
List<Integer>values=newArrayList<Integer>();
//Map
Map<String,Integer>rs=newLinkedHashMap<String,Integer>();
//確保以最小的list集合長度為rs的長度,防止NullPointerException異常
intkeyLen=(keys!=null)?keys.size():0;
intvalLen=(values!=null)?values.size():0;
intlen=keyLen
if(len>valLen)
Len=valLen;
//保存到Map中,for循環遍歷效率較高
for(inti=0;i<len;i++)
rs.put(keys.get(i),values.get(i));
希望能幫助你
『貳』 java6 支持使用guava 工具類庫 將list轉換為map嗎
假設有某個類如下
class Movie {
private Integer rank;
private String description;
public Movie(Integer rank, String description) {
super();
this.rank = rank;
this.description = description;
}
public Integer getRank() {
return rank;
『叄』 java里有沒有專門判斷List里有重復的數據
沒有專門判斷的,可以自己寫一段代碼
『肆』 如何再java鍾使用guava
以前這么用:
Java代碼
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
現在Guava這么用:
Java代碼
ImmutableList<String> of = ImmutableList.of("a", "b", "c", "d");
ImmutableMap<String,String> map = ImmutableMap.of("key1", "value1", "key2", "value2");
文本文件讀取現在Guava這么用
Java代碼
File file = new File(getClass().getResource("/test.txt").getFile());
List<String> lines = null;
try {
lines = Files.readLines(file, Charsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
『伍』 guava Splitter 與java 內置的string的split 方法有什麼區別
java內置的會忽略空內容,guava Splitter不會忽略,也可以自由選擇的忽略。
guava Splitter 可以通過splitToList方法直接將結果轉成List。
『陸』 使用Guava將3個List<Map>集合根據某個欄位合並成一個List<Map>集合
通過 java8 的流式編程,實現還是比較簡單的。
代碼比較多,已上傳附件。
『柒』 int數組轉化成List<Integer>有沒有更簡便的方法
1、建立Test的java測試類並寫出main方法用來測試。
『捌』 java.util.stream map和flatmap的區別
Stream是元素的集合,這點讓Stream看起來用些類似Iterator;
可以支持順序和並行的對原Stream進行匯聚的操作;
大家可以把Stream當成一個高級版本的Iterator。原始版本的Iterator,用戶只能一個一個的遍歷元素並對其執行某些操作;高級版本的Stream,用戶只要給出需要對其包含的元素執行什麼操作,比如「過濾掉長度大於10的字元串」、「獲取每個字元串的首字母」等,具體這些操作如何應用到每個元素上,就給Stream就好了!(這個秘籍,一般人我不告訴他:))大家看完這些可能對Stream還沒有一個直觀的認識,莫急,咱們來段代碼。
//Lists是Guava中的一個工具類
List<Integer> nums = Lists.newArrayList(1,null,3,4,null,6);
nums.stream().filter(num -> num != null).count();
上面這段代碼是獲取一個List中,元素不為null的個數。這段代碼雖然很簡短,但是卻是一個很好的入門級別的例子來體現如何使用Stream,正所謂「麻雀雖小五臟俱全」。我們現在開始深入解刨這個例子,完成以後你可能可以基本掌握Stream的用法!
1.1 剖析Stream通用語法
圖片就是對於Stream例子的一個解析,可以很清楚的看見:原本一條語句被三種顏色的框分割成了三個部分。紅色框中的語句是一個Stream的生命開始的地方,負責創建一個Stream實例;綠色框中的語句是賦予Stream靈魂的地方,把一個Stream轉換成另外一個Stream,紅框的語句生成的是一個包含所有nums變數的Stream,進過綠框的filter方法以後,重新生成了一個過濾掉原nums列表所有null以後的Stream;藍色框中的語句是豐收的地方,把Stream的裡麵包含的內容按照某種演算法來匯聚成一個值,例子中是獲取Stream中包含的元素個數。如果這樣解析以後,還不理解,那就只能動用「核武器」–圖形化,一圖抵千言!
『玖』 判斷一個list集合是否為空,用isEmpty 還是 null的問題
isEmpty() 和(list.size() == 0)都是判斷List內容是否為空。
null判斷是判斷有沒有對list集合分配內存空間,而不是list裡面內容是否為空。
比如,new一個user對象,判斷user的list內容是否為空,出現異常。這是因為,使用isEmpty()和size()的前提是,list是一個空集合,而不是null,否則會拋異常。
所有在判斷集合不為空的時候常採用:
if(list!=null && !list.isEmpty()){
//不為空的情況
}else{
//為空的情況
}
(9)guavalist過濾擴展閱讀:
List集合的常用方法:
1、voidadd(intindex,Eelement)
在指定位置插入元素,後面的元素都往後移一個元素。
2、booleanaddAll(intindex,Collection<? extendsE>c)
在指定的位置中插入c集合全部的元素,如果集合發生改變,則返回true,否則返回false。
意思就是當插入的集合c沒有元素,那麼就返回false,如果集合c有元素,插入成功,那麼就返回true。
3、Eget(intindex)
返回list集合中指定索引位置的元素。
4、intindexOf(Objecto)
返回list集合中第一次出現o對象的索引位置,如果list集合中沒有o對象,那麼就返回-1。
5、Eremove(intindex)
刪除指定索引的對象。
6、Eset(intindex,Eelement)
在索引為index位置的元素更改為element元素。
『拾』 Google guava和Apache commons哪個好
Guava 的 FAQ 部分有專門解答:
Why did Google build all this, when it could have tried to improve the Apache Commons Collections instead?
The Apache Commons Collections very clearly did not meet our needs. It does not use generics, which is a problem for us as we hate to get compilation warnings from our code. It has also been in a "holding pattern" for a long time. We could see that it would require a pretty major investment from us to fix it up until we were happy to use it, and in the meantime, our own library was already growing organically.
An important difference between the Apache library and ours is that our collections very faithfully adhere to the contracts specified by the JDK interfaces they implement. If you review the Apache documentation, you'll find countless examples of violations. They deserve credit for pointing these out so clearly, but still, deviating from standard collection behavior is risky! You must be careful what you do with such a collection; bugs are always just waiting to happen.
Our collections are fully generified and never violate their contracts (with isolated exceptions, where JDK implementations have set a strong precedent for acceptable violations). This means you can pass one of our collections to any method that expects a Collection and feel pretty confident that things will work exactly as they should.
簡單地說:
Apache Commons Collections 3.x 不支持泛型,Guava 支持
Guava 實現了 JDK 的標准介面,而 Apache Commons Collections 3.x 有很多違反標準的地方
Apache Commons Collections 4.x 的發行注記如下:
Majorchangessince3.2.1
(varargs,Iterable)
Removeddeprecatedclasses/
.util.Queue
/Get(seealsopackagesplitmap)
從 4.x 開始,Apache Commons Collections 開始使用 JDK 5 的特性(包括泛型),此外也去除、添加了很多內容
各有千秋, 我主要使用Apache Commons ,輔助使用 Google guava
但是細節方法上還是有區別的, 比如
GoogleGuava Splitter 對比 Apache StringUtils
apache commons的StringUtils提供的常用功能介紹,但是google的guava也提供了一些字元串處理的常見功能,所以,將對兩者的字元串分割函數做一次比較詳細的對比(結果比較surprise)。
區別
首先看基本的使用方法:
//ApacheStringUtils...
String[]tokens1=StringUtils.split("one,two,three",',');
//GoogleGuavasplitter...
Iteratable<String>tokens2=Splitter.on(','),split("one,two,three");
google提供的方法更加的面向對象一點,因為它要先創建一個Splitter對象,然後使用它來分割字元串,而apache的方法則有點函數式編程的味道,它的方法都是靜態的。
這里我更加傾向於採用google的splitter,因為這個對象是可以重用的,且可以在其上附加更多的功能,比如trim,去掉空的元素等,一切都很簡單。
SplitterniceCommaSplitter=Splitter.on(',').omitEmptyString().trimResults();
niceCommaSplitter.split("one,,two,three");//"one","two","three"
niceCommaSplitter.split("four,five");//"four","five"
看起來有點用,還有其他區別么?
另外一個需要注意的地方就是Splitter返回的是Iteratable<String>,而StringUtils.split返回的是一個String數組。
大部分使用分隔符的情況是我們需要對字元串按照分隔符進行遍歷處理,僅此而已。
下面就是常用的代碼性能對比的例子:
finalStringnumberList="One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
longstart=System.currentTimeMillis();
for(inti=0;i<1000000;i++){
StringUtils.split(numberList,',');
}
System.out.println(System.currentTimeMillis()-start);
start=System.currentTimeMillis();
for(inti=0;i<1000000;i++){
Splitter.on(',').split(numberList);
}
System.out.println(System.currentTimeMillis()-start);
代碼很簡單,就是都對同一個字元串進行100萬次的分隔操作,看看時間上的區別,結果如下:
983
165
很明顯,guava的速度快很多,這個程序如果運行在每天處理大量字元串的服務中,那麼性能差異更加明顯。我想其中的原因是Splitter返回的是Iterable<String>,而StringUtils.split返回的是一個String[],需要創建新的String對象,導致耗時增加。
如果我們對Splitter對象緩存,那麼速度提高更多:
start=System.currentTimeMillis();
Splitters=Splitter.on(',');
for(inti=0;i<1000000;i++){
s.split(numberList);
}
System.out.println(System.currentTimeMillis()-start);
結果為12,神奇吧,呵呵