Java面试题每日10问(24)

Java Collections Interview Questions

1. What is the advantage of Properties file?

  • If you change the value in the properties file, you don’t need to recompile the java class.
  • So, it makes the application easy to manage. It is used to store information which is to be changed frequently.
public class Test2 {
    public static void main(String[] args) throws Exception {
        Properties p = new Properties();
        FileReader in=new FileReader("C:\\PracticeCoding\\bean.properties");
        p.load(in);
        //获取配置文件中的信息
        System.out.println(p.getProperty("user"));
        System.out.println(p.getProperty("password"));
    }
}

//bean.properties
user = system
password = 123456
system
123456

2.What does the hashCode() method?

  • The hashCode() method returns a hash code value (an integer number).

  • The hashCode() method returns the same integer number if two keys (by calling equals() method) are identical.

  • it is possible that two hash code numbers can have different or the same keys.

  • If two objects do not produce an equal result by using the equals() method, then the hashcode() method will provide the different integer result for both the objects.

3.Why we override equals() method?

  • The equals method is used to check whether two objects are the same or not.
  • It needs to be overridden if we want to check the objects based on the property.

4.How to synchronize List, Set and Map elements?

Yes, Collections class provides methods to make List, Set or Map elements as synchronized

public static List synchronizedList(List l){}
public static Set synchronizedSet(Set s){}
public static SortedSet synchronizedSortedSet(SortedSet s){}
public static Map synchronizedMap(Map m){}
public static SortedMap synchronizedSortedMap(SortedMap m){}

5.What is the advantage of the generic collection?

  • If we use the generic class, we don’t need typecasting.
  • It is type-safe and checked at compile time.
  • Generic confirms the stability of the code by making it bug detectable at compile time.

6.What is hash-collision in Hashtable and how it is handled in Java?

  • Two different keys with the same hash value are known as hash-collision.

  • Two separate entries will be kept in a single hash bucket to avoid the collision.

  • There are two ways to avoid hash-collision.

    • Separate Chaining
    • Open Addressing

7.What is the Dictionary class?

  • The Dictionary class provides the capability to store key-value pairs.

8.What is the default size of load factor in hashing based collection?

  • The default size of load factor is 0.75. The default capacity is computed as initial capacity * load factor.
  • For example, 16 * 0.75 = 12. So, 12 is the default capacity of Map.

9.What do you understand by fail-fast?

  • The Iterator in java which immediately throws ConcurrentmodificationException
  • if any structural modification occurs in, is called as a Fail-fast iterator.
  • Fail-fast iterator does not require any extra space in memory.

10.What is the difference between Array and ArrayList?

Array ArrayList
The Array is of fixed size, means we cannot resize the array as per need. ArrayList is not of the fixed size we can change the size dynamically.
Arrays are of the static type. ArrayList is of dynamic size.
Arrays can store primitive data types as well as objects. ArrayList cannot store the primitive data types it can only store the objects.

你可能感兴趣的:(后端java以及面试,java,开发语言,哈希算法)