Advanced Java Mcq Questions With Answers Pdf
• for collection classes for supporting sequential as well as parallel processing • default method that we can use to iterate over a collection. It is very helpful when used with because it’s argument Consumer is a. • Miscellaneous Collection API improvements such as forEachRemaining(Consumer action) method in Iterator interface, Map replaceAll(), compute(), merge() methods. • What is Java Collections Framework? List out some benefits of Collections framework? Collections are used in every programming language and initial java release contained few classes for collections: Vector, Stack, Hashtable, Array.
Here are Java multiple choice questions for both freshers and experienced. How To Install Effectrix In Logic The Term there. Check your Java knowledge and score..
But looking at the larger scope and usage, Java 1.2 came up with Collections Framework that group all the collections interfaces, implementations and algorithms. Java Collections have come through a long way with usage of Generics and Concurrent Collection classes for thread-safe operations. It also includes blocking interfaces and their implementations in java concurrent package. Some of the benefits of collections framework are; • Reduced development effort by using core collection classes rather than implementing our own collection classes. • Code quality is enhanced with the use of well tested collections framework classes.
• Reduced effort for code maintenance by using collection classes shipped with JDK. • Reusability and Interoperability. • Can we use any class as Map key? We can use any class as Map Key, however following points should be considered before using them.
• If the class overrides equals() method, it should also override hashCode() method. • The class should follow the rules associated with equals() and hashCode() for all instances. Please refer earlier question for these rules. • If a class field is not used in equals(), you should not use it in hashCode() method. • Best practice for user defined key class is to make it immutable, so that hashCode() value can be cached for fast performance.
Also immutable classes make sure that hashCode() and equals() will not change in future that will solve any issue with mutability. For example, let’s say I have a class MyKey that I am using for HashMap key. //MyKey name argument passed is used for equals() and hashCode() MyKey key = new MyKey('Pankaj'); //assume hashCode=1234 myHashMap.put(key, 'Value'); // Below code will change the key hashCode() and equals() // but it's location is not changed. Key.setName('Amit'); //assume new hashCode=7890 //below will return null, because HashMap will try to look for key //in the same index as it was stored but since key is mutated, //there will be no match and it will return null. MyHashMap.get(new MyKey('Pankaj')); This is the reason why String and Integer are mostly used as HashMap keys. • What are different Collection views provided by Map interface?