<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-8307412328310688269</atom:id><lastBuildDate>Sun, 01 Sep 2024 07:20:19 +0000</lastBuildDate><category>java</category><category>collections</category><category>concurrency</category><category>less known java</category><category>performance</category><category>readability</category><category>tips</category><category>algorithms</category><category>google</category><category>guava</category><category>list</category><category>log4j</category><category>logging</category><category>map</category><category>set</category><category>tricks</category><title>Musings, Thoughts and Cogitations on Java</title><description></description><link>http://thoughtfuljava.blogspot.com/</link><managingEditor>noreply@blogger.com (Manoj Kumar)</managingEditor><generator>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8307412328310688269.post-1963283030207445908</guid><pubDate>Fri, 11 Jul 2014 23:46:00 +0000</pubDate><atom:updated>2014-07-13T11:00:36.382-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Shortest Distance Graph Algorithms - How do they differ?</title><description>The shortest distance graph algorithms can be tricky. Which algorithm should be used when, whether it can work for negative edges or cycles, which one is the most efficient for the given problem - you should be asking these questions before deciding on a Shortest Distance Algorithms.&lt;br /&gt;
&lt;br /&gt;
The implementation and pseudo code for the standard Shortest Distance Algorithms are easily available on the web but how these different algorithms differ is often not highlighted. In this post, I will focus on how different graph algorithms differ from each other.&lt;br /&gt;
&lt;br /&gt;
These different algorithms differ in term of -&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Single Source Shortest Path vs All Pair Shortest Path Algorithms&lt;/b&gt;. Single Source Shortest Path algorithm calculates the shortest distance from a given Source vertex to all other graph vertices. All Pair Shortest Distance algorithm calculates the shortest distance between all pairs of graph vertices.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Weighted graph&lt;/b&gt; - Can the algorithm work if edges have weights?&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Negative weight edges&lt;/b&gt; - Will the algorithm work if there are negative weight edges?&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Negative weight cycles&lt;/b&gt; - The shortest distance problem is not defined for a graph with a negative weight cycle. Because you can keep on going around that cycle and distance with keep on getting smaller. &lt;strike&gt;So no algorithms will work in this case.&lt;/strike&gt;(Update - as pointed out on Reddit, although Shortest Distance problem is not well defined if there are negative weight cycles, it is slightly incorrect to say that algorithms do not work in this case. e.g. Bellman Ford algorithm can be used to detect negative weight cycles in graphs).&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Space and Time Complexity&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
Now let&#39;s look at the standard algorithms from these aspects.&lt;br /&gt;
&lt;h2&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Breadth-first_search&quot; target=&quot;_blank&quot;&gt;Breadth First Search&lt;/a&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Single Source Shortest Path algorithm&lt;/li&gt;
&lt;li&gt;Does not work for weighted graphs. All edges should have equal or unit weight.&lt;/li&gt;
&lt;li&gt;Does not work for Negative weight edges&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Time Complexity - O(E + V)&lt;/li&gt;
&lt;li&gt;Space Complexity - O(V)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm&quot; target=&quot;_blank&quot;&gt;Dijkstra&lt;/a&gt;&amp;nbsp;&lt;/h2&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Single Source Shortest Path algorithm&lt;/li&gt;
&lt;li&gt;Works well for Weighted graphs&lt;/li&gt;
&lt;li&gt;Does not work for Negative weight edges&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Time Complexity - &amp;nbsp;O(E log(V) + &amp;nbsp;V log(V)) using standard heaps, O(E + V log(V)) using Fibonacci Heaps.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Space Complexity - O(V)&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div&gt;
&lt;h2&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm&quot; target=&quot;_blank&quot;&gt;Bellman Ford&lt;/a&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Single Source Shortest Distance algorithm&lt;/li&gt;
&lt;li&gt;Negative Edges - Yes&lt;/li&gt;
&lt;li&gt;Time Complexity - O(EV)&lt;/li&gt;
&lt;li&gt;Space Complexity - O(V)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
Shortest Distance for Directed Acyclic Graphs(DAG)&lt;/h2&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
Apparently if the graph is of type DAG (Directed Acyclic Graph), then there is &lt;a href=&quot;https://www.ics.uci.edu/~eppstein/161/960208.html&quot; target=&quot;_blank&quot;&gt;another algorithm&lt;/a&gt; with better time complexity. It works for all cases where Bellman Ford works plus it has better time complexity.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Single Source Shortest Distance algorithm&lt;/li&gt;
&lt;li&gt;Negative Edges - Yes&lt;/li&gt;
&lt;li&gt;Time Complexity - O(E + V)&lt;/li&gt;
&lt;li&gt;Space Complexity - O(V)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm&quot; target=&quot;_blank&quot;&gt;Floyd Warshall&amp;nbsp;&lt;/a&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;All pairs shortest path algorithm&lt;/li&gt;
&lt;li&gt;Works well for Weighted graphs&lt;/li&gt;
&lt;li&gt;Works well with negative weight edges&lt;/li&gt;
&lt;li&gt;Time Complexity - O(V^3)&lt;/li&gt;
&lt;li&gt;Space Complexity - O(V^2)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
Now let&#39;s try to use these differences to solve a problem. I was asked this problem in an interview and it is a very good problem to test understanding of different graph algorithms.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;You&#39;ve always been intrigued with the Six Degrees of Kevin Bacon game. Let&#39;s say if two actors have been in the same movie we call them &#39;friends&#39; and if two actors have not been in the same movie, we say they are not &#39;friends&#39;. Now choose any two actors at random -- we want to calculate the number of degrees of separation and the path between them. How do you go about this problem?&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;• Discuss your algorithm ideas. For each algorithm talk about the trade-offs.&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Ok. Clearly this is a shortest distance problem.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;What are the weights of the edges? All edges have unit weight. So BFS can be used.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Are there negative weight edges? No. All edges have weight 1. So Dijkstra can also be used. Between BFS and Dijkstra, BFS has better Time complexity O (E + V) vs O(E + V log(V) ) in Dijkstra. Space complexity is same for both O(V). Clearly between BFS and Dijkstra, BFS is better in this case.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;This problem is best modeled as an undirected graph and can contain cycles. So DAG algorithm will not work.&lt;/li&gt;
&lt;li&gt;But what about Floyd Warshall Algorithm. So do we need an All Pair Shortest Path algorithm here? Floyd warshall has time complexity of O(V^3) and space complexity of O(V^2) which is worse than BFS. Here we need to understand the problem better. Do we need to perform the distance calculation for a single pair of actors or there can be many looks we need to perform. If we need to do a one-off lookup, then clearly BFS is a better solution. However if we need to perform many lookups over a long period, then it make better sense to just run Floyd Warshal once, calculate all the distances and store the results and later use the stored result for each lookup.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Hope this articles helps you understand different shortest distance algorithms better. Please comment if you have any questions or feedback.&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://thoughtfuljava.blogspot.com/2014/07/notes-on-shortest-distance-graph.html</link><author>noreply@blogger.com (Manoj Kumar)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8307412328310688269.post-1880889713665243089</guid><pubDate>Fri, 12 Oct 2012 19:49:00 +0000</pubDate><atom:updated>2012-10-16T07:17:55.283-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">collections</category><category domain="http://www.blogger.com/atom/ns#">google</category><category domain="http://www.blogger.com/atom/ns#">guava</category><category domain="http://www.blogger.com/atom/ns#">java</category><category domain="http://www.blogger.com/atom/ns#">list</category><category domain="http://www.blogger.com/atom/ns#">map</category><category domain="http://www.blogger.com/atom/ns#">readability</category><category domain="http://www.blogger.com/atom/ns#">set</category><title>Concise Collection Initialization using Google Guava</title><description>One thing I really like with languages like python, ruby or perl is that they have succinct and elegant way to initialize the collections - &amp;nbsp;Map, Set and Array.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001193.html&quot; target=&quot;_blank&quot;&gt;Collection Literals&lt;/a&gt; project is supposed to bring concise initialization syntax to Java. But there is no hope for this until Java 8. There are some hackish ways to do inline initialization of collections particularly using &lt;a href=&quot;http://www.c2.com/cgi/wiki?DoubleBraceInitialization&quot; target=&quot;_blank&quot;&gt;Double Curly Brace Initialization&lt;/a&gt; but none of these is elegant and expressive.&lt;br /&gt;
&lt;br /&gt;
Google provides an excellent api of their core libraries called &lt;a href=&quot;http://code.google.com/p/guava-libraries/&quot; target=&quot;_blank&quot;&gt;Guava&lt;/a&gt;. This library provides some nice utility classes to perform initialization of collections.&lt;br /&gt;
&lt;br /&gt;
For each collection type, Guava provides Utility Classes. e.g. for Lists, there are classes - &lt;a href=&quot;http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Lists.html&quot; target=&quot;_blank&quot;&gt;Lists&lt;/a&gt;&amp;nbsp;(utility class for operating with lists) and &lt;a href=&quot;http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableList.html&quot; target=&quot;_blank&quot;&gt;ImmutableList&lt;/a&gt;&amp;nbsp;- (an Immutable version of List). Lets take a look at couple of examples -&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;gistLoad&quot; data-id=&quot;3881251&quot; id=&quot;gist-3881251&quot;&gt;
Loading ....&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;References -&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001193.html&quot;&gt;http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001193.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.c2.com/cgi/wiki?DoubleBraceInitialization&quot;&gt;http://www.c2.com/cgi/wiki?DoubleBraceInitialization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained&quot;&gt;http://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Reddit Discussion for this post&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;http://www.reddit.com/r/java/comments/11g46q/concise_collection_initialization_using_google/&quot;&gt;http://www.reddit.com/r/java/comments/11g46q/concise_collection_initialization_using_google/&lt;/a&gt;&lt;/div&gt;
&lt;script src=&quot;https://raw.github.com/moski/gist-Blogger/master/public/gistLoader.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</description><link>http://thoughtfuljava.blogspot.com/2012/10/concise-collection-initialization-using.html</link><author>noreply@blogger.com (Manoj Kumar)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8307412328310688269.post-8385324050312807466</guid><pubDate>Fri, 05 Oct 2012 11:00:00 +0000</pubDate><atom:updated>2012-10-06T02:02:03.292-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">java</category><category domain="http://www.blogger.com/atom/ns#">less known java</category><category domain="http://www.blogger.com/atom/ns#">log4j</category><category domain="http://www.blogger.com/atom/ns#">logging</category><category domain="http://www.blogger.com/atom/ns#">tips</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><title>Java - One liner to Configure Logging</title><description>Often while writing some quick test or doing a prototype, we want to use logging. But we do not want to spend any time writing a properties file and doing the whole setup.&lt;br /&gt;
&lt;br /&gt;
However there is a quick way to get log4j ready to use in a single line.&lt;br /&gt;
Log4j provides a &lt;a href=&quot;http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/BasicConfigurator.html&quot; target=&quot;_blank&quot;&gt;BasicConfigurator&lt;/a&gt; which can be used to quickly configure the logging.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Usage Example -&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;public class LoggingOneLiner1 {

 private static final Logger logger = Logger
   .getLogger(LoggingOneLiner1.class);

 public static void main(String[] args) {
  // Configure the logging - this does the whole setup
  BasicConfigurator.configure();

  // Optional Step - change the logging level to INFO from default DEBUG
  Logger.getRootLogger().setLevel(Level.INFO);

  logger.info(&quot;Its nice to to be able to configure logging in one line&quot;);
  logger.debug(&quot;I like quick and dirty java tricks&quot;);

 }

}
&lt;/pre&gt;
&lt;br /&gt;
From the &lt;a href=&quot;http://logging.apache.org/log4j/1.2/manual.html&quot; target=&quot;_blank&quot;&gt;api docs of log4j&lt;/a&gt; -&lt;br /&gt;
&lt;div style=&quot;font-family: Verdana, Helvetica, Arial, sans-serif; font-size: 13px; line-height: 1.3em;&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;The invocation of the&lt;/span&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/BasicConfigurator.html#configure&quot; style=&quot;color: #4477aa; line-height: 1.3em; text-decoration: none;&quot;&gt;BasicConfigurator.configure&lt;/a&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;method creates a rather simple log4j setup.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;This method is hardwired to add to the root logger a&lt;/span&gt;&lt;a href=&quot;http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/ConsoleAppender.html&quot; style=&quot;color: #4477aa; line-height: 1.3em; text-decoration: none;&quot;&gt;ConsoleAppender&lt;/a&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;. The output will be formatted using a&lt;/span&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html&quot; style=&quot;color: #4477aa; line-height: 1.3em; text-decoration: none;&quot;&gt;PatternLayout&lt;/a&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;set to the pattern &quot;%-4r [%t] %-5p %c %x - %m%n&quot;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;Note that by default, the root logger is assigned to&lt;/span&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;tt style=&quot;line-height: 1.3em;&quot;&gt;Level.DEBUG&lt;/tt&gt;&lt;span style=&quot;line-height: 1.3em;&quot;&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;line-height: 20px;&quot;&gt;Reddit Discussion on this post -&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;line-height: 20px;&quot;&gt;&lt;a href=&quot;http://www.reddit.com/r/java/comments/10zkhr/java_one_liner_to_configure_logging_using_log4j/&quot;&gt;http://www.reddit.com/r/java/comments/10zkhr/java_one_liner_to_configure_logging_using_log4j/&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;line-height: 20px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;script src=&quot;http://crux-framework-tools.googlecode.com/svn/trunk/misc/highlight/highlightLoader.js&quot;&gt;&lt;/script&gt;</description><link>http://thoughtfuljava.blogspot.com/2012/10/java-one-liner-to-configure-logging.html</link><author>noreply@blogger.com (Manoj Kumar)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8307412328310688269.post-3619924111780560194</guid><pubDate>Fri, 28 Sep 2012 12:23:00 +0000</pubDate><atom:updated>2012-10-01T11:37:43.690-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">collections</category><category domain="http://www.blogger.com/atom/ns#">concurrency</category><category domain="http://www.blogger.com/atom/ns#">java</category><category domain="http://www.blogger.com/atom/ns#">performance</category><title>Speedup Your Collections with Improved Hashing Function from Java7</title><description>Java7 has introduced an improved hash function for &lt;u&gt;Map and Map derived implementations&lt;/u&gt; (&lt;i&gt;since 7u6&lt;/i&gt;). It is applicable for &lt;u&gt;keys of type String&lt;/u&gt;.&lt;br /&gt;
&lt;br /&gt;
Following collections can make use of this new hash function.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html&quot;&gt;HashMap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html&quot;&gt;Hashtable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html&quot;&gt;HashSet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html&quot;&gt;LinkedHashMap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html&quot;&gt;LinkedHashSet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html&quot;&gt;WeakHashMap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html&quot;&gt;ConcurrentHashMap&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div&gt;
The new hash function improves the performance &lt;i&gt;&lt;u&gt;when there are large number of collisions&lt;/u&gt;&lt;/i&gt;.&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
It can be enabled by setting the System property&amp;nbsp;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;jdk.map.althashing.threshold&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
If&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;jdk.map.althashing.threshold=k&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp;,&lt;/span&gt;&lt;br /&gt;
This means that java will use alternative hashing function for maps with capacity greater than k entries.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Usage Example -&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;java -Djdk.map.althashing.threshold=512 MyAppMain&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt; Why it is disabled by default and caveat before using - 
&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;This feature is disabled by default to ensure backward compatibility.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;The iteration order of keys, values and entries will change after using the new Hash function. According to Java team, many applications have dependencies on iteration order and therefore they have decided to disable this by default.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;If your application logic has dependencies on iteration order of Map/Set, then you should remove those dependencies before using new Hash function.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Ideally applications should not make any assumptions regarding iteration order and therefore it is a good time to refactor your code and utilize the new Hashing function.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;References - 

