66 votes
Accepted

Should "Set" have a Get method?

The problem here isn't that HashSet lacks a Get method, it's that your code makes no sense from the perspective of the HashSet type. That Get method is effectively, "get me this value , please", to ...
David Arno's user avatar
  • 39.3k
24 votes

Should "Set" have a Get method?

You already have the item that is "in" the set - you passed it as the key. "But it isn't the instance that I called Add with" - Yes, but you specifically claimed that they were equal. A Set is also ...
Caleth's user avatar
  • 11.1k
22 votes

What kind of algorithm requires a set?

I assume that depending on the language used there might be a difference in performance. But is there a reason to use a set other than that? Oh yes, (but it's not performance.) Use a set when you can ...
candied_orange's user avatar
19 votes

Should "Set" have a Get method?

Your problem is that you have two contradictory concepts of equality: actual equality, where all fields are equal set membership equality, where only A is equal If you would use the actual equality ...
amon's user avatar
  • 134k
14 votes
Accepted

Is implementing IEnumerable required to use foreach on collections?

The official documentation looks straightforward to me: The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System....
Arseni Mourzenko's user avatar
14 votes

What kind of algorithm requires a set?

However, I could also do that by adding each elemento to another vector and checking if the element already exists. If you do that, then you are implementing the semantics of a set on top of the ...
Michael Borgwardt's user avatar
14 votes

Is there a sensible way to sort coordinates?

the smaller the difference between the indices of any two elements, the smaller the difference between the values of the elements. When working with lists of coordinates or similar values with more ...
Michael Borgwardt's user avatar
12 votes

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

The reason is the underlying object and memory models. To simplify the reasoning: In java and C#, objects of a class are managed by reference. Containers do not store directly the object value but ...
Christophe's user avatar
  • 77.3k
11 votes
Accepted

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

In my experience, as far as interviews go, the exact (language specific) implementation doesn't actually matter and many will simply let you write it out in pseudo code. If they do require you to ...
yitzih's user avatar
  • 993
8 votes

Is implementing IEnumerable required to use foreach on collections?

It isn't required. The foreach keyword basically just "rewrites" your code so it becomes a while loop calling MoveNext() and looking at Current, but the interface IEnumerable isn't needed. This is ...
sara's user avatar
  • 2,559
8 votes
Accepted

What kind of algorithm requires a set?

You are asking about sets specifically but I think your question is about a larger concept: abstraction. You are absolutely correct that you can use a Vector to do this (if you are using Java, use ...
JimmyJames's user avatar
  • 27.3k
8 votes

Hash map without collision check

You are right, a hash function which such a large hash space will see very few collisions. But a hash table uses a hash function for a specific purpose: mapping hash table entries into a specific bin. ...
amon's user avatar
  • 134k
8 votes
Accepted

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 A default constructor is one that either has no parameters, or ...
David Arno's user avatar
  • 39.3k
7 votes

Is implementing IEnumerable required to use foreach on collections?

Eric Lippert actually has a piece about this: https://blogs.msdn.microsoft.com/ericlippert/2011/06/30/following-the-pattern/ Eric's explanation: What is required is that the type of the collection ...
MetaFight's user avatar
  • 11.6k
7 votes
Accepted

Is it good to store distance like that?

The distance between two Positions is really not an attribute of either of them. If calculating the distance is so heavy that caching the results would be beneficial, then you should probably ...
COME FROM's user avatar
  • 2,696
7 votes

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

The default equals() method which your object inherits from class Object performs a reference comparison. This means that no instance of your objects will ever be equal to any other instance, because ...
Mike Nakis's user avatar
  • 32.2k
7 votes

Why Java Collections class is not final?

Aren't there any performance benefits if the class would be final itself? For instance methods, potentially. For static methods, no. Knowing that a non-overridden instance method is in a final ...
Servy's user avatar
  • 1,976
7 votes

Should "Set" have a Get method?

But why is it impossible to get a particular item from the set? Because that's not what sets are for. Let me rephrase the example. "I have a HashSet that I want store MyClass objects in and I ...
Old Fat Ned's user avatar
7 votes
Accepted

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

In Java8 Vector's source, there is no depracated notice. There is the following comment in the header : Unlike the new collection implementations, {@code Vector} is synchronized. If a thread-...
Walfrat's user avatar
  • 3,496
7 votes
Accepted

How to design an API that returns nested lists?

For new colleagues, this is insanely hard to understand Yeah, no kidding. I'm surprised anyone understands it. Why are you not using actual classes? public ResponseEntity<List<Car>> ...
Michael's user avatar
  • 269
7 votes
Accepted

Hash map without collision check

Yes, it is possible to do this. Look up the details of ZFS for a deployed, production-quality system that uses this idea. In ZFS, any data that is to be stored is hashed with a cryptographic, 256-...
Jules's user avatar
  • 17.8k
7 votes

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

Typically, people remove things from lists already knowing what those things are, as that knowledge is why they want to remove the item in the first place. By contrast, people don't usually want to ...
Nat's user avatar
  • 1,083
6 votes

Returning Unmodifiable Collections only tees you up for runtime exceptions?

I don't know why people think of such exceptions as risky. Developer writes code using add. Developer thinks it might be a good idea to test the code he just wrote. Developer gets a big scary ...
Karl Bielefeldt's user avatar
6 votes
Accepted

Returning Unmodifiable Collections only tees you up for runtime exceptions?

Returning Unmodifiable Collections only tees you up for runtime exceptions? Yes and no. Yes, it does do that. No, it doesn't only do that. It also prevents a class of problems from occurring; i.e. ...
Stephen C's user avatar
  • 25.2k
6 votes

Should "Set" have a Get method?

As soon as you override equals you better override hashcode. As soon as you have done this your "instance" should never change internal state again. If you do not override equals and hashcode VM ...
oopexpert's user avatar
  • 779
6 votes
Accepted

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

IEnumerable<Post> PublishedPosts { get { return Posts.Where(p => p.Published) }; } if you want a single place for your filtering logic. Or, simply filter as needed: var publishedPosts = ...
Robert Harvey's user avatar
6 votes

What kind of algorithm requires a set?

Software entities that represent real-world entities often are logically sets. For example, consider a Car. Cars have unique identifiers and group of cars forms a set. The set notion serves as a ...
andy mango's user avatar
6 votes
Accepted

Do we always need to override equals/hashcode 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? No and I would go even further as to say you probably don't need ...
JimmyJames's user avatar
  • 27.3k
6 votes
Accepted

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

If there was no allocation, there'd be no reason to have the AsReadOnly() function. If all you want to do is cast it to an IReadOnlyList, then do that: var myList = new List<string>(); var ...
Erdrik Ironrose's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible