Java random access list

Java RandomAccess tutorial with examples

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access.

Introduction

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance.

It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:

for (Iterator i=list.iterator(); i.hasNext(); ) i.next();

This interface is a member of the Java Collections Framework.

Example

The following code shows how to use RandomAccess from java.util.

import java.util.AbstractList; import java.util.RandomAccess; /**/*w w w . d e m o2 s . c o m*/ * @author Bernhard Bauer * */ public abstract class AbstractSequence extends AbstractListByte> implements ISequence, RandomAccess /** * */ @Override public int size() < return length(); > /** * @param sequences */ public static int totalLength(Iterableextends ISequence> sequences) < int totalLength = 0; for (ISequence s : sequences) < totalLength += s.length(); >return totalLength; > @Override public String toString() < return getIdentifier(); > >
import java.util.AbstractList; import java.util.RandomAccess; /**// w w w . d e m o 2 s .c o m * User: ed * Date: 1/16/13 * Time: 5:40 PM */ public class ArrayReferenceList extends AbstractList implements RandomAccess private final T[] array; private final int[] indices; private ArrayReferenceList(T[] array, int[] indices) < this.array = array; this.indices = indices; > @Override public T get(int i) < return array[indices[i]]; > @Override public T set(int i, T t) < return array[indices[i]] = t; > @Override public int size() < return indices.length; > public static ArrayReferenceList createFor(T[] array, int. indices) < return new ArrayReferenceList(array, indices); > public boolean validateIndices() < int max = array.length - 1; for (int index : indices) < if (index < 0 || index >max) < return false; > > return true; > >
import java.io.Serializable; import java.util.AbstractList; import java.util.List; import java.util.RandomAccess; /**// w w w . d em o 2 s . c o m * * @author loudyn * */ class TransformingRandomAccessList extends AbstractList  implements RandomAccess, Serializable final List fromList; final Functionsuper Input, ? extends Output> function; TransformingRandomAccessList(List fromList, Functionsuper Input, ? extends Output> function) < this.fromList = fromList; this.function = function; > @Override public Output get(int index) < return function.apply(fromList.get(index)); > @Override public int size() < return fromList.size(); > @Override public Output remove(int index) < return function.apply(fromList.remove(index)); > @Override public boolean isEmpty() < return fromList.isEmpty(); > @Override public void clear() < fromList.clear(); >private static final long serialVersionUID = 1L; >

  • Java Random seed The internal state associated with this pseudorandom number generator.
  • Java Random hashCode() Returns a hash code value for the object.
  • Java Random getClass() Returns the runtime class of this Object.
  • Java RandomAccess tutorial with examples
  • Java java.util ResourceBundle
  • Java ResourceBundle clearCache(ClassLoader loader)
  • Java ResourceBundle clearCache()

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Interface RandomAccess

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

The best algorithms for manipulating random access lists (such as ArrayList ) can produce quadratic behavior when applied to sequential access lists (such as LinkedList ). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance.

It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:

for (Iterator i=list.iterator(); i.hasNext(); ) i.next();

This interface is a member of the Java Collections Framework.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Interface RandomAccess

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

The best algorithms for manipulating random access lists (such as ArrayList ) can produce quadratic behavior when applied to sequential access lists (such as LinkedList ). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance.

It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:

for (Iterator i=list.iterator(); i.hasNext(); ) i.next();

This interface is a member of the Java Collections Framework.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Java random access list

Interface RandomAccess

public interface RandomAccess

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists. The best algorithms for manipulating random access lists (such as ArrayList ) can produce quadratic behavior when applied to sequential access lists (such as LinkedList ). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance. It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:

for (Iterator i=list.iterator(); i.hasNext(); ) i.next();

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

RandomAccess

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance.

It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:

for (Iterator i=list.iterator(); i.hasNext(); ) i.next();

This interface is a member of the Java Collections Framework.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2021-04-08 UTC.

Источник

Читайте также:  Callback кнопки telegram python
Оцените статью