hashmap

Java_集合面试题

亡梦爱人 提交于 2019-12-25 06:07:07
Java_集合面试题 0.链表,队列和栈的区别? 链表是一种存储结构,指得是存储时候除了要存储数据元素之外,还要用数据元素一起的另外空间存储数据元素的关系。 队列和栈都是线性表,属于逻辑结构范畴,都是访问点受到限制,并且限制在线性表端点的线性表。 栈被限定为在线性表中的同一个(唯一一个的)端点插入删除 队列被限定为在线性表的一端插入,另外一个端点删除 栈和队列也可以用链表来实现,分别称为链栈和链队列 1. ArrayList ArrayList是基于数组实现的,最大长度不会超过数组的长度2147483647(最大值是int的最大值是,2的31次方减去1 ).如果业务中可能存在超过这个长度的数据,使用LinkedArrayList 3.HashMap Java中的HashMap是以键值对(key-value)的形式存储元素的。HashMap需要一个hash函数,它使用hashCode()和equals()方法来向集合/从集合添加和检索元素。当调用put()方法的时候,HashMap会计算key的hash值,然后把键值对存储在集合中合适的索引上。如果key已经存在了,value会被更新成新值。 HashMap的一些重要的特性是它的容量(capacity),负载因子(load factor)和扩容极限(threshold resizing)。 4.HashMap和HashTable的区别

Java8 HashMap源码分析

与世无争的帅哥 提交于 2019-12-25 05:42:14
java.util.HashMap 是最常用的java容器类之一, 它是一个线程不安全的容器. 本文对JDK1.8.0中的HashMap实现源码进行分析. HashMap 使用位运算巧妙的进行散列并使用链地址法处理冲突. 自JDK1.8后, 若表中某个位置元素数超过阈值 则会将其自动转换为红黑树来提高检索效率. HashMap 中的迭代器同样采用 fail-fast 机制, 即若迭代过程中容器发生结构性改变, 则会终止迭代. HashMap 主要有三个视图接口 keySet() , values() , entrySet() . 它们都是基于迭代器实现的, 并不实际存储数据. 哈希表 自JDK1.8.0开始HashMap使用静态内部类 Node 来存储键值对结构, 不再使用 Map.Entry : static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; V value; Node<K,V> next; Node(int hash, K key, V value, Node<K,V> next) {...} public final K getKey() { return key; } public final V getValue() { return value; }

HashMap or ConcurrentHashMap at Java Controllers?

限于喜欢 提交于 2019-12-25 05:18:14
问题 As explained here: ConcurrentHashMap in Java? concurrent hashmap at Java is thread safe. Java controllers are for web requests and can be called simultaneously from web. My question is that: Should I use concurrent hash map instead of hash map at Java? 回答1: You only need a ConcurrentHashMap if you will be doing concurrent read along with a write or two concurrent writes. If you never change the map after initialization, a regular HashMap is sufficient. Controllers should generally not contain

Alternative to double brace initialization

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 05:00:31
问题 I have a nested collection in the form: HashMap<String, HashMap<String, List<String>>> errorList; Now I initialize it inline using double braces like this errorList.put(tempName, new HashMap<String, List<String>>() {{ put("upl", new ArrayList<String>() {{ add("Y"); add("Upload Success"); }}); }}); This lies in a foreach loop with the value of tempName changing in every iteration. I did this because i couldn't use instances of List<String> or HashMap<String,List<String>> because every time i

Updating List <HashMap>

北城以北 提交于 2019-12-25 04:40:41
问题 I just want to know how to, if a Hashmap is already on the list add 1 to quantity, if it is not then add it to list. This is what I've done that just add eventhough the item is already on list. list = new ArrayList<HashMap<String, String>>(); Cursor c = db.rawQuery("SELECT code,desc,price FROM TbLPrice WHERE code =" + txtCode.getText().toString(), null); //search database if (c.moveToFirst()){ //if successful txtDesc.setText(c.getString(1)); //get desc txtPrice.setText(c.getString(2)); // get

What is the runtime for quadratic probing in a HashTable?

[亡魂溺海] 提交于 2019-12-25 04:12:30
问题 This is a similar question to Linear Probing Runtime but it regards quadratic probing. It makes sense to me that "Theoretical worst case is O(n)" for linear probing because in the worst case, you may have to traverse through every bucket(n buckets) What would runtime be for quadratic probing? I know that quadratic probes in a quadratic fashion -1, 4, 9, 16, ..... My initial thought was that it's some variation of log n(exponential) but there isn't a consistent base. 回答1: If there are n - 1

How to auto populate text field in angular js based on other form fields

对着背影说爱祢 提交于 2019-12-25 03:24:28
问题 I'm giving an student scenario as example. Assume I have the below scope variables that needs to populated when I select a id. $scope.student.name; $scope.student.address; $scope.student.city; $scope.student.zip; The sample html below. <input type="text" ng-model="student.id"/> <input type="text" ng-model="student.name"/> <input type="text" ng-model="student.city"/> <input type="text" ng-model="student.address"/> <input type="text" ng-model="student.zip"> In a regular jQuery, I would write on

How to display Images in ListView android

别等时光非礼了梦想. 提交于 2019-12-25 02:21:07
问题 I have been looking around for examples on how to do this. Cam across an approach that used SimpleAdapter and HashMaps here on StackOverflow. This is my code. It's not working. All I can see is an empty row of a list. listItems = new ArrayList<HashMap<String,Object>>(); HashMap<String,Object> listData1 = new HashMap<String,Object>(); HashMap<String,Object> listData2 = new HashMap<String,Object>(); image_1 = getResources().getDrawable(R.drawable.camera_icon_focus_dim); image_2 = getResources()

HashMap using Generics

不羁的心 提交于 2019-12-25 02:15:05
问题 I have: protected Map<String, ? extends Transaction> transactions = new HashMap<String, ?>(); However, I get a compilation error: Type mismatch: cannot convert from HashMap<String,?> to Map<String,? extends Transaction> I've tried some variations, including: protected Map<String, ? extends Transaction> transactions = new HashMap<String, ? extends Transaction>(); All yield the same compilation error. My goal is to have an instance of Report abstract class (extended by many different kinds of

How to group the values which are in excel to a HashMap

拈花ヽ惹草 提交于 2019-12-25 02:01:24
问题 i have the following excel: Method Name Status Code getLoggedinUserDetails 400 createRequisition 400 excelMDM 400 and i am able to print the values as they are in excel using the following code : package com.poc.excelfun; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss