15,322 questions
1
vote
0
answers
135
views
Optimized Solution for Power Pokemon Problem
I am trying to solve the following Question:
A Power Pokemon Couple is a pair of pokemons with a sum of their power levels equal to a power of two.
You are given an array power, containing the power ...
-2
votes
3
answers
173
views
Time complexity of adding elements to collection vs using stream in java
I was asked this question in an interview to count the number 1 and 0 in a binary array
eg: arr = 1, 1, 0, 1, 0, 1, 0, 0, 0. We should not use Array.sort or Collections.sort function.
I could think ...
2
votes
3
answers
136
views
Looping through a HashMap using iterator does not stop for an if condition
A HashMap<Integer, Integer> has 10 entries but I only want to print 3 entries.
Code:
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
hm.put(2, 1);
hm.put(5, 3);
hm.put(...
0
votes
2
answers
62
views
Transform a <string, List<string>> map to change values to be as keys
I have a following map, which contains basket number to list of fruits in that basket.
val basketMap = mapOf("basket1" to listOf("apples", "oranges", "grapes"),
...
-1
votes
1
answer
25
views
Improving hashmap query performance when input query doesnt match the data
Consider this data stored in a hashmap:
VIN last 4
License plate
Details
1234
VehicleObject1
2222
ABC123
VehicleObject2
5678
ABC123
VehicleObject3
5678
XYZ123
VehicleObject4
class VehicleKey{
String ...
0
votes
1
answer
52
views
How to avoid Cuda race condition, for parallel comparison of values in a hash map
I have a problem where in a 2D space (x,y) I have a lot of points with different sizes. An array with the struct (x,y, size) represents this space. I want to downsample this space to some specific ...
0
votes
0
answers
82
views
Hash functions which avoid collisions on a fixed set of inputs
I have a space of size ~238 from which I sample a set of ~228 distinct objects (their distribution is structured but hard to characterize). I need a procedure for taking one such sample and ...
1
vote
2
answers
111
views
Convert "Object" to "HashMap" in Java
Here is a example:
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap<Integer, Integer> a = new HashMap<Integer, Integer>();
a.put(...
0
votes
4
answers
136
views
Why are duplicate keys printed when I'm looping through the hashmap? [closed]
What is wrong with my code? I'm trying to print the keys but when I print the I'm getting duplicate keys. I was under the impression that when we add duplicate keys to hashmap, it replaces the ...
0
votes
1
answer
67
views
How to remove the error kotlin.UninitializedPropertyAccessException: lateinit property?
I am having an adapter class and there is an instance named 'imageResources' I have declared it lateinit and later I even made it sure that it is given. a value but still I am getting this error ....
0
votes
2
answers
127
views
Create a Java StringBuffer from a Map and Java Object
I am trying to create a csv file with the headers coming from a map and the values coming from an object. I have a Java object which stores different attribute data.
public class MyObject {
...
-1
votes
2
answers
63
views
Found_HashMap_DistinctPoints
Given N 2D Points, Calculate no. of distinct points among them.
Ex: x[5] = {2, 1, 3, 2, 2}
y[5] = {3, 1, 2, 3, 4}
The first array represents the x co-ordinates, the second array represents the y ...
4
votes
1
answer
119
views
Distributed Hash Tables: Preventing nodes from storing petabytes of data?
I am reading up on distributed hash tables and Kademlia. But I have one pretty big question that I haven't seen answered.
Nodes can broadcast their files by hashing the filename and sending it off to ...
0
votes
1
answer
59
views
How to set Hashmap equal to its original values?
I have a HashMap<Character, Integer> that counts the frequencies of letters in a string, called freq. For example, if the word is PUPPY, then the map holds the values {P=3, U=1, Y=1}.
I am using ...
1
vote
1
answer
233
views
Java HashMap retainAll time complexity
I tested HashMap retainAll method to test which one is faster with JMH.
Two HashMap A, B
Size : A > B, A : 300~400, B : 100~200
Most of B elements is in A, so B - A is like 10~20, very small
Test ...
2
votes
3
answers
113
views
How to split a line in CSV with multiple delimiters into different hashes?
File zone.csv looks like below:
===========================================================
zone0,primaryserver1,primaryserver2,secondaryserver1|secondaryserver2
zone1,primaryserver1,primaryserver2,...
0
votes
2
answers
160
views
Why isn't hashmap insertion `O(1)` worst-case when separate chaining is used?
I have learned how a hashmap is implemented: an array of linked lists, if separate chaining is used. I know that when a key-value pair (key, val) is inserted, the hashmap inserts the pair {key, val} ...
-1
votes
4
answers
136
views
Fill an array with matches from a HashMap in Java
It's for a homework assignment so I have to use an Array. I understand using an ArrayList would be the easier solution, but unfortunately I can't.
I have a HashMap of animals. I have to create a ...
0
votes
1
answer
113
views
Java: Fibonacci use HashMap or TreeMap? [closed]
I was asked to implement a Fibonacci program that can be reused again. I used a Hashmap below. Just curious, if I should have actually implemented a TreeMap to help performance? I was looking through ...
-1
votes
1
answer
39
views
How to remove the error 'Create extension property'Any.second' while accessing the pair values declared in a HashMap in kotlin?
I have a Singleton class named ImageStore in which I have declared a Hashmap where value for each is key is a Pair like shown below
object ImageStore {
val imagesNamesMap = hashMapOf(
R....
0
votes
1
answer
86
views
How can I determine which files have changed across multiple git repositories?
Problem
I have created a tool which runs a code quality analysis tool against all repositories in a shared Gitlab group namespace, and outputs the results to a static site.
The tool is structured as ...
2
votes
1
answer
1k
views
Why does compiler say Hash is not implemented when I put a HashMap in my struct? [duplicate]
I'm trying to use a HashMap in a very basic way in my struct and I'm confounded by this error ... what am I missing?
error[E0277]: the trait bound `HashMap<&str, &T>: Hash` is not ...
0
votes
1
answer
81
views
Result of HashMap .get() call references local variable used for lookup
I'm attempting to perform multiple lookups into a HashMap. In the program's main loop, my method works just fine. However, once I try to move the logic into a function to hide some messy ...
0
votes
2
answers
106
views
In which situation is HashMap keySet() ordered undeterministic
The docs https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html say
This class makes no guarantees as to the order of the map;
In particular, it does not guarantee that the order will ...
2
votes
1
answer
1k
views
C++ hash_map find() vs contains() performance
What's the better option performance-wise if I want to check if an element is contained in a map and use it directly afterwards?
std::unordered_map<int, std::string> my_map;
int my_key;
Option ...
0
votes
1
answer
85
views
How do I sort a tree map using a comparator that uses both key and value
So I have this class where I'm trying to initialize a SortedMap using a Comparator I created in another class (separate files)
Why is this part not working?
Comparator<Map.Entry<Country, Pair<...
0
votes
1
answer
114
views
How to change Map keys to Int when parsing CSV?
val source = scala.io.Source.fromFile("src/main/Mapping.csv");
val data_map: Map[String, String] = source.getLines().map(csv=> (csv.split(",")(0),csv.split(",")(1)))....
0
votes
1
answer
96
views
How to import cell data in map list format from app script?
If there is a cell configured like this, the desired result is
{samsung, [Galaxy S20, Galaxy S21, Galaxy S22, Galaxy S23]}, {apple, [iPhone 13]}, {lg,[g7, g8]}
In this way, I would like to obtain ...
0
votes
0
answers
79
views
function breaks ranged-based for
Somehow, in the ranged-based for loop in findLongestPath(), the dfs() function makes it so the loop quits after the first iteration instead of going through every element to pass it to the function ...
0
votes
1
answer
40
views
How should I search items in a nested HashMap with inner key?
I have a nested HashMap HashMap<String, TreeMap<Integer, Item>>, and now I know the inner key Id(Integer) in TreeMap but don't have the key String in HashMap. Could I use some build-in ...
5
votes
1
answer
86
views
Why do HashMap::get and HashMap::entry take different types of key?
The signature of HashMap::entry is:
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
OK, looks good. I want to take an entry from the HashMap whose key is K, so of course I need to ...
0
votes
1
answer
114
views
Time Complexity of accessing [class variables,functions] vs [dictionary values,keys] in Python3
I implemented trie by using classes and by dictionaries separately and got different runtimes.Trie implementation with dictionaries worked about 2.5 times faster than trie with classes when input size ...
1
vote
2
answers
66
views
Merging two hashmaps with their values combined as object of specific class
I have a class like
public class Values {
BigDecimal a;
BigDecimal b;
}
And two maps with the same keys like
Map<String, Double> map1 = Map.of("A", 123.1, "B", 345....
0
votes
1
answer
211
views
How to initialize a HashMap whose values are functions that return trait objects from functions that return concretes types?
I have the following Rust toy code, which is building a global HashMap with closures that return a Struct that is implementing a Trait (please excuse the silly example)
pub trait Animal {
fn ...
1
vote
0
answers
93
views
Changing ownership back and forth
I'm new to Rust and I'm still struggling with borrowing and move concepts.
To set the context, I'm using a library (namely MVT ) that uses following construct to create a layer:
let mut tile = ...
0
votes
1
answer
155
views
split String twice and put it in a HashMap
I have a String atp.Status="draft";pureMM.maxOccurs="1";pureMM.minOccurs="1";xml.attribute="true" and want to split it twice and insert it into a HashMap. For ...
0
votes
1
answer
61
views
Java Streams to find elements in a list of mapLists
Can you help me converting the below logic to make use of streams. This works fine but i want to use streams instead of this
List<Map<String, Object>> list1=new ArrayList<>();
List&...
0
votes
1
answer
124
views
Bucket definition in HashMap
I'm trying to understand how a bucket(index) is determined in a hashmap by the key and I've read 3 ways to determine it, but there can't be 3.
1) index = hashCode(key) & (n-1)
2) index = hashcode %...
0
votes
1
answer
104
views
How to add as many instances of a Pane defined in a separate fxml file as there are entries in a HashMap into a ScrollPane?
The Pane (pane - fxml: deadline-example.fxml) has elements (Labels & a pie chart) that are set based on a specific instance of the Deadline class. I also have another scene (deadline-menu.fxml) ...
2
votes
2
answers
793
views
Can Rust use a HashMap as a key in another HashMap? [duplicate]
Example:
let mut test: HashMap<HashMap<String, String>, String> = HashMap::new();
test.insert(HashMap::from([("test", "test")]), "test");
The compiler doesn'...
1
vote
1
answer
187
views
How to search in HashMap with a key that was potentially typo'd?
I'm looking for a way to lookup a word where the user is not smart enough to spell it correctly. I have already handled some cases (like capitalizing the word so it match the string expected on the ...
0
votes
2
answers
188
views
Iterate through Vec<&str> and accumulate value to a variable when condition is fullfilled
My current attempt:
let fruits:Vec<&str> = vec!["Banana", "Peach"];
let fruits_hashmap: HashMap<_, _> = HashMap::from_iter([("Apple", 2),("Banana&...
-2
votes
2
answers
62
views
Resize in custom HashMap - test timed out
I am implementing hashmap from scratch.
I have my constructor:
public MyHashMap(int initialCapacity) {
resizeFactor = 2;
loadFactor = 0.75;
bucketSize = initialCapacity;
...
0
votes
2
answers
72
views
Merge two map with different types
I hope all is good for you.
I have two maps Map<Type1, Type2> and a Map<Type3, Type1>. I want to merge them and have at the end a Map<Type3, Type2>.
How can I do that? I make this ...
1
vote
2
answers
90
views
Why is my hashmap clearing some of its values?
I am trying to create a map for a tree. It maps the column number (horizontal depth) to the list of nodes with that horizontal depth.
When I use the below code, somehow the map gets overwritten by the ...
1
vote
1
answer
107
views
In Java, during Object#equals() call, do both objects necessarily have same hashCode ? Do we need to ever compare hashCode within equals()?
I have read somewhere, in context of HashMap :
hashCode() allows sorting objects by their hash values, and then the Object#equals method only needs to be invoked when objects share the same hash value....
1
vote
2
answers
97
views
How do I correct my Sorter methods to allow for more user input?
I am trying to build a sorting algorithm for a few friends of mine, and I am making a test build for it right now. Eventually I will have it more dynamic, but currently I just have it to where it ...
0
votes
1
answer
77
views
GO, Map keys are being all updated
Is there something wrong in what I'm doing? I don't understand why the all the map keys are being updated, it should be only the 'ether' key.
data := []byte(`
[{".id":"*1",&...
1
vote
3
answers
713
views
Python's collection.Counter equivalent in C# or .NET
While solving problems in Python I often had to use Python's collections.Counter object.
When I started solving the same problems in C# I couldn't find an equivalent method or class for it.
I was just ...
1
vote
2
answers
105
views
Two different HashMap returning the same hashCode
I have two different sets of HashMap ({u=0, h=3} and {t=3, i=0}), and it is returning the same hashCode (224). I don't understand this, hashCode needs to be different for different objects, and here ...