Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [collections]

The tag has no usage guidance.

3
votes
1answer
132 views

Performing complex searchs using C# collections

I have a collection with millions of items of Generic type T. Assume this list never changes. I want to perform many types of searches with subsets of fields of type T. Some with only 1 field and ...
0
votes
1answer
155 views

Should I avoid nested generic collections?

I recently came across a dictionary with lists for values passed as an argument: Demo(Dictionary<string, List<string>> arg) It got me thinking---normally I would abstract any list or ...
3
votes
1answer
400 views

ToList() vs Using default list constructor

After reading rules of Clean Code, one of them is to use default constructor when using transferring data from one collection to other. For example i have : Dictionary<string, string> dict = ...
-1
votes
1answer
158 views

Enforcing IF, ELSEIF and ELSE on collection of ojects

I am designing a project where I have to enforce IF, ELSEIF and ELSE conditions on collection of objects. For example: public class Box { private List<Item> items; } I want to define IF ...
4
votes
4answers
423 views

Hash map without collision check

A few days ago I found a fun fact, that finding a collision of 256-bit hash using brute-force is physically impossible in solar system. That made me think, what would happen if we used a good (...
2
votes
1answer
268 views

How to design an API that returns nested lists?

I am retrieving GeoPoints from a database and organize them for usage in frontend. The GeoPoints are aggregated in segments, the segments in layers, the layers are mapped to drive ids and that map is ...
3
votes
1answer
2k views

Collections: Why are Vectors not used anymore but are still around?

I'm writing some code to do some SSH jobs on a remote machine and one of the methods I'm (forced of) using returns a Vector type. I've learned about them several years ago but never had the chance of ...
10
votes
8answers
865 views

What kind of algorithm requires a set?

On my first programming courses I was told I should use a set whenever I need to do things like remove duplicates of something. E.g.: to remove all duplicates from a vector, iterate through said ...
3
votes
4answers
93 views

Is a lookup key a property?

This is sparked by this where an OP did not want to add a string he was using for a key lookup because he did not consider it property of the class. To me the fact you are using it a for a lookup ...
0
votes
1answer
63 views

Should you tag/flag an object or move it to a different collection?

When you need to classify an object; what are the design implications for using a collection vs a member field (say a Boolean flag). So say I have a Client object, Client is either 'active' or '...
5
votes
3answers
137 views

Collection of objects that can decide to split themselves in half

I've come across a problem like this more than once and I'd like to know the recommended approach(es) for dealing with it. Alternatively, I'd like to know if it has a name. For concreteness, let's ...
1
vote
2answers
223 views

What would it be the best OOP approach for comparing lists of objects? [closed]

I am currently working on an algorithm for comparing two list of objects. Something like this: I have a lists of sales: class Sale { protected $client; protected $products (ArrayCollection ...
0
votes
1answer
59 views

Organizing the same set of data based on two different criteria simultaneously

Let's say I want to model a phonebook. I would like a phonebook to be maintained in sorted order by name of the person that the phonebook entry is created for. However, since it is quite possible ...
5
votes
3answers
281 views

Is an interface with two collection properties, where the second filters the first collection, idiomatic for C#?

I had an interesting discussion with a coworker that revolved around how people interpret the use of properties and methods on an interface. For example, let's say we have a blog with posts in various ...
22
votes
7answers
11k views

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) { ...
8
votes
2answers
2k views

Is there any disadvantage to using Eclipse Collections exclusively?

Because my application works with very large collections of integers, Eclipse Collections seems like a very useful framework because of its primitive collections. I've tested it already, happy to see ...
3
votes
2answers
602 views

Is a linked list considered a collection of objects?

Sedgewick's Algorithm 4ed says Several fundamental data types involve collections of objects. Specifically, the set of values is a collection of objects, and the operations revolve around ...
4
votes
5answers
2k views

How, why or when would you use your own data structure instead of Collections Framework?

While most of the top IT companies will ask you (during the interview) to solve a problem applying some data structure, is it good if you define your own class for that data structure? Like if I know ...
6
votes
2answers
522 views

Why Java Collections class is not final?

Collections class in Java consists only from static methods, as stated in the specs This class consists exclusively of static methods that operate on or return collections. It contains ...
2
votes
1answer
1k views

If I am using just HashMap can I override only hashCode method?

If I need to use only HashMap, why do I need to override equals() method along with hashCode()? How will not implementing equals() affect the working of HashMap?
-1
votes
1answer
30 views

Not able to remove an element from map(NamedValue) while iterating on same map? [closed]

Is there any way that I can remove or add element in map I am currently iterating(using iterator) upon. Please suggest.
3
votes
2answers
160 views

What is the most idiomatic way to iterate collection with different action for first element?

Sometimes we meet a situation where we should iterate (or map) over a collection, applying the same procedure (function) for all elements except the first one. The simplest example is finding the max ...
2
votes
3answers
2k views

Does IEnumerable require to use foreach in collections?

I have the following class that doesn't implement IEnumerable but is working perfectly with foreach. And also, arrays are working without implementing IEnumerable. So why does it keep saying that ...
0
votes
2answers
622 views

Guava Multimap merging consecutive items with same key

I have been playing around with Guava Multimaps and I ran into an issue with tracking when an user is damaged (more the way it's displayed, really) Let's say I have a Multimap<User, Hit> and ...
1
vote
2answers
1k 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
3answers
759 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's iterator has a remove() method, one could say Enumeration ...
1
vote
1answer
60 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 ...
4
votes
4answers
3k 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
254 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, float&...
6
votes
2answers
602 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? ...
3
votes
1answer
2k 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
156 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
285 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 ...
-3
votes
2answers
227 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
117 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
562 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
76 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
302 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
228 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 ...
6
votes
1answer
367 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 ...
22
votes
1answer
2k 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
168 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
238 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 ...
4
votes
2answers
522 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 ...
1
vote
4answers
361 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 same ...
12
votes
2answers
1k 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 java....
1
vote
1answer
71 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 ...
7
votes
1answer
1k 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 perform ...
25
votes
2answers
3k 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 ...
4
votes
5answers
666 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 ...