Why Can't I Retrieve an Item from a Hashset Without Enumeration

Why can't I retrieve an item from a HashSet without enumeration?

How were you proposing to retrieve the item from the hash set? A set is by definition not ordered in any way and therefore, there is no index with which to use to retrieve the object in question.

Sets, as a concept, are used to test inclusion, i.e. whether or not the element in question is in the hash data set. If you're looking to retrieve a value from a data source using a key value or index, I would suggest looking into either a Map or a List.

EDIT: Additional answer based on the Edit to the original question

Soonil, based on your new information, it looks like you might be interested in implementing your data as a Java Enum, something similar to this:

 public enum SoonilsDataType {
A, B, C;

// Just an example of what's possible
public static SoonilsDataType getCompositeValue(SoonilsDataType item1,
SoonilsDataType item2) {
if (item1.equals(A) &&
item2.equals(B)) {
return C;
}
}
}

Enum's automatically inherit values() which returns the list of all values in the enum's "set", which you can use to test inclusion against in the same way as the Set. Also, because its a full class, you can define new static methods to do the composite logic (like I was trying to allude to in the example code). The only thing about the Enum is that you can't add new instances at runtime, which may not be what you want (though if the set's data size isn't going to grow at runtime, the Enum is what you want).

How to retrieve actual item from HashSetT?

What you're asking for was added to .NET Core a year ago, and was recently added to .NET 4.7.2:

In .NET Framework 4.7.2 we have added a few APIs to the standard Collection types that will enable new functionality as follows.

- ‘TryGetValue‘ is added to SortedSet and HashSet to match the Try pattern used in other collection types.

The signature is as follows (found in .NET 4.7.2 and above):

    //
// Summary:
// Searches the set for a given value and returns the equal value it finds, if any.
//
// Parameters:
// equalValue:
// The value to search for.
//
// actualValue:
// The value from the set that the search found, or the default value of T when
// the search yielded no match.
//
// Returns:
// A value indicating whether the search was successful.
public bool TryGetValue(T equalValue, out T actualValue);

P.S.: In case you're interested, there is related function they're adding in the future - HashSet.GetOrAdd(T).

How to access the reference values of a HashSetTValue without enumeration?

Basically you could reimplement HashSet<T> yourself, but that's about the only solution I'm aware of. The Dictionary<Peptide, Peptide> or Dictionary<string, Peptide> solution is probably not that inefficient though - if you're only wasting a single reference per entry, I would imagine that would be relatively insignificant.

In fact, if you remove the hCode member from Peptide, that will safe you 4 bytes per object which is the same size as a reference in x86 anyway... there's no point in caching the hash as far as I can tell, as you'll only compute the hash of each object once, at least in the code you've shown.

If you're really desperate for memory, I suspect you could store the sequence considerably more efficiently than as a string. If you give us more information about what the sequence contains, we may be able to make some suggestions there.

I don't know that there's any particularly strong reason why HashSet doesn't permit this, other than that it's a relatively rare requirement - but it's something I've seen requested in Java as well...

Is it possible to perform an operation on an object in a Hashset without iteration

If you want to call a method for all the elements in a Set, you must iterate over the elements of that Set.

That said, instead of an explicit loop, in Java 8 you can use the forEach method:

selectedMap.get(true).forEach(Place::remove);

or

selectedMap.get(true).forEach(place -> place.remove(...));

in case the remove() method requires some arguments.

How does hashset find value to print out with O(1) time complexity?

Hash table data structures make use of a hash function that maps the key to a number of desired size and properties. It also makes use of Random-access memory (RAM) to perform average constant-time lookups.

You know that with a standard array, you can typically access elements with their index in constant-time. This is due to how a computer's memory works in practice. Within the constraints of the hardware, you can store an array as long as you like, but indexed access will happen in constant time. You never have to iterate through your entire RAM memory as long as you know where something is stored.

With hash tables, you essentially map the key to an index, which lets you access the part of memory you stored something at previously.

In practice, there are more things going on (collision handling, buckets etc.), but this is the basic principle. See HashTable on Wikipedia.



Related Topics



Leave a reply



Submit