What will happen if we
put a key object in a HashMap which is already there?
If you put the same
key again than it will replace the old mapping because HashMap doesn't allow
duplicate keys. Same key will result in same hashcode and will end up at same
position in bucket. Each bucket contains a linked list of Map.Entry object,
which contains both Key and Value. Now Java will take Key object form
each entry and compare with this new key using equals() method, if that return
true then value object in that entry will be replaced by new value.
What is difference
between StringBuffer and StringBuilder in Java ?
StringBuffer:
StringBuffer is
mutable means one can change the value of the object . The object created
through StringBuffer is stored in the heap .
StringBuffer has the same methods
as the StringBuilder , but each method in StringBuffer is synchronized that is
StringBuffer is thread safe . Due to this it does not allow two threads to simultaneously access the same
method . Each method can be accessed by one thread at a time . But being thread
safe has disadvantages too as the performance of the StringBuffer hits due to
thread safe property . Thus
StringBuilder is faster than the StringBuffer when calling the same
methods of each class.
String Buffer can be
converted to the string by using toString()
method.
StringBuffer demo1 =
new StringBuffer("Hello") ;
demo1=new
StringBuffer("Bye");
StringBuilder:
StringBuilder is same as the StringBuffer , that is it
stores the object in heap and it can also be modified . The main difference
between the StringBuffer and StringBuilder is that StringBuilder is also not
thread safe.
StringBuilder is fast
as it is not thread safe .
StringBuilder demo2=
new StringBuilder("Hello");
demo2=new
StringBuilder("Bye");
Visit:www.sssedu.inhttp://sssedu.in/java-training-in-chennai.html
No comments:
Post a Comment