HashMap provides constant-time performance for the basic operations (get and put). Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important. An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs.
===============================
What is the capacity of HashMap
===============================
The capacity is the number of buckets in the hash table
===============================
What is loadFactor in HashMap
===============================
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased
===============================
I want to store certain objects in a HashMap. Say object Emp. Emp has age, name, and dept as its attribute. Can I add this object as a key? If yes, how can I make sure that key is unique.?
What is extra thing that object need to be do, to be as key for Hashmap.
===============================
yes, you can add an object as a key.
Your key object must implement/ override the hashCode() and equals(). If it is a SortedMap, it must also implements the Comparable interface
Your key object must implement/ override the hashCode() and equals(). If it is a SortedMap, it must also implements the Comparable interface
===============================
How to make a HashMap as Synchronized.
===============================
===============================
1) HashMap is not synchronized.it must be synchronized externally.
2) the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
2) the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
Map m = Collections.synchronizedMap(new HashMap(...));
Key Points or Facts or Best Practices in java
1. System.out.println is expensive. These calls are synchronized for the duration of disk I/O, which significantly slows throughput.
2. Catch an exception as close as possible to its source.
3. Never throw unchecked exceptions in your methods
===============================
When to throw CheckedException and When to throw RuntimeException
===============================
bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
The cost of exception handling
Exceptions are expensive and should be used exceptionally. The JVM maintains a method invocation stack containing all the methods that have been invoked by the current thread in the reverse order of invocation. Actually it is not the actual method that is present in the stack. Instead a stack frame representing the method is added to the stack. The stack frame contains the method’s parameters, return value, local variables and JVM specific information. When the exception is thrown in a method at the top of the stack, code execution stops and the JVM takes over. The JVM searches the current method for a catch clause for the exception thrown or one of the parent classes of the thrown exception. If one is not found, then the JVM pops the current stack frame and inspects the calling method (the next method in the stack), for the catch clause for the exception or its parents. The process continues until the bottom of the stack is reached. In summary, it requires a lot of time and effort on the part of JVM.
Exceptions are expensive and should be used exceptionally. The JVM maintains a method invocation stack containing all the methods that have been invoked by the current thread in the reverse order of invocation. Actually it is not the actual method that is present in the stack. Instead a stack frame representing the method is added to the stack. The stack frame contains the method’s parameters, return value, local variables and JVM specific information. When the exception is thrown in a method at the top of the stack, code execution stops and the JVM takes over. The JVM searches the current method for a catch clause for the exception thrown or one of the parent classes of the thrown exception. If one is not found, then the JVM pops the current stack frame and inspects the calling method (the next method in the stack), for the catch clause for the exception or its parents. The process continues until the bottom of the stack is reached. In summary, it requires a lot of time and effort on the part of JVM.
===============================
What is the difference == and .equals() method
===============================
1) equals() method is present in the java.lang.Object class
1) '==' operator
1) '==' operator
2) equals() check for the equivalence of the state of objects! That means, the contents of the objects.
2) == check the actual object instances are same or not.
2) == check the actual object instances are same or not.
If you have two object say emp1 and emp2. Both of them have empName and empAddress same.
emp1 stored at momory location 3x00794 and emp2 at 3x00794. Both of them are on heap memory of JVM
Now if you say (emp1 == emp2) will give you FALSE, as == checks at reference lavel and not at object content
but .equals () method will return TRUE.
For String also
if you have two strings s1 = new String("xyz"); and s2 = new String("xyz"); if you use == method , it will return
FALSE but .equals() method will return TRUE. Again same principal. As for Strings when you use new operator it will get saperate space for saperate object.
If you use String s1 ="xyz";
String s2 ="xyz"; Here both the method == and .equals() will return TRUE
===============================
Can I add millions of object in ArrayList
===============================
Yes,
Definately You can. You can write a loop which will iterate for million time and try this in local program.
But it is directly dependent on
1)Heap size of JVM
2) A type of object you are trying to add to collection. i.e. if it is an Integer then its ok, but if its object of say type Emp
which has say 10 attributs in it, they definately its a matter of heap memory. There are chances that system will give
OUT of MEMORY error.
No comments:
Post a Comment