1) how to
remove duplicates in an ArrayList?
Ans:
You can
create a
LinkedHashSet
from the list. The LinkedHashSet
will contain each element only once, and in the same order as the List
.
list = new ArrayList<String>(new
LinkedHashSet<String>(list))
example:
ArrayList ss = new
ArrayList<>();
ss.add("a");
ss.add("t");
ss.add("d");
ss.add("i");
ss.add("s");
ss.add("a");
System.out.println(ss);
ArrayList ss2 = new
ArrayList<>(new LinkedHashSet(ss));
System.out.println(ss2);
2) why do we
override hashCode() method.
Ans:
"If two objects are equal using
Object class equals method,
then thehashcode method should
give the same value for these two objects." So, if in our class we override equals we should override hashcode method also to fallow this rule.
3) write a thread safe singleton?
Ans:
public class Dev21Singleton {
private
static Dev21Singleton instance = null;
protected Dev21Singleton() {
}
//
Lazy Initialization (If required then only)
public
static Dev21Singleton getInstance() {
//
Thread Safe. Might be costly operation in some case
synchronized
(Dev21Singleton.class) {
if
(instance == null) {
instance
= new Dev21Singleton();
}
}
}
return
instance;
}
}
4)
Questions about abstract class constructor, private variables in abstract class
etc.
5) why can’t we create instance
of an abstract class? what stops us? have you ever tried
creating one?
creating one?
Ans:
It's
because an abstract class isn't complete. One or more parts of its public
interface are not implemented. You've said what they're going to be - what
they're called, what their name is, what they return - but no implementation
has been provided, so nobody would be able to call them.
Compilers
prevent you from instantiating such incomplete objects on the basis that if you
do instantiate an object you're going to want to use its entire public
interface (this assumption is related to the idea that an object should do a
minimal set of things, so that the public interface is not too large to
manage). It's also a useful safety net. The compiler says "hang on, this
is just part of a class, you need to provide the rest of it".
6)
MVC questions -- like how to pass textbox val back to controller etc etc.
7) data
annotation, 1 to many relationship in entity framework, primary and composite
key in entity framework.
No comments:
Post a Comment