hashmap

史上最全Java集合面试题

╄→尐↘猪︶ㄣ 提交于 2019-12-20 22:37:30
1、你所知道的集合类都有哪些?主要方法? 最常用的集合类是 List 和 Map。 List 的具体实现包括 ArrayList 和 Vector,它们是可变 大小的列表,比较适合构建、存储和操作任何类型对象的元素列表。 List 适用于按数值索 引访问元素的情形。 Map 提供了一个更通用的元素存储方法。 Map 集合类用于存储元素对(称作"键"和"值"), 其中每个键映射到一个值。 ArrayList/Vector List Collection HashSet/TreeSet Set HashTable Treemap/HashMap 我记的不是方法名,而是思想,我知道它们都有增删改查的方法,但这些方法的具体名称, 我记得不是很清楚,对于 set,大概的方法是 add,remove, contains;对于 map,大概的方 法就是 put,remove,contains 等,因为,我只要在 eclispe 下按点操作符,很自然的这些方 法就出来了。我记住的一些思想就是 List 类会有 get(int index)这样的方法,因为它可以按 顺序取元素,而 set 类中没有 get(int index)这样的方法。List 和 set 都可以迭代出所有元素, 迭代时先要得到一个 iterator 对象,所以,set 和 list 类都有一个 iterator 方法,用于返回那

图解LinkedHashMap原理

泄露秘密 提交于 2019-12-20 19:03:15
1 前言 LinkedHashMap继承于HashMap,如果对HashMap原理还不清楚的同学,请先看上一篇: 图解HashMap原理 2 LinkedHashMap使用与实现 先来一张LinkedHashMap的结构图,不要虚,看完文章再来看这个图,就秒懂了,先混个面熟: LinkedHashMap结构.png 2.1 应用场景 HashMap是无序的,当我们希望有顺序地去存储key-value时,就需要使用LinkedHashMap了。 Map < String , String > hashMap = new HashMap < String , String > ( ) ; hashMap . put ( "name1" , "josan1" ) ; hashMap . put ( "name2" , "josan2" ) ; hashMap . put ( "name3" , "josan3" ) ; Set < Entry < String , String >> set = hashMap . entrySet ( ) ; Iterator < Entry < String , String >> iterator = set . iterator ( ) ; while ( iterator . hasNext ( ) ) { Entry entry =

How can I store HashMap<String, ArrayList<String>> inside a list?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 17:52:35
问题 My hashmap stores the string as key and arraylist as the values. Now, I need to embed this into a list. That is, it will be of the following form: List<HashMap<String, ArrayList<String>>> These are the declarations I have used: Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); ArrayList<String> arraylist = new ArrayList<String>(); map.put(key,arraylist); List<String> list = new ArrayList<String>(); Can anyone help me which method and how to use in the list to

Why does the Java compiler not like primitive int as type for values in HashMap?

自古美人都是妖i 提交于 2019-12-20 17:37:44
问题 The compiler complains about this code: HashMap<String,int> userName2ind = new HashMap<String,int>(); for (int i=0; i<=players.length; i++) { userName2ind.put(orderedUserNames[i],i+1); } It writes "unexpected type" and point on int . If I replace int by String and i+1 by i+"1" , the compilation goes OK. What is wrong with in here? 回答1: It's fine with Integer , but not okay with int - Java generics only work with reference types, basically :( Try this - although be aware it will box everything

Java : HashSet vs. HashMap

拜拜、爱过 提交于 2019-12-20 14:06:30
问题 I have a program working on enormous data sets. The objects are best stored on hash implemented containers since the program keeps seeking for objects in the container. The first idea was to use HashMap since the methods get and remove of this container are more suitable to the uses I need. But, I came to see the use of HashMap is pretty memory consumable which is a major problem, so i thought switching to HashSet will be better because it only uses <E> , and not <K,V> per element, but when I

ConcurrentHashMap put vs putIfAbsent

孤街醉人 提交于 2019-12-20 11:37:35
问题 Java Docs says that, putIfAbsent is equivalent to if (!map.containsKey(key)) return map.put(key, value); else return map.get(key); So if the key exists in the map, it doesn't update its value. Is this correct? What if i want to update a keys value based on some criteria? Say expiration time etc. Would this be a better impl for adding and updating cache? public void AddToCache(T key, V value) { V local = _cache.putifabsent(key, value); if(local.equals(value) && local.IsExpired() == false){

ConcurrentHashMap put vs putIfAbsent

本秂侑毒 提交于 2019-12-20 11:37:18
问题 Java Docs says that, putIfAbsent is equivalent to if (!map.containsKey(key)) return map.put(key, value); else return map.get(key); So if the key exists in the map, it doesn't update its value. Is this correct? What if i want to update a keys value based on some criteria? Say expiration time etc. Would this be a better impl for adding and updating cache? public void AddToCache(T key, V value) { V local = _cache.putifabsent(key, value); if(local.equals(value) && local.IsExpired() == false){

3 企业付款(Transfers)Demo

最后都变了- 提交于 2019-12-20 11:30:45
1 企业转账Demo 1.1 pingpp–java 在下载的ping++ sdk中找到TransferExample复制到你的项目中 1.2 运行前将你的ip配置到白名单 1.3 替换参数(以银联转账为例) /* * * Ping++ Server SDK * 说明: * 以下代码只是为了方便商户测试而提供的样例代码,商户可根据自己网站需求按照技术文档编写, 并非一定要使用该代码。 * 接入企业付款流程参考开发者中心:https://www.pingxx.com/docs/server/transfer ,文档可筛选后端语言和接入渠道。 * 该代码仅供学习和研究 Ping++ SDK 使用,仅供参考。 */ package com.omlife.pay.common; import com.pingplusplus.Pingpp; import com.pingplusplus.exception.*; import com.pingplusplus.model.Transfer; import com.pingplusplus.model.TransferCollection; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util

How do I add values to a Set inside a Map? [closed]

牧云@^-^@ 提交于 2019-12-20 10:44:17
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have this map Map<String, Set<Integer>> myMap; , now I need to interact with it, how do I do it? for example: Keys are: "apple", "orange", "grape", etc. Each set will contain random numbers: 1-9 I need to

HashMap知识总结

不羁的心 提交于 2019-12-20 10:23:45
常见的数据结构 数组:长度固定,可以使用下标索引,所有元素类型一致。 列表:长度可变,可以包含重复的元素。 集合:长度可变,不能放置重复的元素。 堆栈:只允许对最后插入的元素进行操作。后进先出,通过仅有的peek(),push(),和pop()几个方法的强制性限制达到。 队列:先进先出,通过只提供peek(),offer()和poll()这几个方法来访问数据进行限制来达到的。 链表:链表是一种由多个节点组成的数据结构,每个节点包含有数据以及指向下一个节点的引用,在双向链表里还会有一个指向前一个节点的引用。 或许,可以用一张图直观的展现集合这个大家庭。 HashMap底层实现 由上图可知,HashMap实现了Map接口。其底层数据结构是哈希表,而hash表其实就是链表+数组(+红黑树 jdk8之后加入)。表现形式大致如下图: 初始化的表长度为什么是16: 之所以初始长度为16,目的是为了让hash既有数组快速查询优点,又能融合链表方便快捷增加删除元素的优势。 哈希表使用数组+链表的缺点:(两个极端) 1.每次计算hash值都是同一个值,如果每次计算的hash值都是同一个,会造成链表中长度过长的问题。 2.每次计算hash值都是不同的值,如果每次计算的hash值都不一样,会造成HashMap中数据不断扩容,造成容量不断增大。 为了解决这些问题,jdk1.8中使用红黑树代替了链表。