Questions tagged [collections]
112 questions
140
votes
7 answers
Why doesn't Java 8 include immutable collections?
The Java team has done a ton of great work removing barriers to functional programming in Java 8. In particular, the changes to the java.util Collections do a great job of chaining transformations into very fast streamed operations. Considering…
GlenPeterson
- 14,950
54
votes
9 answers
Is immutability very worthwhile when there is no concurrency?
It seems that thread-safety is always/often mentioned as the main benefit of using immutable types and especially collections.
I have a situation where I would like to make sure that a method will not modify a dictionary of strings (which are…
Den
- 4,877
48
votes
6 answers
Good or bad practice to mask Java collections with meaningful class names?
Lately I've been in the habit of "masking" Java collections with human-friendly class names. Some simple examples:
// Facade class that makes code more readable and understandable.
public class WidgetCache extends Map {
}
Or:
// If…
herpylderp
- 2,057
43
votes
8 answers
Why does java.util.ArrayList allow to add null?
I wonder why java.util.ArrayList allows to add null. Is there any case where I would want to add null to an ArrayList?
I am asking this question because in a project we had a bug where some code was adding null to the ArrayList and it was hard to…
Alfredo Osorio
- 906
39
votes
9 answers
Should I accept empty collections in my methods that iterate over them?
I have a method where all logic is performed inside a foreach loop that iterates over the method's parameter:
public IEnumerable TransformNodes(IEnumerable nodes)
{
foreach(var node in nodes)
{
// yadda yadda…
Nick Udell
- 1,194
33
votes
6 answers
I'd like to write an "ultimate shuffle" algorithm to sort my mp3 collection
I'm looking for pseudocode suggestions for sorting my mp3 files in a way that avoids title and artist repetition. I listen to crooners - Frank Sinatra, Tony Bennett, Ella Fitzgerald etc. singing old standards. Each artist records many of the same…
DeveloperDan
- 689
30
votes
4 answers
Is it OK to partially change a collection with PUT or DELETE?
I have a collection of products in a product group e.g.:
product-groups/123/products
If I need to add to the collection, is it OK that I pass only some products with PUT?
If I need to delete some products from the collection, is it OK that I pass…
user151851
- 401
29
votes
1 answer
Why do arrays in .Net have Length but other collection types have Count?
In C# for example, arrays have Length property. But other collection types like lists, etc. have Count property. Is there a reason why these two are different? If so I would like to know.
Arunster
- 417
28
votes
2 answers
Java: why do collections accept a Comparator but not (a hypothetical) Hasher and Equator?
This issue is most apparent when you have different implementations of an interface, and for the purposes of a particular collection you only care about the interface-level view of the objects. For example, suppose you had an interface like…
Sam
- 471
- 4
- 6
22
votes
7 answers
Should "Set" have a Get method?
Let's have this C# class (it would be almost the same in Java)
public class MyClass {
public string A {get; set;}
public string B {get; set;}
public override bool Equals(object obj) {
var item = obj as MyClass;
if (item ==…
vojta
- 338
21
votes
3 answers
Is it a sane thing to return Streams wherever we would normally return Collections?
While developing my API that is not tied to any legacy code, I often find myself writing methods that are purely Streams pipeline terminated by collecting the results. Like this one:
ImmutableSet deriveSomethingMeaningfulFromPrivateState() {
…
jojman
- 313
- 1
- 7
20
votes
5 answers
Efficient way to shuffle objects
I'm writing a program for some quiz software. I have a question class containing the ArrayLists for the question, answer, options, marks and negative marks. Something like this:
class question
{
private ArrayList index_list;
private…
user1369975
- 1,299
- 4
- 16
- 24
16
votes
3 answers
How to unit test method that returns a collection while avoiding logic in the test
I am test-driving a method that is to generate a collection of data objects. I want to verify that the properties of the objects are being set correctly. Some of the properties will be set to the same thing; others will be set to a value which is…
Kazark
- 1,820
16
votes
2 answers
What is the difference of the add and offer methods of Java's PriorityQueue?
In java.util.PriorityQueue we have the methods add(E e) and offer(E e). Both methods are documented as:
Inserts the specified element into this priority queue.
What are the differences between these two methods?
agent13
- 421
15
votes
5 answers
Java Heap Allocation Faster than C++
I already posted this question on SO and it did ok. It was unfortunately closed though(only needs one vote to reopen) but someone suggested I post it on here as it is a better fit so the following is literally a copy paste of the question
I was…
aaronman
- 685