Questions tagged [hashmap]
A data structure that uses a hash function to map identifying values, known as keys, to their associated values
14,544
questions
0
votes
0
answers
31
views
How do I format my HashMap and access specific elements?
I have a .json file in my assets directory that looks like that:
{
"2": {
"3 darts hint": "D1",
"2 darts hint": "D1",
"1 ...
0
votes
1
answer
25
views
HashMap Problem: Entries missing after filling
Hello and thank you for reading. I've got a problem with a Hashmap in Java. The HashMap is thought to replace a2D array, so that the array index pair is used to caluculate the key. The map is filled ...
0
votes
0
answers
26
views
HashMap Solution for finding multiples of 50 (Java) [closed]
If you're given a list of random numbers and the solution has to use HashMap to find multiples of 50 from that list, what would the code look like?
I'm new to learning Java and data structures like ...
0
votes
2
answers
30
views
Adding to list from hashmap key kotlin
I'm practicing leetcode problems to perfect my kotlin syntax and am wondering why this code doesn't work. My question specifically is why doesn't my courses hashmap populate with this code.
...
-3
votes
1
answer
48
views
How to implement thread-safe map of maps in golang?
I am working on a multi-threaded module and need to implement map of map in golang - map[outer]map[inner]*some_struct. The outer key(map[outer]) will be accessed by multiple threads(goroutines) to add ...
0
votes
0
answers
8
views
How to count words from a list of String without using Stream and returning a map result?
I want the count the number of words from the list words but without using stream.
I want to refactor map computation with merge method and replace for loop with forEach method and eventually then use ...
3
votes
3
answers
103
views
How to increase HashMap value using merge method in Java?
I have the following list and use LinkedHashMap.
I want to increase the value of the key by 1 (if the key is not present in the map, it starts from 0, and I add +1):
int nums[] = new int[]{4, 10, 5, 4,...
2
votes
1
answer
36
views
Get first LinkedHashMap key based on value in Java
I use the following LinkedHashMap and get the occurences of numbers as <number, occurences>.
Map<Integer, Integer> map = new LinkedHashMap<>();
The values stored in the map are as ...
0
votes
2
answers
40
views
Why doesn't my custom object Java HashMap handle unique keys? [duplicate]
Below code returns false. Trying to make Equation object act as a key in a custom HashMap. Pretty sure method overrides are implemented correctly, hashCode is the exact same between the two equation ...
0
votes
1
answer
42
views
C++ Struct of array global scope
I need a struct of array[lenght] to be seen by all my methods(global).
The problem I have is that the struct needs to be initialized with a specific length inside a specific function. To be more ...
-1
votes
1
answer
31
views
randomize items in an array and keep original order
I would like to randomize the email addresses that are being output and remove duplicates and have them retain the original order. This works perfectly fine when I do not randomize. I generate the ...
3
votes
4
answers
95
views
Collect the data from both Nested List and Outer List with Java Streams
Class Package{
Long packageId;
String packageName;
List<Service> serviceList;
}
Class Service{
Long serviceId;
String location;
}
List <Package> packageList
Above there are my ...
3
votes
2
answers
48
views
How to properly use Enums for simple price calculation (Java)
I am trying to optimize a code-snippet that looks like this:
public class Kvarteret {
public static final String ONE_BEER = "hansa";
public static final String ONE_CIDER = "...
1
vote
2
answers
58
views
How to check if all Keys of the given Map are present in a one of the Maps contained within a List<Map<String, Object>> in Java efficiently?
I have a list of maps List<Map<String, Object>>.
Now I need to check whether a map exists in this list or not.
The list looks like:
List<Map<String, Object>> contentList = new ...
1
vote
3
answers
65
views
How to generate a Map based on a List of objects containing a Map as and attribute
I'm getting familiar with the Stream API and facing some problems.
Here is my code.
class Student {
String name;
int height;
LocalDate dob;
Map<String, Integer> scores;
...
-1
votes
1
answer
39
views
How to update value of a map<int,vector<int>> in c++? [closed]
Given an array ar[]={10, 8, 3, 1, 3, 8, 2, 5, 8, 7};
The array is taken to be 1-based index.
Aim is to create a map, which stores all the indices of a particular element in a vector and hence the data ...
-3
votes
2
answers
21
views
my java program is giving error ar .put() method The method put(String, Student) is undefined for the type HashMap [duplicate]
import java.util.*;
public class HashMap {
public static void main(String[] args){
HashMap h=new HashMap();
Student s1= new Student("Anees",84);
Student s2= new Student(&...
0
votes
0
answers
10
views
Problem when clearing ArrayList after adding it as value to HashMap
I have one short question:
I have HashMap where key is String type, and value is ArrayList type.
Now, I have following piece of code:
putInMap(//some string for key, list);
list.clear();
Now, what is ...
0
votes
1
answer
51
views
How to implement a global and mutable HashMap with str as key and Trait as values?
I am trying to implement value key pairs whose key would be a string and the value would be an object type that implements some predefined functions. The problem I encounter is that a Trait (which I ...
0
votes
0
answers
51
views
Redis hashmap bucket strategy to store ~32M keys in a single instance
I am looking to verify if my redis hashmap bucket/slot strategy is reasonable to store around ~32 million keys to take advantage of redis hashmap encoding optimization (ziplist). Assuming that the ...
0
votes
0
answers
74
views
Static HashMap giving incorrect result
I have implemented program to find unique permutations of elements in input list. I have used backtracking and I am using map to prevent repetitive recursion.
Initially, first entry in map is: Key=&...
0
votes
2
answers
55
views
Hashmap - cannot infer arguments
Hey :) This is my first post and my first approach at streams! I am trying to learn it by myself, but since its a new style of programming Im having a tough time :D
So here's my problem:
In the ...
1
vote
3
answers
54
views
How to remove the empty objects and null values present in nested complex LinkedHashMap?
I have a complex nested LinkedHashMap for which I am assigning the values coming from another application. Some of the fields can have Null values since the values may be missing in input JSON.
I am ...
-2
votes
0
answers
25
views
I have a this List<Map<String, Object>> data need to sort this by nearLocation. this is possible
{
"data": [
{
"id": "ad4a6748-3d77-4eb8-9aa4-b7c11eff3df4",
"host_id": "fdd7732e-578f-4135-a644-749f2a567d55",
"...
0
votes
1
answer
32
views
Java 11 handle unmodifiable map
I have a class to process some files which is uploaded zipped.
And a method to unzip and fill a HashMap and convert to an Collection.unmodifiableMap.
public class MyClass extends HttpServlet {
...
...
0
votes
0
answers
40
views
Convert List<Map<String, Object>> to string json
I have a following method which creates list of map and populates it with product code and quantity:
private List<Map<String, Object>> createListOfMap(int count) {
List<Map<...
1
vote
2
answers
32
views
React isn't updating a variable from a function
I'm using axios to return a map to my main app where it will be distributed to other values in the program. I am having an issue though. When I use 'onClick' on a drop down select, I want it to call ...
2
votes
6
answers
105
views
How to iterate over a Map to obtain a String of repeated Character Keys with Java 8
I have a Map shown below:
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
map.put('c', 5);
map.put('f', 2);
map.put('r', 1);
map.put('D', 3);
I need ...
0
votes
0
answers
17
views
How can I avoid a NullPointerException when deserializing a HashMap whose keys/value reference the map?
I've created a graph class that I'd like to make serializable. The graph tracks its nodes in a HashMap. The keys to this map reference nodes, and the nodes reference the graph.
When you deserialize a ...
-1
votes
0
answers
33
views
ConcurrentHashMap locking on value versus no locking for get/remove
I'm seeing code that looks something like this
public static void main(String args[]) throws ExecutionException, InterruptedException
{
final ConcurrentHashMap<String, String> ...
0
votes
1
answer
23
views
How to replace values from a data frame with the values from a hash map/dictionary?
Created a hash from mtcars data frame.
counter <- 0
h <- hash()
for(i in 1:nrow(mtcars)){
for(j in 1:length(names(mtcars))){
counter <- counter + 1
h[[as.character(mtcars[i,j])]] &...
1
vote
1
answer
45
views
Group the contents of a Map of LocalDates into a Map of Ranges of dates
I have a map of working shifts indexed by day Map<LocalDate, Collection<Shift>> shiftsAllDays
such as:
"2020-03-26": [
{
"id": 4,
"startTime": ...
-2
votes
0
answers
45
views
check hashmap contain same value
My input is
51234624: A, B, D, E, F
427613829: B, D, G, H, I
38216542: Z, B, D, J
423212822: AA, B1, C3, D
4126548: J, Z3
98871973331: Q, M, N
and user input
4126548
and the output will show
...
0
votes
2
answers
54
views
JavaScript Leetcode Two Sum Answer explanation please [closed]
I found a solution for this Two Sum on Leetcood that I could not understand:
The question is:
Given an array of integers nums and an integer target, return indices of the two numbers such that they ...
1
vote
0
answers
63
views
ClassCastException in HashSet.put()
In seemingly simple code - as far as it relates to the problem - such as this:
private final HashSet<ValueType> set = new HashSet<>();
...
void doIt(ByteBuffer b) {
set.add(new ValueType(...
1
vote
1
answer
78
views
Ternary operator within Java Streams
I have the following function which utilizes streams and collectors to create a map and differs in execution based on a boolean input parameter isMultiSelectableQuiz:
private Map<Integer, Long> ...
-1
votes
1
answer
34
views
How to extract two values from a json list and save them to a map?
I have a JSON file with a lot of fields. I need to extract two fields to the map: x and y.
I am trying to extract the x field using list.stream().Map(x -> x.get ('x') .....
But I want to join this ...
1
vote
2
answers
54
views
Issue with creating nested map using streams and collectors
class QuizAnswers {
List<CheckboxAnswer> checkBoxAnswers;
}
class CheckboxAnswer {
int questionId;
// The indices of the selected answer choices
List<Integer> answer_selections;
}
...
-3
votes
0
answers
22
views
Bug in Leetcode 76 [duplicate]
When i'm trying leetcode problem 76, at one point I am trying to get a value paired to key in one of the two maps (smap) for that I have used smap.get(curr)(as you can see in the last if condotion), ...
0
votes
2
answers
39
views
Determine the common items between two sorted linked lists with hashmap JAVA
I used a hashtable to find the intersection between the two linked lists. Still, the problem is that I have to give the table size beforehand and if the table size is > intersection elements the ...
2
votes
1
answer
57
views
Java Streams - group-by and return a Nested Map
my data is like this,
unitId time value1 value2
a 2021 10 11
a 2022 15 13
b 2021 20 25
b 2022 30 37
my goal is put every unitId and value into a ...
1
vote
1
answer
104
views
Why is there no collision in a HashMap when different keys have the same hash code
I'm trying to create a collision intentionally.
fun main(args: Array<String>) {
val india = Country("India1", 1000)
val india2 = Country("India2", 1000)
val ...
0
votes
2
answers
36
views
java.util.LinkedHashMap cannot be cast to class com.profile.model.Profile - Spring Boot
I have a microservice architecture where I am trying to call other service via exchange method to get list of users profiles.
@Override
public List<Profile> getListOfProfile(List<String&...
1
vote
2
answers
36
views
How to return a Key from HashMap based on its Value in Java
Consider the following:
for (Boolean i: x.values()) {
if (i == false) {
return // KEY;
}
}
In the above code, I am trying to iterate through the HashMap. And I want to return a Key ...
0
votes
0
answers
30
views
In java map, I want to assign the repository functions as value like Map<key, function(>
I Want to call the JPA repository functions to access the data from the database based on the String value passed.
So I want to make the string as the key and the reposetory function as Value so that ...
2
votes
1
answer
52
views
How to insert records into a Nested HashMap?
I want to create a nested HashMap of:
var myHashMap = new HashMap<String, HashMap<String, int>>();
And I want to insert records into the inner HashMap, I find myself needing to ...
1
vote
4
answers
94
views
How to use Map.computeIfAbsent() in a stream?
I have a fun puzzler. Say I have a list of String values:
["A", "B", "C"]
Then I have to query another system for a Map<User, Long> of users with an attribute that ...
2
votes
5
answers
79
views
Rewrite imperative code of creating and populating Map in a declarative way using Java Stream API
I have this piece of code :
Map<String, BillingAccount> billingAccountsMap = new HashMap<>();
for (BillingAccount ba : billingAccounts) {
if (ba.getBillingAccountIdentifier() != null &...
0
votes
1
answer
57
views
Hashmap values always being overwritten to last looped value
I cannot figure out why my HashMap keeps over writing all of my key/value pairs with the last ip address the method reads. Basically we are meant to store all of the ip addresses of website visitors ...
1
vote
2
answers
77
views
Java 8 Streams - How to get an equivalent of partitioningBy() with Integer keys?
In the code shown below, p is a predicate of type Predicate<String>.
Map<Boolean, List<String>> partitioned = numbers.stream()
.collect(Collectors.partitioningBy(p));
Is it ...