What do you know about Containers?

There are two types of container library available in java.
1. Collection: Collection category only holds one item in each location. This includes List and Set Interfaces.
a. List (ArrayList, Vector, LinkedList): A collection of elements, duplicates allowed.
b. Set (HashSet, LinkedHashSet, TreeSet): A collection of unique elements.
2. Map: The Map holds key-value pairs, rather like a mini database. We can look up on the basis of key. This includes Map interface.
a. Map (HashMap, HashTable, LinkedHashMap, TreeMap): A mapping of keys to values.

Converting Date to String object

Below is the code for converting Date to specified String. This code uses SimpleDateFormat class for converting Date to String.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-dd-mm");
Date now = new Date();

// Convert Date object to date string in the above mentioned format
String strNow = dateFormat.format(now);
System.out.println(strNow);
}
}

Converting String to Date object

Below is the code for converting String of specified format into Date object. This code uses SimpleDateFormat class for converting String to Date.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-dd-mm");
String strNow = "2008-11-03";

// Convert given String to date object
try {
Date todayDate = dateFormat.parse(strNow);
System.out.println(todayDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Caused by: java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException

I am getting the following exception. Please let me know what am I missing? Thanks in advance.

Caused by: java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:54)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:757)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:722)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:386)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:267)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)

nested exception is java.lang.IllegalStateException: Cannot create command without commandClass

I am getting following exception while using AbstractCommandController in spring framework.

The server encountered an internal error () that prevented it from fulfilling this request.
exception org.springframework.web.util.NestedServletException: Request processing
failed; nested exception is java.lang.IllegalStateException: Cannot create command without commandClass
being set - either set commandClass or (in a form controller) override formBackingObject
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:488)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:441)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:265)
org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:110)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)

Please help me in resolving this exception. Thanks in advance.

nested exception is java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher

I am getting following exception while deploying a web spring based application in tomcat 6.0.

nested exception is java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:899)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:793)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:441)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:265)

Iterating over Map

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. Map iteration can be done on the basis of key or value or key value set. Choose iteration according to your requirement.

productMap.put("P001", "Product 1");
productMap.put("P002", "Product 2");
productMap.put("P003", "Product 3");
productMap.put("P004", "Product 4");
productMap.put("P005", "Product 5");

System.out.println("--Iterating over Key Set(Method 1)---");
for(Object key : productMap.keySet()) {
System.out.println(key);
}

System.out.println("--Iterating over Key Set(Method 2)---");
Iterator keyIter = productMap.keySet().iterator();
while (keyIter.hasNext()) {
String element = (String)keyIter.next();
System.out.println(element);
}

System.out.println("--Iterating over Value Set--");
Iterator valueIter = productMap.values().iterator();
while(valueIter.hasNext()) {
System.out.println(valueIter.next());
}

System.out.println("--Iterating over Key Value Set--");
Iterator keyValIter = productMap.entrySet().iterator();
while(keyValIter.hasNext()) {
Map.Entry entry = (Map.Entry)keyValIter.next();
System.out.println(entry.getKey() + " => " + entry.getValue());
}

Chose iterator carefully for improved performance.

StringBuilder in Java

StringBuilder was introduced in Java 1.5 (5.0). StringBuilder is mutable sequence of characters. This class is almost similar to StringBuffer, but with no guarantee of synchronization i.e. instances of StringBuilder are not safe in multithreaded environment. If such is the case then it is recommended that the StringBuffer is used. Avoiding the overhead of synchronization makes the StringBuilder very slightly faster.

← Previous Page