&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://mail.openjdk.java.net/pipermail/jdk7u-dev/2012-July/003721.html&quot;&gt;http://mail.openjdk.java.net/pipermail/jdk7u-dev/2012-July/003721.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/technotes/guides/collections/changes7.html&quot;&gt;http://docs.oracle.com/javase/7/docs/technotes/guides/collections/changes7.html&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Reddit discussion for this post -&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;http://www.reddit.com/r/java/comments/10m5tg/speedup_your_collections_with_improved_hashing/&quot;&gt;http://www.reddit.com/r/java/comments/10m5tg/speedup_your_collections_with_improved_hashing/&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thoughtfuljava.blogspot.com/2012/09/speedup-your-collections-with-improved.html</link><author>noreply@blogger.com (Manoj Kumar)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8307412328310688269.post-7093123166964350898</guid><pubDate>Fri, 21 Sep 2012 13:33:00 +0000</pubDate><atom:updated>2012-09-29T00:55:39.793-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">concurrency</category><category domain="http://www.blogger.com/atom/ns#">java</category><category domain="http://www.blogger.com/atom/ns#">performance</category><title>Prefer ThreadLocalRandom over Random </title><description>Java 7 has introduced a new random number generator - &lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadLocalRandom.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;ThreadLocalRandom&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Normally to generate Random numbers, we either do&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Create an instance of &lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/Random.html&quot; target=&quot;_blank&quot;&gt;java.util.Random&lt;/a&gt;&amp;nbsp;OR&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#random()&quot; target=&quot;_blank&quot;&gt;Math.random()&lt;/a&gt; - which internally creates an instance of java.util.Random on first invocation.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
However in a concurrent applications usage of above leads to contention issues -&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Random is thread safe for use by multiple threads. But if multiple threads use the same instance of Random, the same seed is shared by multiple threads. It leads to contention between multiple threads and so to performance degradation.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadLocalRandom.html&quot; target=&quot;_blank&quot;&gt;ThreadLocalRandom&lt;/a&gt; is solution to above problem. ThreadLocalRandom has a Random instance per thread and safeguards against contention.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
From the api docs -&amp;nbsp;&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;background-color: white; color: #353833; font-family: Arial, Helvetica, sans-serif; font-size: 12px;&quot;&gt;Usages of this class should typically be of the form:&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: white; color: #353833; font-size: 12px;&quot;&gt;ThreadLocalRandom.current().nextX(...)&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #353833; font-family: Arial, Helvetica, sans-serif; font-size: 12px;&quot;&gt;&amp;nbsp;(where&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: white; color: #353833; font-size: 12px;&quot;&gt;X&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #353833; font-family: Arial, Helvetica, sans-serif; font-size: 12px;&quot;&gt;&amp;nbsp;is&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: white; color: #353833; font-size: 12px;&quot;&gt;Int&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #353833; font-family: Arial, Helvetica, sans-serif; font-size: 12px;&quot;&gt;,&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: white; color: #353833; font-size: 12px;&quot;&gt;Long&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #353833; font-family: Arial, Helvetica, sans-serif; font-size: 12px;&quot;&gt;, etc). When all usages are of this form, it is never possible to accidently share a&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: white; color: #353833; font-size: 12px;&quot;&gt;ThreadLocalRandom&amp;nbsp;&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #353833; font-family: Arial, Helvetica, sans-serif; font-size: 12px;&quot;&gt;across multiple threads.&lt;/span&gt;&lt;/blockquote&gt;
&lt;div&gt;
Usage Example -&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;//Generate a random number b/w 0 and 10.  0 &amp;lt;= R &amp;lt; 10
//Using Math.random()
int r1 = (int)Math.random()*10;
//Using Random
Random rand = new Random();
int r2 = rand.nextInt(10);
//Using ThreadLocalRandom
int r3 = ThreadLocalRandom.current().nextInt(10);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Update -&lt;br /&gt;
&lt;a href=&quot;http://www.reddit.com/r/java/comments/108wia/avoid_random_use_new_threadlocalrandom_from_java7/&quot; target=&quot;_blank&quot;&gt;Reddit discussion on this post&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;http://crux-framework-tools.googlecode.com/svn/trunk/misc/highlight/highlightLoader.js&quot;/&gt;</description><link>http://thoughtfuljava.blogspot.com/2012/09/prefer-threadlocalrandom-over-random.html</link><author>noreply@blogger.com (Manoj Kumar)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8307412328310688269.post-8926334396136828407</guid><pubDate>Sun, 16 Sep 2012 06:30:00 +0000</pubDate><atom:updated>2012-09-29T00:54:41.210-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">java</category><category domain="http://www.blogger.com/atom/ns#">less known java</category><category domain="http://www.blogger.com/atom/ns#">readability</category><category domain="http://www.blogger.com/atom/ns#">tips</category><title>Improving readability using TimeUnit sleep</title><description>All of us have seen the following idiom &amp;nbsp;numerous times in java code.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;//Sleep for 6 minutes
Thread.sleep(6*60*1000);
&lt;/pre&gt;
&lt;br /&gt;
However the above is not readable.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;When someone is writing code, he has to perform the conversion to milliseconds.&lt;/li&gt;
&lt;li&gt;While trying to understand the code also, someone has to perform the transformation from milliseconds to familiar seconds and minutes.&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;a href=&quot;http://download.java.net/jdk7/archive/b123/docs/api/java/util/concurrent/TimeUnit.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;java.util.concurrent&lt;/span&gt;.TimeUnit&lt;/a&gt; &lt;/span&gt;class also provides mechanism to sleep which circumvents the above difficulties.&lt;br /&gt;
Following is an example of sleep using TimeUnit.&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;//Sleep for 6 minutes
TimeUnit.MINUTES.sleep(6);
//Sleep for 10 seconds
TimeUnit.SECONDS.sleep(10);
//Sleep for 2 hours
TimeUnit.HOURS.sleep(2);

