collections
This package contains JavaScript implementations of common data structures with idiomatic interfaces.
Collections for JavaScript
that's kind off a general question (but I'm using C#), what's the best way (best practice), do you return null or empty collection for a method that has a collection as a return type ?
Source: (StackOverflow)
Using Linq on collections, what is the difference between the following lines of code?
if(!coll.Any(i => i.Value))
and
if(!coll.Exists(i => i.Value))
Update 1
When I disassemble .Exists it looks like there is no code.
Update 2
Anyone know why there is not code there for this one?
Source: (StackOverflow)
In Java there are the SortedSet
and SortedMap
interfaces. Both belong to Java's standard Collections framework and provide a sorted way to access the elements.
However, in my understanding there is no SortedList
in Java. You can use java.util.Collections.sort()
to sort a list.
Any idea why it is designed like that?
Source: (StackOverflow)
I need to create a Set
with initial values.
Set<String> h = new HashSet<String>();
h.add("a");
h.add("b");
Is there a way to do this in one command?
Source: (StackOverflow)
It's clear that a search performance of the generic HashSet<T>
class is higher than of the generic List<T>
class. Just compare the hash-based key with the linear approach in the List<T>
class.
However calculating a hash key may itself take some CPU cycles, so for a small amount of items the linear search can be a real alternative to the HashSet<T>
.
My question: where is the break-even?
To simplify the scenario (and to be fair) let's assume that the List<T>
class uses the element's Equals()
method to identify an item.
Source: (StackOverflow)
What are the reasons behind the decision to not have a fully generic get method
in the interface of java.util.Map<K, V>
.
To clarify the question, the signature of the method is
V get(Object key)
instead of
V get(K key)
and I'm wondering why (same thing for remove, containsKey, containsValue
).
Source: (StackOverflow)
This is similar to this question:
http://stackoverflow.com/questions/880581/java-convert-int-to-integer
I'm new to Java. How can i convert a List to int[] in Java? I'm confused because List.toArray() actually returns an Object[], which can be cast to nether Integer[] or int[].
Right now I'm using a loop to do so:
int[] toIntArray(List<Integer> list){
int[] ret = new int[list.size()];
for(int i = 0;i < ret.length;i++)
ret[i] = list.get(i);
return ret;
}
I'm sure there's a better way to do this.
Source: (StackOverflow)
I have an ArrayList
of Strings
, and I want to remove repeated strings from it. How can I do this?
Source: (StackOverflow)
Being somewhat new to the Java language I'm trying to familiarize myself with all the ways (or at least the non-pathological ones) that one might iterate through a list (or perhaps other collections) and the advantages or disadvantages of each.
Given a List<E> list
object, I know of the following ways to loop through all elements:
Basic for loop (of course, there're equivalent while
/ do while
loops as well)
// Not recommended (see below)!
for (int i = 0; i < list.size(); i++) {
E element = list.get(i);
// 1 - can call methods of element
// 2 - can use i to make index-based calls to methods of list
// ...
}
Note: As @amarseillan pointed out, this form is a poor choice
for iterating over List
s because the actual implementation of
the get
method may not be as efficient as when using an Iterator
.
For example, LinkedList
implementations must traverse all of
the elements preceding i to get the i-th element.
In the above example there's no way for the List
implementation to
"save its place" to make future iterations more efficient.
For an ArrayList
it doesn't really matter because the complexity/cost of get
is constant time (O(1)) whereas for a LinkedList
is it proportional to the size of the list (O(n)).
For more information about the computational complexity of the built-in Collections
implementations, check out this question.
for (E element : list) {
// 1 - can call methods of element
// ...
}
for (Iterator<E> iter = list.iterator(); iter.hasNext(); ) {
E element = iter.next();
// 1 - can call methods of element
// 2 - can use iter.remove() to remove the current element from the list
// ...
}
EDIT: Added ListIterator
for (ListIterator<E> iter = list.listIterator(); iter.hasNext(); ) {
E element = iter.next();
// 1 - can call methods of element
// 2 - can use iter.remove() to remove the current element from the list
// 3 - can use iter.add(...) to insert a new element into the list
// between element and iter->next()
// 4 - can use iter.set(...) to replace the current element
// ...
}
EDIT: Added "functional-style" solution (thanks Dave Newton)
list.map({E e => e++ } // can apply a transformation function for e
What other ways are there, if any?
I feel like this has got to be a duplicate, but I haven't been able to find what I'm looking for, so I apologize for this question potentially being redundant.
(BTW, my interest does not stem at all from a desire to optimize performance; I just want to know what forms are available to me as a developer.)
EDIT: Moved ListIterationExample.java to a suggested answer
Source: (StackOverflow)
Is there any built-in method in java which allows us to convert comma separated String to some Container (e.g Array, List or Vector)? Or do i need to write custom code for that
String CommaSeparated = "item1 , item2 , item3";
ArrayList<String> Items = //method that converts above string into list??
Source: (StackOverflow)
In Java, Is there an object that acts like a Map for storing and accessing key/value pairs, but can return an ordered list of keys and an ordered list of values, such that the key and value lists are in the same order?
So as explanation-by-code, I'm looking for something that behaves like my fictitious OrderedMap:
OrderedMap om = new OrderedMap();
om.put(0, "Zero");
om.put(7, "Seven");
Object o = om.get(7); // o is "Seven"
List keys = om.getKeys();
List values = om.getValues();
for(int i = 0; i < keys.size(); i++)
{
Object key = keys.get(i);
Object value = values.get(i);
Assert(om.get(key) == value);
}
Source: (StackOverflow)
I've always been one to simply use:
List<String> names = new ArrayList<String>();
I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code.
When should LinkedList
be used over ArrayList
and vice-versa?
Source: (StackOverflow)