Skip to main content

Questions tagged [collections]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
7 votes
2 answers
1k views

Why does C# List<T>.AsReadOnly() allocate?

I am looking at the List<T>.AsReadOnly() method. Since List<T> itself is a IReadOnlyCollection<T>. (It implements IReadOnlyList<T> and IReadOnlyList<T> implements ...
Madushan's user avatar
  • 223
-2 votes
1 answer
66 views

Assigning Relationships between various objects in a collection

Similarly to a sports team chemistry or even a better example would be fifa ultimate team chemistry where putting certain players in the team would improve the team's overall chemistry Say there are ...
user2921885's user avatar
1 vote
2 answers
597 views

Depicting association between list and a class inside this list in UML

I'm making an UML class diagram representing classes used for authentication. There are 3 classes: X509AuthenticationFilter ProviderManager X509AuthenticationProvider The X509AuthenticationFilter ...
Joe D's user avatar
  • 113
1 vote
1 answer
246 views

Compile-time type-safe access to collection with polymorphic items, and extending it

I would like to have type-safe access to items in a collection with polymorphic items at compile-time. For this, I set up the code below as a proof of concept. Passing the DataItemProps instances to ...
Cédric Moers's user avatar
0 votes
1 answer
435 views

Thread-Safety with Enumerable collections and copies

Imagine I have an interface IHeapObject: interface IHeapObject { string Id {get;} SomeEnum Type {get;} } and it's implementation HeapObject: class HeapObject : IHeapObject { public string ...
AsPas's user avatar
  • 111
0 votes
0 answers
58 views

Dependency Injector and Collections

I have been searching for an answer in this topic but I haven’t been able to find a satisfactory one like in other topics, where the consensus is solid. The situation To keep things simple: I am ...
Demolition Architect's user avatar
-1 votes
1 answer
112 views

How to subscribe to collection edits happening in a different thread?

I'll be using C# for my example since this is what I am using, but the problem is sort of generic. My app code takes a collection of Foos (externally populated and edited) to do some processing, and ...
Filippo Bannò's user avatar
8 votes
2 answers
6k views

Is there a sensible way to sort coordinates?

Sorting is generally used to solve problems where distance between elements matters. A sorted list/array has the convenient property that the smaller the difference between the indices of any two ...
TheEnvironmentalist's user avatar
-3 votes
1 answer
2k views

C# is fantastic, if only List 'd respect Remove&Return [closed]

In the domain of system-modeling (e, systemVerilog, matlab, phyton), lists are obsoleting arrays, stacks and queues(*) altogether. Other domains that use python, perl and ruby have that same mindset, ...
jsonphy's user avatar
  • 47
-3 votes
4 answers
1k views

C# Why should i limit myself to List or Stack ? ( instead of having both)

List is implemented in C# exactly as Stack, see: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1.push?view=netframework-4.8#remarks https://docs.microsoft.com/en-us/...
jsonphy's user avatar
  • 47
2 votes
4 answers
1k views

Iterating through objects with slightly different interfaces

I am constructing modular logic for my object: separate pieces of functionality are going to separate modules. I will have the ability to connect and disconnect modules on demand. At some point my ...
Xamtos's user avatar
  • 131
4 votes
4 answers
2k views

Why doesn't C++ support covariance in STL containers like C# or Java?

The Covariance and Contravariance feature is well supported in C# and Java collections. However C++ doesn't support them in their STL containers. Why is it so? For example the below code will ...
Sisir's user avatar
  • 858
4 votes
7 answers
7k views

Do we always need to override equals/hashcode when creating a new class?

When creating a new class, should we always override the equals and hashCode even if we don’t intent at that point to use the class with any Collection classes? Or is it better to wait till such a ...
Jim's user avatar
  • 327
2 votes
3 answers
2k views

What is an good way to conditionally update a property on a collection of objects?

I have a collection of Shipment objects that I am iterating through to update another system. A property on a Shipment is a Stockroom. The target system has a unique ID defining a Stockroom record. If ...
CodenameCain's user avatar
0 votes
0 answers
171 views

Most performant way to store and find overlap in (coordinate, id, time interval) triples

Let me explain what I'm doing. I have a grid based on integer coordinates like (5,5), (5,6) etc., and I have an algorithm FindCells that generates a list of (coordinate, interval) tuples. These ...
sollniss's user avatar
  • 109
0 votes
1 answer
57 views

When to use type contraints for sortable collections

I am implementing a Binary Search Tree and cannot decide if it would be good practice to constrain the generic type of the tree to a comparable item, i.e. IComparable<T>. Or, to just use the ...
Connor Blakey's user avatar
0 votes
1 answer
1k views

Two reversed dictionaries or one dictionary with key and value lookup?

I have situation where in the Repository class I have a dictionary: Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> map1 = new Dictionary<TableName, Dictionary<...
NSKBpro's user avatar
  • 219
3 votes
1 answer
180 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 ...
deeproute's user avatar
0 votes
1 answer
2k 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 ...
user avatar
3 votes
1 answer
2k 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 = ...
NSKBpro's user avatar
  • 219
-1 votes
1 answer
163 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 ...
Kishor Prakash's user avatar
4 votes
4 answers
2k 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 (...
CodeSandwich's user avatar
2 votes
1 answer
623 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 ...
phil294's user avatar
  • 179
5 votes
1 answer
4k 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 ...
svarog's user avatar
  • 469
10 votes
8 answers
1k 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 ...
Floella's user avatar
  • 383
2 votes
4 answers
159 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 ...
paparazzo's user avatar
  • 1,937
0 votes
1 answer
64 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 '...
JackMordaunt's user avatar
5 votes
3 answers
591 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 ...
Daniel McLaury's user avatar
2 votes
2 answers
853 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 ...
nimrod188's user avatar
0 votes
1 answer
68 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 ...
Vladimir Stokic's user avatar
4 votes
3 answers
393 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 ...
Greg Burghardt's user avatar
22 votes
7 answers
15k 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) { ...
vojta's user avatar
  • 338
8 votes
2 answers
3k 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 ...
Saturn's user avatar
  • 3,907
3 votes
2 answers
1k 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 ...
Tim's user avatar
  • 5,485
3 votes
5 answers
6k 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 ...
Aman Grover's user avatar
6 votes
2 answers
823 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 ...
vtor's user avatar
  • 169
2 votes
1 answer
2k 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?
karan ratnaparkhi's user avatar
-1 votes
1 answer
32 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.
Rahul Tyagi's user avatar
3 votes
2 answers
203 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 ...
ov7a's user avatar
  • 141
5 votes
3 answers
5k views

Is implementing IEnumerable required to use foreach on 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 ...
Roshan Fernando's user avatar
0 votes
2 answers
1k 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 ...
Teti's user avatar
  • 3
1 vote
2 answers
4k 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 ...
Freddy's user avatar
  • 161
2 votes
3 answers
2k 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 ...
juliaaano's user avatar
  • 131
1 vote
1 answer
79 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 ...
NPE's user avatar
  • 121
4 votes
4 answers
10k 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? ...
user4205580's user avatar
1 vote
3 answers
318 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&...
user5539357's user avatar
6 votes
2 answers
1k 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? ...
konishiki's user avatar
  • 475
6 votes
1 answer
7k 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 ...
YoYo's user avatar
  • 586
4 votes
2 answers
889 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 ...
Oebele's user avatar
  • 215
5 votes
3 answers
417 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 ...
konishiki's user avatar
  • 475