如果LinkedHashMap的时间复杂度与HashMap的复杂性相同,为什么我们需要HashMap?与Java中的HashMap相比,LinkedHashMap的额外开销是多少?
我把一个字符串数组元素是一个映射,其中字符串数组的元素是键,字的频率是值,例如:
String[] args = {"if","it","is","to","be","it","is","up","me","to","delegate"};
Run Code Online (Sandbox Code Playgroud)
然后地图会有像这样的条目 [ if:1, it:2 .... ]
Set<String> keys = m.keySet();
System.out.println("keyset of the map : "+keys);
Run Code Online (Sandbox Code Playgroud)
打印所有键: "if","it","is","to","be","it","is","up","me","to","delegate"
Set<Map.Entry<String, Integer>> entrySet = m.entrySet();
Iterator<Map.Entry<String, Integer>> i = entrySet.iterator();
while(i.hasNext()){
Map.Entry<String, Integer> element = i.next();
System.out.println("Key: "+element.getKey()+" ,value: "+element.getValue());
}
Run Code Online (Sandbox Code Playgroud)
打印所有键值对:
使用条目集打印所有值:
Key: if ,value: 1
Key: it ,value: 2
Key: is ,value: 2
Key: to ,value: 2
Key: be ,value: 1
Key: up ,value: 1
Key: me ,value: 1
Key: delegate ,value: 1
Run Code Online (Sandbox Code Playgroud)
但是下面的代码块应该打印与上面完全相同的输出,但它不会:
Iterator<String> …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个包含名称/年龄(字符串/整数)对的LinkedHashMap(称为info).我想知道,如果我输入密钥,如何获得键/值的位置.例如,如果我的LinkedHashMap看起来像这样{bob = 12,jeremy = 42,carly = 21}并且我要搜索jeremy,它应该返回1作为它的位置1.我希望我可以使用类似info.getIndex的东西( "杰里米")
String s = "m\\"+"/m\\/m/m/m/m/m";
LinkedHashMap<String, String> hm = new LinkedHashMap<>();
hm.put("test", s);
System.out.println(hm+" Hash map = "+hm.toString());
Run Code Online (Sandbox Code Playgroud)
精细输出是 {test=m\/m\/m/m/m/m/m} Hash map = {test=m\/m\/m/m/m/m/m}
String s2 = new Gson().toJson(hm.toString());
System.out.println("Json result is "+s2);
Run Code Online (Sandbox Code Playgroud)
输出不好Json result is "{test\u003dm\\/m\\/m/m/m/m/m}"
是GSON要疯了还是我做错了什么吗?反斜杠发生了什么,从哪里u003d出现?我知道很久以前就存在这种性质的错误,但它已经解决了.我该如何解决这个问题?提前致谢.
遍历功能HashMap和LinkedHashMap遍历values()功能之间是否存在性能差异?
我有一个问题类似于stackoverflow上的一些问题,但没有一个真正回答我的问题.我使用ObjectMapperJackson并希望将此JSON字符串解析为User对象列表:
[{ "user" : "Tom", "role" : "READER" },
{ "user" : "Agnes", "role" : "MEMBER" }]
Run Code Online (Sandbox Code Playgroud)
我定义了一个这样的内部类:
public class UserRole {
private String user
private String role;
public void setUser(String user) {
this.user = user;
}
public void setRole(String role) {
this.role = role;
}
public String getUser() {
return user;
}
public String getRole() {
return role;
}
}
Run Code Online (Sandbox Code Playgroud)
要将JSON字符串解析为UserRoles我使用泛型的List :
protected <T> List<T> mapJsonToObjectList(String json) throws Exception {
List<T> list;
try {
list …Run Code Online (Sandbox Code Playgroud) 我读到HashMap具有以下实现:
main array
?
[Entry] ? Entry ? Entry ? linked-list implementation
[Entry]
[Entry] ? Entry
[Entry]
[null ]
Run Code Online (Sandbox Code Playgroud)
因此,它有一个Entry对象数组.
问题:
我想知道如果相同的hashCode但不同的对象,这个数组的索引如何存储多个Entry对象.
这与LinkedHashMap实施有何不同?它是map的双链表实现,但它是否像上面那样维护一个数组,它如何存储指向下一个和前一个元素的指针?
我有一个哈希表.values()方法以某种顺序返回值,这些顺序与我插入的顺序不同.如何以与插入时相同的顺序获取值?使用LinkedHashmap是一种替代方法,但它不是同步的.
我想ImmutableLinkedHashMap<>在Guava库中找到类似的东西.我需要使用带有插入顺序的不可变键值数据结构.那么,我该怎么用?
我试图使用LinkedHashMap实现LRU缓存.在LinkedHashMap(http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html)的文档中,它说:
请注意,如果将键重新插入地图,则插入顺序不会受到影响.
但是,当我做以下投入时
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private int size;
public static void main(String[] args) {
LRUCache<Integer, Integer> cache = LRUCache.newInstance(2);
cache.put(1, 1);
cache.put(2, 2);
cache.put(1, 1);
cache.put(3, 3);
System.out.println(cache);
}
private LRUCache(int size) {
super(size, 0.75f, true);
this.size = size;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > size;
}
public static <K, V> LRUCache<K, V> newInstance(int size) {
return new LRUCache<K, V>(size);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
{1=1, 3=3}
Run Code Online (Sandbox Code Playgroud)
这表明重新插入确实影响了订单.有人知道任何解释吗?
java ×10
linkedhashmap ×10
hashmap ×4
collections ×2
generic-list ×1
generics ×1
gson ×1
guava ×1
hashtable ×1
immutability ×1
insert ×1
jackson ×1
keyset ×1
lru ×1