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...