There are several differences between HashMap
and Hashtable
in Java:
Hashtable
is synchronized, whereas HashMap
is not. This makes HashMap
better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
Hashtable
does not allow null
keys or values. HashMap
allows one null
key and any number of null
values.
One of HashMap's subclasses is LinkedHashMap
, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap
for a LinkedHashMap
. This wouldn't be as easy if you were using Hashtable
.
Since synchronization is not an issue for you, I'd recommend HashMap
. If synchronization becomes an issue, you may also look at ConcurrentHashMap
.
From the Java Tutorial:
Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.
Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:
class OuterClass {
...
class InnerClass {
...
}
}
An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass outerObject = new OuterClass()
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
see: Java Tutorial - Nested Classes
For completeness note that there is also such a thing as an inner class without an enclosing instance:
class A {
int t() { return 1; }
static A a = new A() { int t() { return 2; } };
}
Here, new A() { ... }
is an inner class defined in a static context and does not have an enclosing instance.
Best Answer
It's always a good practice to Program to an interface, not an implementation and Spring Framework uses this practice by providing quite a lot of these interfaces,
HandlerInterceptor
is one of them. Some of these interfaces are richer than others. So if you as a client want to provide a custom implementation for them and only care for a few of their methods, you would end up with a few actual implementations and a lot of empty implementations.For example, suppose you want to provide an implementation for
preHandle
method and don't care about the other two. Unfortunately you should provide some empty implementations for the other two:Those empty implementations would cause greater boilerplate codes when you're implementing even more richer interfaces, like
WebMvcConfigurer
with10+
abstract methods, imagine the loads of empty methods.In order to solve this problem, Spring Framework usually provides a corresponding abstract Adapter for those interfaces, like
HandlerInterceptorAdaptor
forHandlerInterceptor
interface orWebMvcConfigurerAdapter
forWebMvcConfigurer
. Those adapters are just a bunch of default and simplified implementations for all methods of those interfaces. You can refactor the preceding code using the provided adapter:Since those empty implementations are provided by the
HandlerInterceptorAdapter
class, you just need to provide your implementation forpreHandle
method.As I said, this is a recurring theme in the Spring Framework, some of the common examples are:
WebMvcConfigurer
andWebMvcConfigurerAdapter
CachingConfigurer
andCachingConfigurerSupport