The tag has no usage guidance.

learn more… | top users | synonyms

1
vote
2answers
43 views

Iterate over Collection while removing elements from it

I am iterating over a collection and want to delete some entries of it. Example: For i = 0 To Node.Nodes.Count If i >= args.TotalNumberOfNodes Then Node.Nodes.RemoveAt(i) End If ...
2
votes
1answer
80 views

Should I return Iterable or Enumeration in Java

In order to achieve better immutability, should my API return java.lang.Iterable<T> or java.util.Enumeration<E>? Since Iterable has a remove() method, one could say Enumeration is the ...
1
vote
1answer
17 views

How to map collection of unique large numbers to an array?

I have a list of unique 8 byte random ids and new ids are generated often. Let's say that the total number of ids can become at most N (let's say 100,000). Now, I want to map each of these ids to a ...
3
votes
5answers
199 views

Method returning an unmodifiable list

In Java you could return a list that cannot be modified by the caller by doing return Collections.unmodifiableList(list). Should the method name indicate that it's returning an unmodifiable list? ...
2
votes
3answers
228 views

Is it good to store distance like that?

I have a Position class with int x and int y fields. I have a bunch of points and some of them need to know the distance to all other points. Does it make sense to store it in Map<Position, ...
6
votes
2answers
119 views

Returning Unmodifiable Collections only tees you up for runtime exceptions?

Seeing as how there are no distinct Unmodifiable Collections interfaces, aren't you just setting yourself up for runtime exceptions by returning Unmodifiable Collections from method invocations? ...
2
votes
1answer
212 views

LinkedList - why no direct .next() on elements?

I never understood this: why is it that I can't seem to find a core collections api somewhere that Allows me to retrieve elements that have a direct .next() or .previous() method on them without all ...
4
votes
2answers
101 views

Accessing members of a set by one of their properties

I often find myself in the situation where an object contains a set of objects, and I want to access one of these objects based on one of its properties. How I currently do this in C++ is to put ...
6
votes
3answers
200 views

Doesn't returning a Collection as Unmodifiable / Immutable unnecessarily break method scope?

Let's say I create a Collection in an instance method. I do not assign that reference to any instance variables. Rather, I just return it to the invoker. Then, I exit. Now, the only thing with a ...
-2
votes
2answers
134 views

How do you hash 4 doubles into a size_t?

I have bounding boxes the key type. Box { double mins[2]; double maxs[2]; } And I want to have Box as the key type in the D programming language, so I have to implement: size_t toHash() const ...
0
votes
0answers
33 views

Collection for time-series data in .Net

