Category "hashmap"

How to create a matrix with key and value in java

I have this table with these values I have this code in java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

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 attr

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

How to create a unique key for a map

I'm creating a structure where a developer can store a reference to something and retrieve it when needed using a reference key, but not delete the reference. H

Maximum Frequency Number in an array using Hashmaps

Problem: You are given an array of integers that contain numbers in random order. Write a program to find and return the number which occurs the maximum

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 o

hashCode values not same in debugger and output in netbeans 8.2

Map<String, Integer> map = new HashMap<>(); map.put("Naveen", 100); System.out.println("Naveen".hashCode()); /* output (-1968696341)

Get list of keys in Complex JSON Object (Java 8)

I am dealing with a JSON that looks like this :- { "key1": { "key1.1": { "nestedkey1": "something", "nestedkey2": "something", "nest

How to convert List to Map?

Recently I have conversation with a colleague about what would be the optimal way to convert List to Map in Java and if there any specific benefits of doing so.

Average test score from result array with varying group sizes

I am working on a problem where I have to calculate the average from a bunch of test results with the following criteria. -his program was tested on several tes

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(Co

How to merge multiple Maps having the same keys but different values into a single Map

I have n maps of the kind: HashMap<String,Double> map1; HashMap<String,Double> map2; ... What could I do to merge all these maps into a single map?

How to return the key that I wrote if the key was not found

Let's say we have a HashMap named hsm, and I want the value of hsm.get(invalidKey). Because the invalidKey has no mapping it will return for me a null value, bu

Updating multiple values of different keys in map using java

I have converted a JSON file to a map and I need to modify multiple values of different keys at once using JAVA. Following is the map converted JSON file: {

Laravel Eloquent get() indexed by primary key id

I often find it very useful to index my results by the primary key id. Example: $out = []; $users = User::where('created_at', '>=', '2015-01-01')->get(

Can we use object as a key in hashmap in Java?

How to use an object as a key in a hashmap. If you use an object as key do you need to override equals and hashcode methods for that object?

Delete top or bottom n elements of a map

I have a HashMap like below: map.put("1","One"); \\KV1 map.put("3","Three"); \\KV2 map.put("2","Two"); \\KV3 map.put("5","Five"); \\KV4 map.put("4","F

Implementing Priority queue using hashmap

This may be a very naive question or does not make sense. But i am really confused about implementation of priority queue. Why we cannot use a hashmap with key

Recursive iteration of a Map Java

I am writing a recursive function whose purpose is to iterate over the pList File. My code is public static void HashMapper(Map lhm1) throws ParseException {

How can I use a HashMap with f64 as key in Rust?

I want to use a HashMap<f64, f64>, for saving the distances of a point with known x and key y to another point. f64 as value shouldn't matter here, the fo