Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
when programming in Java I practically always, just out of habit, write something like this: public List<String> foo() { return new ArrayList<String>(); } Most of the time without even thinking about it. Now, the question is: should I always specify the interface as the return type? Or is it advisable to use the actual implementation of the interface, and if so, under what ci...
I have two LinkedList objects that are always of the same size. I want to compare them to see if they are identical in content. What are the general performance and style implications of creating a ListIterator for each list and using a while hasNext loop versus using a counter (int i) and iterating from 0 to linkedlist.size() using linkedlist.get(i) to get and compare the values? Is there a...
What is the best way to sort a collection while updating a progress bar? Currently I have code like this: for (int i = 0; i < items.size(); i++) { progressBar.setValue(i); // Uses Collections.binarySearch: CollectionUtils.insertInOrder(sortedItems, item.get(i)); } This shows progress but the progress bar slows down as the number of items in sortedItems grows larger. Does anyo...
I am looking for sample code which explains Guava ForwardingList class. Basically I am implementing a custom ArrayList class which will be used to solve this requirement mentioned in my earlier SO question. I never used Google collection before. But by just looking at the JavaDoc of ForwardingList, I think I can implement my custom class by sub classing ForwardingList.
Does anyone know of a List implementation that has a constant time get(int index) (I.e. implements RandomAccess) but doesn't have to copy the whole list when it grows as ArrayList does? I'm thinking the implementation may well be in terms of other lists e.g. public class ChunkedList<T> implements List<T>, RandomAccess { private LinkedList<ArrayList<T>> chunks; publ...
Which implementation is less "heavy": PriorityQueue or a sorted LinkedList (using a Comparator)? I want to have all the items sorted. The insertion will be very frequent and ocasionally I will have to run all the list to make some operations. Thank you!
List<T> list = new ArrayList<T>(); 1 method: for(int i = list.length - 1; i >= 0; i--) { System.out.println(list.get(i)); } 2 method: for(T t : list) { System.out.println(t); } 3 method: Iterator<T> it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); }
If I have an ArrayList<Double> dblList and a Predicate<Double> IS_EVEN I am able to remov e all even elements from dblList using: Collections2.filter(dblList, IS_EVEN).clear() if dblList however is a result of a transformation like dblList = Lists.transform(intList, TO_DOUBLE) this does not work any more as the transformed list is immutable :-) Any solution?
Why do we have tagging interface? what is the use?(sorry for the question if its too noob but i just not getting it!)
I have gone through various articles ,but i still do not know why instanceof should not used.kindlylet me know your thoughts.
I have a List<Cat> sorted by the cats' birthdays. Is there an efficient Java Collections way of finding all the cats that were born on January 24th, 1983? Or, what is a good approach in general?
Say I want to work with a linked list in java. I thought that the best way to create one is by: List list = new LinkedList(); But I noticed that this way I can only use methods on the list that are generic. I assume that the implementation is different among the different data structures. So if I want to use the specific methods for linked list, I have to create the list by: LinkedList lis...
Currently I am doing the profiling to a piece of code. During the profiling, I discovered that this very method call, Class<T>.isAssignableFrom(Class<?> cls) takes up to quite amount of the entire time. Because this is a method from reflection, it takes a lot of time compared to normal keywords or method calls. I am wondering if there are some good alternatives for this method ...
Is it slower to iterate through a list in Java like this: for (int i=0;i<list.size();i++) { .. list.get(i) } as opposed to: for (Object o: list) { ... o }
Let's say I have a class which, internally, stores a List of data: import java.util.List; public class Wrapper { private List<Integer> list; public Wrapper(List<Integer> list) { this.list = list; } public Integer get(int index) { return list.get(index); } } For the sake of this example, pretend it's a useful and necessary abstraction. Now, here's my c...
 /*
  * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.util;

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 (int i=0, n=list.size(); i < n; i++)
         list.get(i);
 
runs faster than this loop:
     for (Iterator i=list.iterator(); i.hasNext(); )
         i.next();
 

This interface is a member of the Java Collections Framework.

Since:
1.4
public interface RandomAccess {
New to GrepCode? Check out our FAQ X