Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. **2. Пояснити що таке Comparator i Comparable. В чому їхня відмінність?**
  2.  
  3. Comparable is meant for objects with natural ordering which means the object itself must know how it is to be ordered. Whereas, Comparator interface sorting is done through a separate class.
  4. Logically, Comparable interface compares “this” reference with the object specified and Comparator in Java compares two different class objects provided.
  5. If any class implements Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Arrays.sort() method and objects will be sorted based on there natural order defined by CompareTo method.
  6. If sorting of objects needs to be based on natural order then use Comparable whereas if you sorting needs to be done on attributes of different objects, then use Comparator.
  7.  
  8. **3. Чому в PriorityQueue MAX capacity - MAX_INTEGER- 8? І чому initial capacity 11?**
  9.  
  10. a) PrirityQueue backed by an array of Objects. Which the maximum size of array can we allocate? Some VMs reserve some header words in an array. Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit. That's why MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8. 8 - is reserved to some header words in an array.
  11.  
  12. b) Since 1.5. Priority queue represented as a balanced binary heap: the two children of queue[n] are queue[2*n] and queue[2*n + 1]. The priority queue is ordered by comparator, or by the elements' natural ordering, if comparator is null: For each node n in the heap and each descendant d of n, n <= d.The element with the lowest value is in queue[1], assuming the queue is nonempty. (A one-based array is used in preference to the traditional zero-based array to simplify parent and child calculations.) queue.length must be >= 2, even if size == 0. (queue is an array of Objects)
  13. In ArrayList (backed by an array too) initial capacity is 10, maybe in PriorityQueue initial capacity is 11, because index 0 does not used:)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement