本文整理了Java中java.util.LinkedList
类的一些代码示例,展示了LinkedList
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LinkedList
类的具体详情如下:
包路径:java.util.LinkedList
类名称:LinkedList
[英]Doubly-linked list implementation of the List and Dequeinterfaces. Implements all optional list operations, and permits all elements (including null).
All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
Note that this implementation is not synchronized. If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections#synchronizedListmethod. This is best done at creation time, to prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new LinkedList(...));
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
This class is a member of the Java Collections Framework.
[中]双链表实现了链表和DEQUE接口。实现所有可选的列表操作,并允许所有元素(包括null)。
所有操作的执行都与双链接列表的预期一样。索引到列表中的操作将从开始或结束遍历列表,以更接近指定索引的为准。
请注意,此实现是不同步的。如果多个线程同时访问一个链表,并且至少有一个线程在结构上修改链表,则必须在外部对其进行同步。(结构修改是添加或删除一个或多个图元的任何操作;仅设置图元的值不是结构修改。)这通常通过在自然封装列表的某个对象上进行同步来实现。如果不存在此类对象,则应使用Collections#synchronizedListmethod“包装”列表。最好在创建时执行此操作,以防止意外不同步地访问列表:
List list = Collections.synchronizedList(new LinkedList(...));
这个类的迭代器和listIterator方法返回的迭代器是快速失效的:如果在迭代器创建后的任何时候,列表在结构上被修改,除了通过迭代器自己的remove或add方法,迭代器将抛出ConcurrentModificationException。因此,在面对并发修改时,迭代器会快速、干净地失败,而不是在将来的不确定时间冒着任意、不确定行为的风险。
请注意,无法保证迭代器的快速失效行为,因为一般来说,在存在非同步并发修改的情况下,不可能做出任何硬保证。快速失败迭代器会尽最大努力抛出ConcurrentModificationException。因此,编写依赖于此异常的正确性的程序是错误的:迭代器的快速失败行为应该只用于检测bug。
此类是Java Collections Framework的成员。
canonical example by Tabnine
private void mappingWordsLength(List<String> wordsList) {
Map<Integer, Set<String>> mapping = new HashMap<>();
for (String word : wordsList) {
mapping.computeIfAbsent(word.length(), HashSet::new).add(word);
}
List<Integer> lengths = new LinkedList<>(mapping.keySet());
Collections.sort(lengths);
lengths.forEach(n -> System.out.println(mapping.get(n).size() + " words with " + n + " chars"));
}
代码示例来源:origin: google/guava
@Override
public List<Object> get() {
return new LinkedList<>();
}
}
代码示例来源:origin: org.mockito/mockito-core
public static <T> LinkedList<T> filter(Collection<T> collection, Filter<T> filter) {
LinkedList<T> filtered = new LinkedList<T>();
for (T t : collection) {
if (!filter.isOut(t)) {
filtered.add(t);
}
}
return filtered;
}
代码示例来源:origin: log4j/log4j
public boolean isEmpty() {
boolean empty = false;
if (_categoryElements.size() == 0) {
empty = true;
}
return (empty);
}
代码示例来源:origin: org.mockito/mockito-core
public void removeLast() {
//TODO: add specific test for synchronization of this block (it is tested by InvocationContainerImplTest at the moment)
synchronized (invocations) {
if (! invocations.isEmpty()) {
invocations.removeLast();
}
}
}
代码示例来源:origin: org.mockito/mockito-core
public Object answer(InvocationOnMock invocation) throws Throwable {
if (elements.size() == 1)
return elements.get(0);
else
return elements.poll();
}
}
代码示例来源:origin: log4j/log4j
/**
* Ensures that the MRU list will have a MaxSize.
*/
protected void setMaxSize(int maxSize) {
if (maxSize < _mruFileList.size()) {
for (int i = 0; i < _mruFileList.size() - maxSize; i++) {
_mruFileList.removeLast();
}
}
_maxSize = maxSize;
}
//--------------------------------------------------------------------------
代码示例来源:origin: org.mockito/mockito-core
public void add(Invocation invocation) {
synchronized (invocations) {
invocations.add(invocation);
}
}
代码示例来源:origin: log4j/log4j
/**
* Moves the the index to the top of the MRU List
*
* @param index The index to be first in the mru list
*/
public void moveToTop(int index) {
_mruFileList.add(0, _mruFileList.remove(index));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Create a new buffer and store it in the LinkedList
* <p>Adds a new buffer that can store at least {@code minCapacity} bytes.
*/
private void addBuffer(int minCapacity) {
if (this.buffers.peekLast() != null) {
this.alreadyBufferedSize += this.index;
this.index = 0;
}
if (this.nextBlockSize < minCapacity) {
this.nextBlockSize = nextPowerOf2(minCapacity);
}
this.buffers.add(new byte[this.nextBlockSize]);
this.nextBlockSize *= 2; // block size doubles each time
}
代码示例来源:origin: log4j/log4j
/**
* Adds an object to the mru.
*/
protected void setMRU(Object o) {
int index = _mruFileList.indexOf(o);
if (index == -1) {
_mruFileList.add(0, o);
setMaxSize(_maxSize);
} else {
moveToTop(index);
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Returns a tree-style representation of the current {@code ParseState}.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < this.state.size(); x++) {
if (x > 0) {
sb.append('\n');
for (int y = 0; y < x; y++) {
sb.append(TAB);
}
sb.append("-> ");
}
sb.append(this.state.get(x));
}
return sb.toString();
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public Reference<K, V> pollForPurge() {
if (TestWeakConcurrentCache.this.disableTestHooks) {
return super.pollForPurge();
}
return TestWeakConcurrentCache.this.queue.isEmpty() ? null : TestWeakConcurrentCache.this.queue.removeFirst();
}
};
代码示例来源:origin: spring-projects/spring-framework
@SuppressWarnings("unchecked")
@Nullable
private <T> T getLastBuilder(Class<T> builderClass) {
if (!this.builders.isEmpty()) {
PathComponentBuilder last = this.builders.getLast();
if (builderClass.isInstance(last)) {
return (T) last;
}
}
return null;
}
代码示例来源:origin: hankcs/HanLP
/**
* 获取最长的后缀
* @param word
* @return
*/
public int getLongestSuffixLength(String word)
{
word = reverse(word);
LinkedList<Map.Entry<String, Integer>> suffixList = trie.commonPrefixSearchWithValue(word);
if (suffixList.size() == 0) return 0;
return suffixList.getLast().getValue();
}
代码示例来源:origin: org.mockito/mockito-core
public boolean isEmpty() {
synchronized (invocations) {
return invocations.isEmpty();
}
}
代码示例来源:origin: google/guava
private void runNext() {
tasks.removeFirst().run();
}
}
代码示例来源:origin: hankcs/HanLP
@Override
public boolean add(Pipe<M, M> pipe)
{
return pipeList.add(pipe);
}
代码示例来源:origin: org.mockito/mockito-core
public static Invocation findPreviousVerifiedInOrder(List<Invocation> invocations, InOrderContext context) {
LinkedList<Invocation> verifiedOnly = ListUtil.filter(invocations, new RemoveUnverifiedInOrder(context));
if (verifiedOnly.isEmpty()) {
return null;
} else {
return verifiedOnly.getLast();
}
}
代码示例来源:origin: hankcs/HanLP
@Override
public boolean isEmpty()
{
return pipeList.isEmpty();
}
内容来源于网络,如有侵权,请联系作者删除!