All Questions
227 questions
0
votes
1
answer
75
views
A hash function that maintains mathematical equality (especially for sets)
I'm working on a little math-based programming language (written in Rust) that uses sets instead of types. In it, a variable or value belongs to a set of values (eg. x : {1, 2, 3} or msg : Str). The ...
-1
votes
2
answers
85
views
Why is a private variable of type HashSet is being shared among object instances of the class? [closed]
I recently stumbled upon this weird behaviour of Java where a variable of type Hashset, which is declared private in the parent class, is not unique for each instance of the child. Instead, all the ...
0
votes
1
answer
251
views
Cleanest Way to Get a Set in Redis where Individual Elements Expire in 30 Days?
I need a data structure similar to a Python set in redis, with the additional capability of individual elements automatically expiring (getting popped) from the set 30 days after insertion. Basically, ...
1
vote
1
answer
140
views
Is a ISet<T> sort of "guaranteed" to have O(1) lookup speed?
Sometimes I have a method that I want to accept a broad IEnumerable<T> as input, but I'm doing an operation that might have O(2^n) impact if it's not a HashSet<T>.
Even worse when it's a ...
-1
votes
1
answer
328
views
How set (hashset) in python works, especially in comparison? [duplicate]
I am learning hash table and BST. I got confused when I compared (True) with True I have got True as an answer. But if I compare (True, True) == True, True I,m getting set: (False, True). Why it is ...
0
votes
1
answer
37
views
Quiz Application returning duplicates
I'm relatively new to programming , and I'm creating a quiz application. The problem is it keeps on repeating questions
Random random = new Random();
Set<Integer> set = new HashSet<>();
...
0
votes
1
answer
659
views
cast set to linkedhashset
following is the scenario that I am encountering
I have this set of string here
Set<String> attributes = new LinkedHashSet<>();
yet I have another function with a signature LinkedHashSet
...
-2
votes
1
answer
355
views
How to check if a set of object contains object with a particular attribute [closed]
Please look at the do while loop, I think the comment is self explanatory.
I want to check if the set contains all the cards having symbol a,b,c,d (implementing it with help of array or anything else)....
6
votes
2
answers
177
views
Why writing map entries to a HashSet is slower than to a CopyOnWriteArraySet in Java
I think writing to a HashSet will be faster than to a CopyOnWriteArraySet; I'm not doing multi threading here. However I surpisingly got benchmark results indicate writing map entries to a ...
0
votes
1
answer
148
views
Adding Int array values in the set of type short
public class Solution {
public Set<Short> setx = new HashSet<>();
public void AddAll(int[] numbers) {
for (int number : numbers) {
setx.addAll((short) number);
...
2
votes
1
answer
829
views
remove duplicates in Set [duplicate]
How can I avoid inserting duplicate elements in a Set? If I have:
Set<User> user=new HashSet<>();
User user1=new User("11","Mark",null,&...
-1
votes
4
answers
117
views
print all repeated items in array object using java
I would like to know how to print all repeated items in an array of objects. For ex:
[
{
"key": "KEY1",
"value": "123",
},
{
"...
1
vote
2
answers
1k
views
Can't add an object to a Set
Hi I have a little problem in java, I do not have an error but I do not get what I want. Here I create 3 Pizza with and I want to put them in a set, the problem is that when I print my set I only have ...
0
votes
1
answer
930
views
Most performant way to verify an element exists in a given set in C++
I've been trying to write a code that finds all the numbers which summed to its inverted counterpart would result in an odd number, as for "12 + 21 = 33", "605839 + 938506 = 1544345&...
1
vote
1
answer
35
views
How to find a key in a multiple-value HashMap?
my task is to make a synonym dictionary using HashMaps and Sets.
I have the following code in my main method:
public static void main(String[] args) {
addSynonym("casa", "...
4
votes
2
answers
8k
views
Set.of(E... elements) - which Set implementation is it using? and what is its relation to new HashSet<>()?
I wanted to ask - what is the difference between
Set<CurrencyType> set1 = new HashSet<>() {{
add("a");
add("b");
add("c");
}}
and
Set<...
1
vote
3
answers
939
views
Java HashSet does not call overriden equals but it should [duplicate]
In main method I create two different equal objects.
private static Set<Cards> allCombs = new HashSet<>();
cards = new Cards();
cards.add(new Card(1, 10));
cards.add(new Card(1, 12));
...
3
votes
2
answers
166
views
HashSet adds two objects which returns true for equals() and has same hashcode in Java
The frequencySet() is calculating frequency of each character of a String in an Integer[] wrapped into Counter class which has overriden equals and hashcode. This method is supposed to return only ...
6
votes
2
answers
3k
views
Convert Set of Set to List of List in Java
List<List<Integer>> lst = new ArrayList<List<>>();
Suppose I have list of list declared above with name as lst and I have a set of set elements variable declared below. How ...
1
vote
1
answer
371
views
Why does javascript return Set elements in not in sorted order?
I wanted to use a set in javascript and I found in docs we have Set in js.
But when I tried to use it, it doesn't returns the elements in sorted order.
let mySet1 = new Set()
mySet1.add(1) /...
0
votes
1
answer
2k
views
Finding the first unique integer in int[]
Given an integer array, I want to return the first unique element in the array. I used A List .contains() method to check if Integer array contains the element, the method is correct but not efficient(...
2
votes
4
answers
103
views
Why are the items in the HashSet always displayed in the same order?
I've created two sets: HashSet and unmodifiable set. Both types of sets do not guarantee the order of the elements. But I have noticed that in case of hashset the result is always the same:
@Test
...
1
vote
1
answer
850
views
Java HashSet remove item by using its hash code [closed]
I have a class with its own hashCode() method. I am adding this class to a HashSet. How can I remove an item by its hashCode, without knowing the object itself?
For example, if I have the following ...
0
votes
0
answers
156
views
Java HashSet remove item by its hash code [duplicate]
I have a class with its own hashCode() method. I am adding this class to a HashSet. How can I remove an item by its hashCode, without knowing the object itself?
For example, if I have the following ...
-1
votes
1
answer
45
views
how do java set judge the similar ListNode
when I'm solving the problem Intersection of Two Linked Lists from Leetcode, I found that the HashSet solution doesn't work on my computer.
The return result is always null, but it works well on ...
2
votes
2
answers
147
views
Converting a Set<String> to List<Set<String>> with specific conditions using stream()
I have a set let's suppose with 10 unique elements
Set<String> myset =new HashSet<String>();
[a,b,c,d,e,f,g,h,i,j]
I wish to construct a List<Set<String>> such that each ...
0
votes
2
answers
102
views
HashSet not returning expected output
I want to store unique lists, so I am using HashSet. But, I am not getting desired output. Here is my code, Could you tell me what is going wrong?
public List<List<Integer>> threeSum(int[]...
0
votes
0
answers
61
views
Iterating HashSet of size 10 takes 7-8 seconds
I have an JPA entity A which has a many to one relation with JPA entity B. In the A.java class I have a @OneToMany mapping to B and a private HashSet data member.
When I fetch all entities A from the ...
0
votes
1
answer
184
views
Merge two arrays in java without using extra space
I have used Set to merge the array as it doesn't contain the duplicate value. I have tried it but after printing the hash set it must be in increasing order but it's not. please help me out
public ...
1
vote
1
answer
21
views
How this test can be fixed?
I am beginner to java and testing. I am getting error with the test @shouldAddOrderToTheList() :
Expected :5
Actual :1.
Could you please point to me what is wrong and how it can be fixed?
class ...
1
vote
3
answers
1k
views
What is the difference between type casting a set to HashSet and initializing a HashSet with a set?
I am looking to convert a set values (from Collectors.toSet()) into a HashSet, what is the difference between these two and which is generally preferred?
HashSet<Integer> set = new HashSet<...
0
votes
1
answer
144
views
Iterating over a Hashset of custom objects throws ClassCastException
This is my abstract class which contains the keysAsSet() method implemented from the interface Readablemap
public abstract class AbstractReadableMap<K, V> implements ReadableMap {
protected ...
0
votes
1
answer
877
views
Java Set - remove element after updating
I have following code:
Set<A> aSet = new HashSet<>();
A a1 = new A(); //{1,"1"}
a1.x = 1;
a1.y = "1";
A a2 = new A(); //{1,"2"}
a2.x = 1;
a2.y = "2";...
4
votes
2
answers
444
views
Equals method is not working for two same object
I have written a code to find unique Images. Two images are equal if they have the same name (even if the extension is different) and the size is equal (width * length).
But It's failing to find ...
2
votes
1
answer
1k
views
Java map Set to another Set (Using Model Mapper or Any other way)
In the project currently I'm working on, has a Project class,
public class project {
private Integer projectId;
private String projectName;
//getters & Setters
}
This project is using as a ...
0
votes
1
answer
666
views
Question about set in java, how does Set works to distinct the duplicate elements?
Can anyone please explain to me how Set works in java? Code as following:
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
...
0
votes
0
answers
463
views
What does this line mean ? Set<ListNode> nodesSeen = new HashSet<>(); in Java [duplicate]
I am trying to understand what's this line doing:
Set<ListNode> nodesSeen = new HashSet<>();
This is from a solution to check if we have a cyclic linkedList or not.
Could someone help me ...
1
vote
1
answer
137
views
Ambiguity in a CodeForces Problem - usage of HashSet Vs LinkedHashSet
I was solving a Codeforces problem yesterday. The problem's URL is this
I will just explain the question in short below.
Given a binary string, divide it into a minimum number of subsequences
in such ...
3
votes
2
answers
2k
views
Kotlin: Iterator in custom order
I need to iterate through a list of sets in a custom order, namely with ascending cardinality. Can a iterator with custom order be created in Kotlin?
0
votes
1
answer
50
views
Why does HashSet<Integer> store the integer value itself, but HashSet<Demo> stores the reference of the Demo object?
Hi All,
class Demo{
private int a;
Demo(int a){
this.a = a;
}
}
public class Test{
public static void main(String[] args){
Demo d1 = new Demo(10);
...
0
votes
3
answers
615
views
What is the return type of toSet in Scala
Suppose I have a Scala array arr of Int. Consider arr.toSet. I know that toSet returns a Set object. Is this Set a HashSet, or TreeSet in default?
3
votes
2
answers
2k
views
How can you correctly create and modify a hashset of tuples in powershell?
I'm trying to make a hashset of 2-element (pair) tuples in PowerShell thus:
$MySet = New-Object System.Collections.Generic.HashSet[System.Tuple]
Which seems to work:
$MySet.GetType()
IsPublic ...
0
votes
1
answer
139
views
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 ...
2
votes
1
answer
118
views
Removing an element while using multiple iterators on a Java HashSet
I am trying to update a set of Sets based on sharing one or more common elements, when two elements have a (or more) common element, they will be combined to a single element and so on.
For example:
...
0
votes
1
answer
27
views
What does Set<Integer>[] 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.
1
vote
0
answers
191
views
HashSet contains not working for Set of Long in Java only for some specific values?
I am trying to execute the below code :
for (Map.Entry<Long, List<FieldBO>> sectionField : sectionFieldMap.entrySet()) {
if (secIds.contains(Long.valueOf(sectionField.getKey()))) {
/////...
1
vote
2
answers
417
views
Why is set.contains not using overriden equals() method?
I have a class like this:
class Vertex {
char v;
char w;
int cost;
public Vertex(char v, char w, int cost){
this.v = v;
this.w = w;
this.cost = cost;
}
...
2
votes
4
answers
753
views
How to get the last element of a HashSet? [duplicate]
I want to get last element of HashSet.
So there is a last() method in TreeSet so, I thought to convert set to TreeSet and apply last() method on it so that I will get last element.
I tried below code :...
-1
votes
1
answer
137
views
Why i can't get an elements from Set by using For Loop? [duplicate]
I have a problem in my mind, why i can't get an element from Set by using normal For Loop, but when i use Enhanced For Loop all things work normal.
Example :
private static Set<HeavenlyBody> ...
0
votes
2
answers
1k
views
Distinct items List, Set, HashSet, Lambda, Java 8
I have three alternatives in order to obtain Distinct items in a List
return new ArrayList<>(new HashSet<>(someTypeList));
return new HashSet<>(someTypeList).stream().collect(...