java knowledge finishing - 2

1, abstract class can not be instantiated? why?

  An abstract class can not be instantiated because abstract classes and interfaces is designed to specify a subclass of behavioral characteristics, it is to let the other class inheritance, polymorphism is a design embodies the idea.

2, private abstract methods can be modified it? why

  Private abstract method can not be modified, because the abstract method is to subclass inherits rewrite, and private subclass can not be rewritten

3, the interface can not have a method thereof

  Before jdk8 not have an interface method thereof, a new method and a default static method after 8 jdk

4, collection

  4.1 list and set the difference

  • list allows multiple null values, set only a null value
  • list allows distinct elements, set does not permit duplicates
  • list can ensure that the storage order of each element, set not guarantee

  4.2 collection that can be automatically sorted

  TreeSet collection implements automatic sorting of elements

  4.3 vector, ArrayList, linkedlist What is the difference? Usage scenarios What is the difference?

  the difference:

  • vector is a dynamic array java provided, using synchronized to ensure thread-safe, non-thread safe if needed, is not recommended. ,
  • ArrayList is the most commonly used dynamic array itself is not thread safe
  • linkedlist doubly linked list is a collection of non-thread-safe

  scenes to be used:

  • ArrayList vector and the internal structure are stored in an array, thus very suitable for random access, but poor performance of non-tail deleted or added, such as we insert an element in the middle, it is necessary to follow the movement of all the elements for
  • linkedlist insert and delete more efficient, but more than two random access performance will be slower than dynamic array

  4.4 collection and collections difference:

  • Collection is a collection of superior class interfaces inherit it and set mainly List
  • Collections help against a set of classes, which provides a number of static methods to achieve, such as Collections.sort () to sort, Collections.reverse () in reverse order, etc.

5, a collection of Map

  Map popular implementation class:

  • Hashtable: thread-safe, does not support null keys and values
  • HashMap: support null keys and values ​​can be used in multiple threads instead of ConcurrentHashMap
  • TreeMap: providing access order is based on a red-black tree Map, itself realized the natural ordering key, you can also customize the order specified comparator
  • LinkedHashMap: HashMap subclass, the insertion order of the records stored

  HashMap and HashTable What is the difference?

  • Hashtable using the synchronized keyword to protect the security thread, and the thread-safe HashMap
  • HashMap allows K \ V are null, and HashTable are not allowed to be empty
  • AbstractMap inherited from HashMap class; and HashTable inherited from Dictionary

6, Generics

  Parameterized type is to solve the problem of uncertain type of generic nature

  Generics advantages:

  • Security: do not worry about the wrong type of conversion occurs during program execution
  • Type conversion avoided: if non-generic, acquired elements are of type Object, need to force conversion type
  • High readability: the encoding process to know the type of elements in a set

7, iterators

  An iterator is used to traverse all elements of the objects within the container, it is also a common design pattern

  Iterator contains four methods:

  • hasNext (): Are there any elements that can be accessed within the container boolean ---
  • next (): E --- return the next element
  • remove (): void --- delete the current element
  • forEachRemaining (Consumer): void --- jdk8 new in traversing through a container element Lambda Expression

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/antLoser/p/11870212.html