Fail Fast vs Fail Safe in Java

A system is called fail-fast if it stops immediately its execution when an error occurred. The system does not continue with the errors and these errors in the fail-fast systems are immediately exposed. But, fail-safe system does the opposite thing. The fail-safe system does not stop its execution despite an error or a fault is occurred in the system. The system continues with the operations hiding the errors instead of exposing the errors immediately.

Continue

TreeSet in Java

What is TreeSet A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used. Like HashSet, TreeSet also implements Set interface. Like HashSet, TreeSet allows to store only unique elements in their objects. HashSet is much faster than TreeSet because HashSet has constant-time, whereas TreeSet has log-time for most operations like add, remove and contains. Unlike HashSet, TreeSet offers a few handy methods to deal with the ordered set like first(),…

Continue

Spring NamedParameterJdbcTemplate and Collections.singletonMap Example

Introduction In this post I will show you how to use NamedParameterJdbcTemplate and Collections.singletonMap to execute query for returning result. The NamedParameterJdbcTemplate class adds support for programming JDBC statements using named parameters, as opposed to programming JDBC statements using only classic placeholder (?) arguments. The NamedParameterJdbcTemplate class wraps a JdbcTemplate, and delegates to the wrapped JdbcTemplate to do much of its work. An SqlParameterSource is a source of named parameter values to a NamedParameterJdbcTemplate. The Collections.singletonMap returns an immutable map, mapping only the specified key to the specified value. The…

Continue

Collection merging in Spring

With this example we will show you how to merge Collections in Spring. Since Spring 2.0, the container supports merging of collections. You can define <prop/>, <list/>, <map/> and <set/> of parent-style and then you can inherit the child-style element and override values of each type from parent collections. That is, the child collection’s values are the result of merging the elements of the parent and child collections, with the child’s collection elements overriding values specified in the parent collection. If you already have an idea on how to create…

Continue

Collection element props in Spring

With this example we will show you how to inject Collections in Spring Bean properties. In order to show how Collections can be injected in a Spring Bean we will create a simple Spring Bean with a property Properties. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Prerequisites The following things are required in order to run the application Eclipse Kepler JDK 1.8 Have maven 3…

Continue