I am communicating with a device that returns logged data in roughly this format: { timestamp of first data set, [ device1value, device2value, device3value] }, { timestamp of second data set, [ ...
1
vote
1answer
51 views

Repository Pattern Class Collection examples with Lazy/Eager loading

When implementing the repository it's fairly easy for a stand alone class. Unfortunately, we are unable to use an ORM to manage our data access, so I'm trying to recreate some of the functionality ...
0
votes
2answers
73 views

Altering the contents of a Collection outside the scope of an instance method?

Is it best practices to never alter the contents of a Collection that is passed as a parameter? I want orgSet to only have words. But, sometimes there are one char strings (like question marks, ...
2
votes
1answer
86 views

Finding methods that return null instead of a collection (in Java)

I'm looking for a good tool or technique to search a Java code base and find every method that says it returns a collection (List, Set, Map, etc.) but sometimes returns null. This would be a cool ...
3
votes
1answer
129 views

What to do when properties of a HashMap and ArrrayList are neccessary?

I have a data structure that stores various permission groups for a multiplayer game server. There is an instance of a "Rank" class for each permission group. Each rank also has a name. I need to be ...
5
votes
1answer
276 views

Why aren't Java Collections put in a dedicated package

Why do they just reside in java.util and not somewhere more specialized like java.collections or java.util.collections? It could contribute to mess up with different unrelated code. Couldn't it? Was ...
20
votes
1answer
1k views

Why do arrays in .Net have Length but other collection types have Count? [closed]

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.
0
votes
1answer
127 views

Collections.sort question

My question maybe related to this question - Encapsulating Internal Data Structure I have a class LCTile.java which contains a Coordinate.java class. Coordinate.java that contains latitude and ...
1
vote
1answer
112 views

HashMap to replace Singletons?

Consider the following: I have controllers and views in a client-application. As this runs purely on the client side, each controller must only exist once. At first I thought about implementing ...
3
votes
2answers
132 views

(Partially) Sorting a lazy-evaluated collection

So I'm trying to implement a lazy-evaluating data type (in PHP, though that shouldn't matter any) where you can queue up actions to take on a set of data. This data can come (theoretically) from any ...
0
votes
4answers
250 views

Is collection object better being immutable? [closed]

As per this link, it is an advice that: A good object should never change his encapsulated state. Remember, an object is a representative of a real-life entity, and this entity should stay the ...
12
votes
2answers
604 views

Packaging structure of Java collections (java.util) - why does Iterable sit in java.lang?

As per the below diagram, except for interface Iterable, all the remaining constructs (interface / class / abstract class) sit in same package java.util   Why does Iterable sit in ...
1
vote
1answer
65 views

Virtual method returning a unique collection - how to ensure and hint?

I have a virtual method that returns a collection of items that must be unique. I want to make sure that it will be obvious when overriding the method. What is the best way to do this? Here is an ...
5
votes
1answer
398 views

In Java, why does (unsorted) Set implement Iterable, but SortedMap does not?

There are two aspects to this question that I felt were too closely related to ask as separate questions. Why doesn't SortedMap implement Iterable<Map.Entry<K,V>>? If you need to ...
16
votes
2answers
1k views

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 ...
3
votes
5answers
302 views

Injecting collections as constructor dependencies

We are using constructor dependency injection in our application. Following that approach we inject everything using an injection container so we are able to replace any dependency with a Mock. Some ...
0
votes
3answers
335 views

Why python function programming functions are not collection methods? [duplicate]

In other words, is there a Python design related reason for it to be so? Functions like map, filter, reduce etc. are just plain functions. Is it just a poor design choice (if it is a good one, ...
15
votes
3answers
543 views

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: ...
2
votes
3answers
470 views

Concurrent collections, should read methods allow multiple threads at one time?

I have custom collection and I want to add wrapper to allow concurrent access. public class MyConcurrentCollection<T> { private MyCollection _collection; // passed in constructor ...
1
vote
2answers
66 views

What Scala type to use for a list of meeting participants

I want to store a number of participants for a meeting. The order of them is not important. The only thing I want to make sure is, that I will be able to add and remove participants, use filter ...
2
votes
1answer
646 views

Maintain ordered collection by updating as few `order` fields as possible

I'm working on integrating a reorderable lists UI widget with a Meteor.js (MongoDB, in effect) collection: { order: ???, // what type to use here? property1: ..., ... propertyN: ... } Each ...
6
votes
6answers
4k views

Is there an alternative to instanceof when filtering a Java stream by class?

I have an unexpected situation in a project in which all types extending one class are packed into a Java collection; but only a specific extension of that class contains an additional method. Let's ...
38
votes
9answers
6k views

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<TransformedNode> TransformNodes(IEnumerable<Node> nodes) { ...
2
votes
1answer
430 views

CopyOnWriteArrayList: Why do we have to make a full copy of source data?

I have a lot of confusion about Java's CopyOnWriteArrayist. I know, when we have a modification on it like update, insert of set, it will lock the whole list, copy out a total copy of the source list, ...
2
votes
3answers
2k views

What should I do in C++ when implementing a container class: storing objects by value or by reference?

I'm new to C++, coming from Java. In Java, all variables (except for primitives) are essentially pointers. They hold the address of whatever they're 'holding'. So any Java data structure stores it's ...
5
votes
2answers
3k views

Why does java collections not supply a function for the index of max value?

I have been using Collections.max(list) for many projects, but I occasionally want to find the index of that maximum element. I feel silly writing a function to do this for myself in every program I ...
4
votes
1answer
486 views

Why does SortedDictionary not implement IReadOnlyDictionary?

The .NET type SortedDictionary<TKey, TValue> does not implement the interface IReadOnlyDictionary<TKey, TValue>, although Dictionary<TKey, TValue> does. It is trivial to make ...
2
votes
1answer
104 views

Models, collections…and then what? Processes?

I'm a LAMP-stack dev who's been more on the JavaScript side the last few years and really enjoying the Model + Collection approach to data entities that BackboneJS, etc. uses. It's helped me organize ...
1
vote
3answers
473 views

Filtering a List

I will try to simplify my problem. Let say I have a collection (more specifically a List) with different types of elements, for explanation purpose let say we have objects "Person" with different age ...
50
votes
8answers
4k views

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 ...
3
votes
1answer
306 views

Why is subclassing TraversableOnce not recommended

Reading http://www.scala-lang.org/api/2.11.1/index.html#scala.collection.TraversableOnce: Directly subclassing TraversableOnce is not recommended - instead, consider declaring an Iterator with a ...
36
votes
6answers
4k views

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 ...
1
vote
4answers
281 views

“Collection Wrapper” pattern - is this common?

A different question of mine had to do with encapsulating member data structures inside classes. In order to understand this question better please read that question and look at the approach ...
1
vote
2answers
722 views

java.util.HashMap lock on actual HashMap object compare to lock on object that encapsulate the HashMap

The below Javadoc is an snippet of HashMap documentation. Why authors would emphasize on putting a lock on the object that encapsulate a HashMap? Lock on the actual HashMap Object makes for sense. ...
1
vote
3answers
189 views

Best way to find whether a collection does or does not contain an element with a specific desired quality

It seems to be a very common thing to have to tell whether some list or set contains at least one object matching a given condition, yet my prior searching and reading have never found a satisfactory ...
1
vote
3answers
550 views

Empty superclass for Collection of derived classes

Basically, what I would like to obtain is a way to iterate through ONE list, and call methods specific to the interface the objects in the collection implement. In my Java project, it would result in ...
2
votes
2answers
1k views

Collection vs data structure vs aggregate

I know three terms that have to do with storing objects or data. I'm not sure I understand the exact differences between the terms. Please confirm if what I'm about to write is accurate. A data ...
0
votes
5answers
2k views

Is it a good practice to wrap collection in Java? [closed]

I came across a snippet like this, and find it's over engineering. Is it a good practice? public class SchoolList extends ArrayList<School> { } public class School extends ...
81
votes
5answers
23k views

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 ...
3
votes
2answers
2k views

Should I return Collection or ImmutableCollection from a method?

When writing an API in Java, returning an immutable collection of some sort, I've got the option of returning Collection (or List, Map, etc) from the method, or guava's ImmutableCollection (or ...