Questions tagged [hashset]

A HashSet encapsulates operations that allow for the comparison of elements in collections. HashSets are frequently used to determine overlapping and unique elements within a collection.

HashSet<T> was introduced in .NET Framework 3.5 as part of the System.Collections.Generic namespace. HashSet is an unordered collection containing unique elements and provides a set of standard set operators such as intersection and union (plus many more). It has the standard collection operations Add (although this method returns a Boolean indicating whether or not that element already existed in the collection), Remove, and Contains, but because it uses a hash-based implementation for object identity, these operations are immediately accessible without looping the entire list as occurs with the List<T> collection for example (O(1) rather than O(n)).

Source: Linq to Objects using C# 4.0, Troy Magennis, Addison Wesley, 2010, Pearson Education Inc, ISBN-13: 978-0-321-63700-0

References

2277 questions
0
votes
1 answer

Array of LinkedLists always gives NullPointerException

I'm creating my own HashSet implementation for practice. I'm getting a NullPointerException every time I add anything to my array of linked list. I've even tried to initiate the LinkedList inside each array index with a value (just to debug) and I'm…
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
0
votes
1 answer

Why does the HashSet preserve insertion order?

Maybe I'm doing this incorrectly, but I'm trying to shuffle a List by casting it into a HashSet, List
art = new List
(rootobject.articles); HashSet
setart = new HashSet
(art); When I iterate through both the…
insomniac
  • 192
  • 1
  • 3
  • 16
0
votes
2 answers

Java contains() method returns False even though overridden equals() returns True

I have an class called Record that consists of a vector of Data class objects. Class Data has nothing but two fields: Object value; String name; I override the equals method in the Record class as follows: public boolean equals(Object obj) { …
HR1
  • 487
  • 4
  • 14
0
votes
1 answer

How to get a random element from an implemented set?

I am implementing my own Set called RandomizedSet using a technique called open hashing. I have to create a function that returns me a random element in my set, with the condition that all elements must have the same probability of being chosen and…
Samuel Mariña
  • 109
  • 3
  • 15
0
votes
1 answer

How to correctly identify words when reading from a file with java Scanner?

I'm trying to do an exercise where I need to create a class to read the words from a .txt put the words in an HashSet. The thing is, if the text read "I am Daniel, Daniel I am." I'll have a word for "am" , "am." and "Daniel," and "Daniel". How do I…
Daniel Oscar
  • 287
  • 1
  • 9
0
votes
1 answer

How to keep this code repeating more than once

My code pulls the links and adds them to the HashSet. I want the link to replace the original link and repeat the process till no more new links can be found to add. The program keeps running but the link isn't updating and the program gets stuck in…
0
votes
2 answers

How can I index object properties?

I have a list of objects that contain peoples names and their service times. These names can occur more than once throughout this list, and the service times can be different for each occurrence. What I'm trying to do is create a HashSet of names…
Michael
  • 1,454
  • 3
  • 19
  • 45
0
votes
2 answers

How to use ExceptWith with the type of HashSet> in C#?

HashSet> test1 = new HashSet> (); for (int i = 0; i < 10; i++) { List temp = new List (); for (int j = 1; j < 2; j++) { temp.Add (i); temp.Add (j); } test1.Add…
lynkewsw
  • 5
  • 3
0
votes
1 answer

How to avoid duplicate values in a sorted string array?

I found this code which was meant to avoid duplicate values for numbers so I changed it to consider String elements.It successfully avoids duplicate names when printing, but it does not sort the names in alphabetic order. I also hope that it does…
Genesis_Kitty
  • 63
  • 2
  • 7
0
votes
2 answers

Iterating through a HashSet using a for loop

I have a HashSet that I would like to iterate through a for loop and display its contents but I don't know how to make it work. I can't seem to find a way to access an element of a certain index(i) of a HashSet. Is there a way to do this? I have the…
0
votes
1 answer

What is difference in returning element using it.next() and directly printing the object of hash set in sysout

In hashset, i am able to print the objects added using sysout of obj name and also by iterating and returning the elements. what is the difference? both gives the objects stored in the hash set. Below is the code : public static void main(String[]…
Siva
  • 17
  • 7
0
votes
2 answers

HashSet of Strings and retrieval of only the first word of each element

If I have a HashSet, how can I retrieve only the first word of each element of the Set?
0
votes
1 answer

Looking for C++ immutable hashset/hashmap

I work on GPL'ed C++ code with heavy data processing. One particular pattern we often have is to collect some amount (thousands to millions) of keys or key/value pairs (usually int32..int128), insert them into hashset/hashmap and then use it without…
Bulat
  • 2,435
  • 1
  • 15
  • 15
0
votes
1 answer

Creating new collection with calculated totals from HashSet of existing ArrayList

I'm struggling with the following: I'm building an order system. I have an ArrayList "sales", the list contains some variables and a HashSet with the items that were sold as part of the order. There's a Part class with getters for the part name…
TomZ
  • 1
  • 1
0
votes
1 answer

What does Set[] sets = new Set[]{setW, setX, setY, setZ}; in Java mean?

I believe this is a set of integer hashsets, but I'm confused about why [] are used before and after the equals sign.
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35