Posts

Showing posts from February, 2018

Differences between Map implementations - HashMap, TreeMap, LinkedHashMap, WeakHashMap, IdentityHashMap, EnumMap

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. HashMap, TreeMap, LinkedHashMap, WeakHAshMap, IdentityHashMap, EnumMap     HashMap is implemented as a hash table, and there is no ordering on keys or values.  TreeMap is implemented based on red-black tree structure, and it is ordered by the key. Key object should implement Comparable interface inorder to compare with other keys and maintain sorted order.  TreeMap is not synchronized. We can make it synchronized by using Collections.synchronizedMap method. Ex: Simple example which outputs the natural ordering of key elements.  Map treeMap = new TreeMap(); treeMap.put("f",10); treeMap.put("d", 4); treeMap.put("a", 1); treeMap.put("b", 2); output :  a=1, b=2, d=4, f=10...

Performance of HashMap, HashTable, SynchronizedHashMap and ConcurrentHashMap

Synchronized  HashMap : Each method is synchronized using an object level lock. So the get and put methods on synchMap acquire a lock. Locking the entire collection is a performance overhead. While one thread holds on to the lock, no other thread can use the collection. ConcurrentHashMap  was introduced in JDK 5. There is no locking at the object level,The locking is at a much finer granularity. For a  ConcurrentHashMap , the locks may be at a hashmap bucket level. The effect of lower level locking is that you can have concurrent readers and writers which is not possible for synchronized collections. This leads to much more scalability. ConcurrentHashMap  does not throw a  ConcurrentModificationException  if one thread tries to modify it while another is iterating over it. I highly recommend to follow the below link. Clearly explained about the difference in the performance between HashMap, HashTable, SynchronizedHashM...

8 Options for capturing Java Thread Dumps

8 Options for capturing Java Thread Dumps Thread dumps are used to check CPU spikes, deadlocks, memory problems, unresponsive applications, poor responsive times and other system problems. jstack Kill -3 JVisualVM JMC Windows(Ctrl - Break) ThreadMXBEan APM Tool - APP Dynamics JCMD For more info, visit https://dzone.com/articles/how-to-take-thread-dumps-7-options

Java Split method, Handling special characters in Split method, Retaining split character in the resulting parts

Java Split method, Handling special characters in Split method, Retaining split character in the resulting parts String string = "004-034556"; String[] parts = string.split("-"); String part1 = parts[0]; // 004 String part2 = parts[1]; // 034556 Note that this takes a regular expression, so remember to escape special characters if necessary. there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters". So, if you want to split on e.g. period/dot . which means "any character" in regex, use either backslash \ to escape the individual special character like so split("\\."), or use...

Eclipse build errors - java.lang.object cannot be resolved

Eclipse build errors - java.lang.object cannot be resolved The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project Solution: The work around is to remove the JRE System Library from the project and then add it back again.  Here are the steps: Go to properties of project with the build error (right click > Properties) View the "Libraries" tab in the "Build Path" section Find the "JRE System Library" in the list (if this is missing then this error message is not an eclipse bug but a mis-configured project) Remove  the "JRE System Library" Hit "Add Library ...", Select "JRE System Library" and add the appropriate JRE for the project (eg. 'Workspace default JRE') Hit "Finish...

HashMap basics and its internal implementation

Hi, I am Malathi Boggavarapu working at Volvo Group and i live in Gothenburg, Sweden. I have been working on Java since several years and had vast experience and knowledge across various technologies. In this post we will learn about basics of HashMap and its internal implementation. What is HashMap? HashMap implements Map interface. Collection view of a Map can be obtained using entrySet() method. To obtain a collection-view of the keys, keySet() method can be used. HashMap is not synchronized and allows null keys and values whereas HashTable is synchronized. An instance of HashMap has two parameters that affect its performance:  initial capacity  load factor.  The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.  The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When th...

Java Multithreading concepts

Java is a  multi-threaded programming language  which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs. By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application. We can manually create threads in the following ways 1) Class extends Thread class Example:: import java.util.concurrent.TimeUnit; public class FirstWay{ public static void main(String args[]) ...

Some Random and Important technical stuff

Some Random and Important technical stuff public class DefaultedMap <K,V> extends AbstractMapDecorator <K,V> implements Serializable Decorates another  Map  returning a default value if the map does not contain the requested key. When the  get(Object)  method is called with a key that does not exist in the map, this map will return the default value specified in the constructor/factory. Only the get method is altered, so the  Map.containsKey(Object)  can be used to determine if a key really is in the map or not. The defaulted value is not added to the map. Compare this behaviour with  LazyMap , which does add the value to the map (via a Transformer). For instance: Map map = new DefaultedMap("NULL"); Object obj = map.get("Surname"); // obj == "NULL" After the above code is executed the map is still empty. Note that DefaultedMap is not synchronized and is not thread-safe.  If you wish to use this map from multiple thr...

Javascript and jQuery stuff - Part1

var arrayOfIds = document.querySelectorAll('*[id^="workspaceRightsOfUsage_entity_releaseFormSubjName"]'); //returns array of ids that match It returns all the divs which have 'workspaceRightsOfUsage_entity_releaseFormSubjName' as substring in the id. Ex: when multiple input fields exists with same div id but each div id is suffixed with incremented index, workspaceRightsOfUsage_entity_releaseFormSubjName_0 workspaceRightsOfUsage_entity_releaseFormSubjName_1 workspaceRightsOfUsage_entity_releaseFormSubjName_2 then the querySelectorAll helps as regex to find all such div's - CSS .disableRightsOfUsage{     pointer-events: none;     cursor: default;     opacity: 0.4; } Used to disable the html element. pointer events will be disabled. If we set opacity, the area will be shaded out How to make an ajax using jQuery ?????? // INFO // Used to create a AJAX call and for rendering the result. // Will trigger the eve...