java Intermediate knowledge points inductive (a)

A difference of abstract classes and interfaces

  • An abstract class may contain constructors, and the interface can not have.
  • An abstract class can have an ordinary member variables, and the interface can not have.
  • An abstract class may contain a non-abstract conventional method, and the interface must be in all the abstract methods, can not have non-abstract ordinary method.
  • Access Type abstract method of an abstract class can be public, protected and default type, but abstract method only public interface and the default type.
  • An abstract class may contain static methods, static methods can not contain the interface.
  • Abstract classes and interfaces can contain static member variables, static member variables of type of access abstract class can be any, but the variables defined in the interface can only be a public static type, and the default is public static type.
  • A class can implement multiple interfaces, but can only inherit an abstract class.
  • More interfaces play a role in the system frame design method, the communication between the primary definition module, and abstract classes play a role in implementing aspects of the code, code reuse may be implemented.

Second, the run-time data area has a few java virtual machine? Threads and threads share private area what?

  1. Program Counter: thread private, bytecode execution of the current thread line number indicator.
  2. VM stack: thread private, storing basic data types, object references and returnAddress type.
  3. Native method stacks: Use the virtual machine to Native method service.
  4. java heap: thread shared instance of an object store, as well as the main GC area collector management.
  5. Methods District: threads share, store class information has been loaded in the virtual machine, constants, static variables, the real-time data such as compiled code.
  6. Runtime constant pool: zone part of the method, and store a variety of literal symbols compile-generated reference.
  7. Direct Memory: not part of the virtual machine runtime data area, nor is java virtual machine memory area defined in the specification, easily lead to abnormal OOM, NIO calls, free Java heap size limit.

Three, HashMap and HashTable difference?

  1. HashTable is based on obsolete Dictionary class, HashMap is a realization Java1.2 the introduction of the Map interface.
  2. HashTable method is synchronized, and HashMap solution is not, therefore HashTable are thread-safe, but on the efficiency of the code is slower than HashMap.
  3. HashMap allow nulls and dangling bonds, but not HashTable.
  4. HashMap asynchronous implement the Map interface, the data structure is a linked list array [], the maximum load is 16 automatically becomes long, the Entry is determined by the [] control (key, value, next), hashCode () key if repeated.
  5. Recommendation: If you need to do synchronization, use ConcurrentHashMap, reducing lock granularity. On the basis of the HashMap, dividing the data into a plurality of ConcurrentHashMap segment, default is 16 (concurrency level), and each operation on a segment lock, the lock is to avoid the chance of multithreading, increasing concurrency efficiency. Concurrent read here, except for the key value than the corresponding null, the lock is not used.

Fourth, the difference between ArrayList and LinkedList?

  1. ArrayList based on an array to achieve, based on the list LinkedList ArrayList implement additions and deletions than LinkedList slow, but LinkedList need to find the time to find a recursive, efficiency slower than ArrayList. About multi-threading, thread-safe if required, there is a Vector, but more and more use is CopyOnWriteArrayList instead of ArrayList, CopyOnWriteArrayList suitable for use in a read operation is much greater than the scene of a write operation, such as caching. It does occur when you modify copy, old and new versions of separation, to ensure high performance read for read-mostly the case.

Five, Set Interface

  1. HashSet is a typical implementation of the Set interface, HashSet use the hash algorithm to store elements, it has good access and lookup performance. Disadvantages: can not guarantee the order of the elements, the order may change; HashSet is asynchronous; collection element values ​​may be null; when the HashSet into a collection element, HashSet will call the object's hashCode () method to get hashCode value of the object, and then to determine the position of the object based on the stored HashSet hashCode value. There is a subclass of a LinkedHashSet HashSet, which is a set of storage elements is determined according to the position of the element hashCode value, but it is also used to maintain the order of the list of elements, so that elements appear in order of insertion is stored, that is, when LinkedHashSet when traversing a collection of elements, it will be added according to the order of elements to access elements in the collection. So LinkedHashSet performance is slightly lower than HashSet, but in the iterative access to all the elements will have a good performance, because it lists maintain internal order.
  2. TreeSet is the only achieve SortSet interface, TreeSet ensure collection of elements in the sort state. Insert element according TreeSet not sorted order, but ordered according to the value of the element. TreeSet supports two sorting methods: natural sorting and custom ordering.
  3. EnumSet All values ​​must be specified enumerated type value, its elements also ordered to define enumeration values ​​in the order of enumeration of columns to determine the order of elements in the collection. EnumSet collection does not permit null elements added, otherwise throw NPE (NullPointerException) exception. EnumSet without exposing any class constructor to create an instance of the class, the program should be created by objects EnumSet it provides static methods.
  4. Summary: They are thread safe. Collection Set is not containing repetitive elements, i.e., any two elements e1 and e2 are e1.equals (e2) = false, Set up to a null element. A.   HashSet performance better than TreeSet, because TreeSet require additional red-black tree algorithm to maintain the order of collection element, only when needed to keep a sort of Set, will use TreeSet. B.   EnumSet best performance, but it can only save enumeration values.
  5. About HashSet, number and capacity of entry and in terms of iterations is linear. Therefore, if iteration performance is important, it should carefully select an appropriate initial capacity. Capacity election is too large, a waste of space, but also a waste of time. The default initial capacity is 101, in general, it's more than you need. Int constructor can specify the initial capacity. as follows:

 

Set s = new HashSet(15)

 

  

 

Guess you like

Origin www.cnblogs.com/tizer/p/11329765.html