&lt;/pre&gt;
&lt;br /&gt;
TimeUnit supports following time units for sleep.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;DAYS&lt;/li&gt;
&lt;li&gt;HOURS&lt;/li&gt;
&lt;li&gt;MICROSECONDS&lt;/li&gt;
&lt;li&gt;MILLISECONDS&lt;/li&gt;
&lt;li&gt;MINUTES&lt;/li&gt;
&lt;li&gt;NANOSECONDS&amp;nbsp;&lt;/li&gt;
&lt;li&gt;SECONDS&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Internally TimeUnit also invokes Thread.sleep() for sleeping. Following is the code used by sleep() method in TimeUnit&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;    public void sleep(long timeout) throws InterruptedException {
    if (timeout &amp;gt; 0) {
        long ms = toMillis(timeout);
        int ns = excessNanos(timeout, ms);
        Thread.sleep(ms, ns);
    }
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This small trick improves the readability of code.&lt;/div&gt;
&lt;br /&gt;
Update -&lt;br /&gt;
&lt;a href=&quot;http://www.reddit.com/r/java/comments/zyoat/improving_readability_using_timeunit_sleep/&quot; target=&quot;_blank&quot;&gt;Reddit discussion for this post&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;http://crux-framework-tools.googlecode.com/svn/trunk/misc/highlight/highlightLoader.js&quot;/&gt;</description><link>http://thoughtfuljava.blogspot.com/2012/09/improving-readability-using-timeunit.html</link><author>noreply@blogger.com (Manoj Kumar)</author><thr:total>1</thr:total><georss:featurename>Bangalore, Karnataka, India</georss:featurename><georss:point>12.9715987 77.5945627</georss:point><georss:box>12.724026199999999 77.2787057 13.2191712 77.910419699999991</georss:box></item></channel></rss>