hashmap

Hash Map with LinkedList in java

旧巷老猫 提交于 2019-12-12 17:41:49
问题 Is there a built in implementation in java for hash map whose values are linked lists? like, if I put: map.put(1, "A"); map.put(1, "B"); then it automatically add A and B to the linked list. When I retrieve from the map, as: map.get(1) I get back a list containing both of them? 回答1: Java does not have it but you can use MultiMap from Google Guava. A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but

NegativeArraySizeException on a HashMap

依然范特西╮ 提交于 2019-12-12 15:04:28
问题 For some reason, my program suddenly throws a NegativeArraySizeException after running for a while. The code that throws it is behind a command, which I've entered before the exception is thrown. The code I'm using is mostly for debug purposes, and is the following: final HashMap<String, Integer> busy = new HashMap<>(); //this map gets filled and emptied in threads System.out.println("Busy tables: " + Arrays.toString(this.busy.keySet().toArray())); System.out.println("Time busy: " + Arrays

Android how to view long string in logcat

怎甘沉沦 提交于 2019-12-12 13:58:51
问题 I have a HashMap<String, LinkedHashMap<String,String> that is fairly long (not an issue) and I'm trying to make sure things look correct before I use the data inside it. To do this I'm trying to just attempting to do this Log.v("productsFromDB",products.toString()) but in the LogCat It shows about 1/3 of it. Is there a way to output the entire map? 回答1: Logcat can only show about 4000 characters. So you need to call a recursive function to see the entire hashmap. Try this function: public

Storing values with duplicate keys in TreeMap, HashMap, or LinkedHashMap

心不动则不痛 提交于 2019-12-12 13:20:30
问题 I am currently working on a project in which I am retrieving data about names from the Social Security website. Basically I'm given a number x, and years y and z. I have to return the top x names from each of the years y through z. So the data returned from the website is a name, a rank, and a year. I have to enter each name returned into either a TreeMap, HashMap, or LinkedHashMap, but I'm not sure how to store them, because no matter what I use as the key, there might be duplicates. The

find closest point from users coordinates

◇◆丶佛笑我妖孽 提交于 2019-12-12 13:09:42
问题 I would like to know how to use find the closes point using coordinates. i am using a Hashmap which holds coordinates and strings. i have allowed the user to input an x and y axis and store them as int a and int b but i don't know where to go from there. thanks for looking import java.util.HashMap; import java.util.Scanner; public class Coordinate { static class Coords { int x; int y; public boolean equals(Object o) { Coords c = (Coords) o; return c.x == x && c.y == y; } public Coords(int x,

Inserting objects into hash table (C++)

为君一笑 提交于 2019-12-12 13:01:53
问题 This is my first time making a hash table. I'm trying to associate strings (the keys) with pointers to objects (the data) of class Strain. // Simulation.h #include <ext/hash_map> using namespace __gnu_cxx; struct eqstr { bool operator()(const char * s1, const char * s2) const { return strcmp(s1, s2) == 0; } }; ... hash_map< const char *, Strain *, hash< const char * >, struct eqstr > liveStrainTable; In the Simulation.cpp file, I attempt to initialize the table: string MRCA; for ( int b = 0;

HashMap底层源码

China☆狼群 提交于 2019-12-12 12:39:15
​ 这里是修真院后端小课堂,每篇分享文从 本篇分享的是:【HashMap 】 (1)背景介绍: 不讲HashMap的使用方法,看一看底层的源码是什么? 思考:HashMap使用key,·value进行存储,使用的数据结构是什么? 我们知道数组和链表两种数据结构 数组: 优点:查询速度快 缺点:增加和删除慢 链表: 优点:增加和删除快 缺点:查询速度慢 那我们可不可以将两者的优点结合一下,达到查询、增加、删除效率都非常快呢? 所以我们猜测一下,HashMap能否源码底层数据结构是采用的链表+数组的形式呢? (2)知识剖析: 1.HashMap数据底层具体存储的是什么? Java是一门面向对象开发的语言,可以把所有东西可以看做是对象。 通过查看源码可知:map里面的key和value都保存在了这个Node对象里面。 class Node<K,V>{ private K key; //用来定位数组索引位置 private V value; private Node<K,V> next; //链表的下一个node } 2.数组怎么表示? transient Node< K,V>[] table; 每个列表被称为桶,即哈希桶数组,是一个Node的数组。 HashMap使用哈希表进行存储。怎样得到表中对象的索引位置? key.hashCode( )----->hashCode-----

Sorting by values in HashMap class using Java

烂漫一生 提交于 2019-12-12 12:13:24
问题 I'm trying to get results HashMap sorted by value. This is HashMap's keys and values: map.put("ertu", 5); map.put("burak", 4); map.put("selin", 2); map.put("can", 1); I try to get results like this: 1 = can 2 = selin 4 = burak 5 = ertu Here is my code: import java.util.*; public class mapTers { public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("ertu", 5); map.put("burak", 4); map.put("selin", 2); map.put("can", 1); Integer dizi[] =

Constructor in a class of static methods

倖福魔咒の 提交于 2019-12-12 12:12:01
问题 I've got a class of static methods that can be performed on a map held within the class, and I want the map to be set up when the class is called. I've tried using a private contructor, but it isn't being called. The relevant parts of my code are: public class MyClass { private static final String KEYS = "ABC"; private static final String[] DATA = {"AAA", "BBB", "CCC"}; private static HashMap<Character, String> myMap; private MyClass() { System.out.println("Running constructor");

“Transpose” a hashmap for key->value to value->key?

雨燕双飞 提交于 2019-12-12 12:07:19
问题 Say I have a map of key -> value pairs, I want to reverse this so that I have a new map which is effectively value -> key (i.e. the old value becomes the new key and the old key becomes the new value). Whats the best way to do this? (I am using Java...). Oh and values are unique. 回答1: Personally I'd use a Guava BiMap to start with (with an implementation such as HashBiMap) and then call inverse() when I wanted to use the values as keys :) 回答2: I think there are enough solutions for your