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
2 answers

HashSet allows multiple items with same HashCode

My HashSet contains multiple 'AccessRequests' with the same HashCode. I only want there to be one instance. I didn't think items with the same HashCode could show up in a HashSet. What am I doing wrong here? UPDATE: Based on the assumption that…
CoupFlu
  • 311
  • 4
  • 20
0
votes
2 answers

Is it safe to use HashSet as a very simple cache in a multi-threaded environment?

I realize there are similar questions/answers to this, but I can't seem to find exactly what I'm looking for. I'm looking to implement an in-memory cache that caches the result of a call to the DB, which is doing an existence check. This existence…
TR1096
  • 47
  • 8
0
votes
1 answer

why N entries of HashMap and N entries of HashSet take same space in Heap?

i am checking whether Set takes less memory than Map. because in Map we Enter 2 Integer objects but in Set we store (1 Integer) + (1 Static Object) RUN 1:- i have tried to fill Set with 50,000 integers by testSet(i) function RUN 2:- i have tried…
user3869979
  • 21
  • 1
  • 6
0
votes
1 answer

Seeking Optimization Suggestions for Map-to-Map Conversion

I'm seeking feedback as to whether there's a more efficient approach than what I'm doing in my code shown at the bottom. Basically, given this map: Set A_Set = new HashSet<>(Arrays.asList("1111", "2222", "5555")); Set
Casey B.
  • 279
  • 3
  • 13
0
votes
1 answer

How to write a remove method for hashes to remove a value?

I wrote a remove method for a HashMap and I'm getting errors that it's not returning the correct value. The instructions read: The remove(Object) method should remove and return the value associated with the given key if the key is found in the…
Adan Vivero
  • 422
  • 12
  • 36
0
votes
0 answers

Chosing the correct data structure

I am writing a Java application where I am receiving related data from two sources, from first source I receive all data item one time and from second source I am receiving data as continues stream. There are some common fields in both data items,…
0
votes
1 answer

Two different HashSet objects affected by one command (JAVA)

I have a custom object: Class(id, code, title, numberOfECTSPoints, headProfessor, attendingStudentsSet) I create an instance of that custom object called class1, and use a pre-created HashSet to feed its constructor. After I do that and clear the…
Gandeloft
  • 45
  • 8
0
votes
1 answer

Automapper & Entity Framework mapping complex relations suggestions

I am trying to map a complex entity type and am hitting some wall when it comes to nested relations. Basically here's my EF entity: public partial class LDT001002_FILE_MST { public LDT001002_FILE_MST() { …
rzr19
  • 11
  • 1
0
votes
2 answers

Duplicate custom objects are getting added to Hashset

I have an Employee class having 2 attributes id and name. I am overriding the hashcode and equals method as given below. Employee.java: import java.util.Objects; public class Employee { private int id; private String name; public int…
user3244519
  • 661
  • 5
  • 18
  • 36
0
votes
1 answer

Adding multiple users creates multiple roles

I have this solution to enter single Users into my application but now we need to incorporate all(over 2k) our users, I have the following code that creates a single user from Model. Single user Entry. WORKS GREAT FOR SINGLE USER. public interface…
pool pro
  • 2,084
  • 12
  • 21
0
votes
0 answers

Java: HashSet Behavior with Character vs String

I have the below method which checks if given two strings have atleast one character in common String checkCommonCharacters(String s1, String s2) { Set s1Chars = new HashSet(Arrays.asList(s1.split(""))); Set s2Chars = new…
OTUser
  • 3,788
  • 19
  • 69
  • 127
0
votes
1 answer

Calculating Word Frequency Using StreamTokenizer () , HashMap() , HashSet(). in Java Core

import java.io.*; import java.util.*; class A { public static void main(String args[]) throws Exception { Console con = System.console(); String str; int i=0; HashMap map = new HashMap(); HashSet set =…
Akash
  • 425
  • 2
  • 7
  • 21
0
votes
2 answers

Removing duplicates in sql without using aggregate functions

I'm starting to learn java and I have a problem with my practice exercise. This is a snippet of the whole code: import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Connection; import…
0
votes
2 answers

How to check how many HashSet are inside Map>

public class Phonebook implements PhonebookInterface { Map> phonebook; public Phonebook() { this.phonebook = new HashMap>(); } @Override public void addNumber(String person, String number) { if…
HorrorRice
  • 33
  • 1
  • 5
0
votes
0 answers

Why is the int[] array I'm printing out being cut off?

I'm trying to print out an int[] array for this assignment I'm working on. My code is as follows: import VacuumCleanerEnvironment.Constants; import java.util.*; /** * Agent Class * Student's Assignment */ public class Agent { private String…