這篇文章主要介紹了java中集合的代碼示例,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、平順網(wǎng)絡(luò)推廣、小程序開(kāi)發(fā)、平順網(wǎng)絡(luò)營(yíng)銷(xiāo)、平順企業(yè)策劃、平順品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供平順建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.js-pz168.com
List,Set,Map都是接口,前兩個(gè)繼承Collection接口,Map為獨(dú)立接口
Set的實(shí)現(xiàn)由HashSet,LinkedHashSet,TreeSet
List下有ArrayList,Vector,LinkedList
Map下有Hashtable,LinkedHashMap,HashMap,TreeMap
Collection還有Queue接口,實(shí)現(xiàn)有PriorityQueue
ArrayList、LinkedList、HashMap中都有字段叫modCount,字段用途:
/**
The number of times this list has been <i>structurally modified</i>.
Structural modifications are those that change the size of the
list, or otherwise perturb it in such a fashion that iterations in
progress may yield incorrect results.
<p>This field is used by the iterator and list iterator implementation
returned by the {@code iterator} and {@code listIterator} methods.
If the value of this field changes unexpectedly, the iterator (or list
iterator) will throw a {@code ConcurrentModificationException} in
response to the {@code next}, {@code remove}, {@code previous},
{@code set} or {@code add} operations. This provides
<i>fail-fast</i> behavior, rather than non-deterministic behavior in
the face of concurrent modification during iteration.
<p><b>Use of this field by subclasses is optional.</b> If a subclass
wishes to provide fail-fast iterators (and list iterators), then it
merely has to increment this field in its {@code add(int, E)} and
{@code remove(int)} methods (and any other methods that it overrides
that result in structural modifications to the list). A single call to
{@code add(int, E)} or {@code remove(int)} must add no more than
one to this field, or the iterators (and list iterators) will throw
bogus {@code ConcurrentModificationExceptions}. If an implementation
does not wish to provide fail-fast iterators, this field may be
ignored.
*/
*
此列表在結(jié)構(gòu)上被修改的次數(shù)。
結(jié)構(gòu)修改是指改變
列出,或者以這樣一種方式干擾它
進(jìn)度可能會(huì)產(chǎn)生不正確的結(jié)果。
<p>此字段由迭代器和列表迭代器實(shí)現(xiàn)使用
由@code迭代器和@code lis迭代器方法返回。
如果此字段的值意外更改,則迭代器(或列表
迭代器)將在
響應(yīng)@code next,@code remove,@code previous,
@code set或@code add操作。這提供了
<i>fail fast</i>behavior,than non determinatic behavior in
迭代期間并發(fā)修改的面。
<p><b>按子類(lèi)使用此字段是可選的。<b>如果是子類(lèi)
希望提供fail-fast迭代器(和list迭代器),然后
只需在其@code add(int,e)中增加該字段,
@code remove(int)方法(以及它重寫(xiě)的任何其他方法)
這將導(dǎo)致對(duì)列表進(jìn)行結(jié)構(gòu)修改)。打個(gè)電話(huà)給
@code add(int,e)或@code remove(int)必須添加不超過(guò)
一個(gè)到這個(gè)字段,或者迭代器(和列表迭代器)將拋出
偽造{@code ConcurrentModificationExceptions}。如果一個(gè)實(shí)現(xiàn)
不希望提供fail-fast迭代器,此字段可能是
已忽略。
/
List<String> list=new ArrayList();
list.add("config");
list.add("config");
list.add("config1");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.forEach(s -> {
if("config1".equals(s)){
list.remove(s);
}
});
java.util.ConcurrentModificationException
at java.util.ArrayList.forEach(ArrayList.java:1260)
at com.mufeng.test.base.dataStructure.TestList.test1(TestList.java:31)
//HashSet
//巧妙利用HashMap中key實(shí)現(xiàn)
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
// 仿真的值與Map中對(duì)象保持一致
private static final Object PRESENT = new Object();
public HashSet() {
map = new HashMap<>();
}
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
//LinkedHashSet
//繼承HashSet
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
//初始容量為16
public LinkedHashSet() {
super(16, .75f, true);
}
//LinkedHashMap
//繼承HashMap 好多方法都可以用HashMap中的
public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after;
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}
/** * The head (eldest) of the doubly linked list. */ //單鏈表 首位 transient LinkedHashMap.Entry<K,V> head; /** * The tail (youngest) of the doubly linked list. */ //末位 transient LinkedHashMap.Entry<K,V> tail; /** * The iteration ordering method for this linked hash map: <tt>true</tt> * for access-order, <tt>false</tt> for insertion-order. * * @serial */ final boolean accessOrder;
//TreeSet
//具體實(shí)現(xiàn)為T(mén)reeMap
private transient NavigableMap<E,Object> m;
// Dummy value to associate with an Object in the backing Map
//仿真值
private static final Object PRESENT = new Object();
public TreeSet() {
this(new TreeMap<E,Object>());
}
//利用TreeMap的key
public boolean add(E e) {
return m.put(e, PRESENT)==null;
}
public boolean remove(Object o) {
return m.remove(o)==PRESENT;
}感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“java中集合的代碼示例”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
文章題目:java中集合的代碼示例
瀏覽路徑:http://www.js-pz168.com/article28/pojojp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、商城網(wǎng)站、電子商務(wù)、云服務(wù)器、標(biāo)簽優(yōu)化、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)