Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Code example for collections in java

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the java collection of code examples, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

List,Set,Map is an interface, the first two inherit Collection interface, and Map is an independent interface

Set is implemented by HashSet,LinkedHashSet,TreeSet

There is ArrayList,Vector,LinkedList under List.

There is Hashtable,LinkedHashMap,HashMap,TreeMap under Map.

Collection also has Queue interface, which is implemented with PriorityQueue.

There are fields called modCount in ArrayList, LinkedList and HashMap. The purpose of the field is:

/ * *

The number of times this list has been structurally modified.

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.

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

Fail-fast behavior, rather than non-deterministic behavior in

The face of concurrent modification during iteration.

Use of this field by subclasses is optional. 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.

, /

*

The number of times this list was structurally modified.

Structural modification refers to change

List, or interfere with it in such a way

Progress may produce incorrect results.

This field is used by iterators and list iterators

Returned by @ code iterator and @ code lis iterator methods.

If the value of this field changes unexpectedly, the iterator (or list

Iterator) will be specified in the

Response @ code next,@code remove,@code previous

@ code set or @ code add operation. This provides

Fail fastbehavior,than non determinatic behavior in

Faces that are modified concurrently during the iteration.

Using this field by subclass is optional. If it is a subclass

You want to provide fail-fast iterators (and list iterators), and then

Just add this field to its @ code add (int,e)

The @ code remove (int) method (and any other methods it overrides)

This will result in structural changes to the list. Give me a call

@ code add (int,e) or @ code remove (int) must be added no more than

One to this field, or an iterator (and list iterator) will throw a

Forge {@ code ConcurrentModificationExceptions}. If an implementation

You do not want to provide a fail-fast iterator, this field may be

Ignored.

/

List 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

/ / skillfully use key in HashMap to implement

Private transient HashMap map

/ / Dummy value to associate with an Object in the backing Map

/ / the value of the simulation is consistent with that of the object in Map

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

/ / inherit HashSet

Public class LinkedHashSet

Extends HashSet

Implements Set, Cloneable, java.io.Serializable {

/ / initial capacity is 16

Public LinkedHashSet () {

Super (16, .75f, true)

}

/ / LinkedHashMap

/ / many methods of inheriting HashMap can be used in HashMap

Public class LinkedHashMap

Extends HashMap

Implements Map

Static class Entry extends HashMap.Node {

Entry before, after

Entry (int hash, K key, V value, Node next) {

Super (hash, key, value, next)

}

}

/ * The head (eldest) of the doubly linked list. * / / the first single linked list transient LinkedHashMap.Entry head;/** * The tail (youngest) of the doubly linked list. * / / Last transient LinkedHashMap.Entry tail;/** * The iteration ordering method for this linked hash map: true * for access-order, false for insertion-order. * * @ serial * / final boolean accessOrder

/ / TreeSet

/ / the specific implementation is TreeMap

Private transient NavigableMap m

/ / Dummy value to associate with an Object in the backing Map// simulation value private static final Object PRESENT = new Object (); public TreeSet () {this (new TreeMap ());} / / using TreeMap's keypublic boolean add (E) {return m.put (e, PRESENT) = = null;} public boolean remove (Object o) {return m.remove (o) = = PRESENT } Thank you for reading this article carefully. I hope the article "Code examples collected in java" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 291

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report