1. ArrayDequeue is implemetation of Deque.
    –> Double ended queue, also called as Deck.
    –> “offer” and “offerLast” method adds the element to the end of the queue.
    –> “peek” and “peekFirst” methods retrieves element from top of the queue, does not remove it.
    –> “push” and “offerFirst” inserts in the front of the queue.
    –> “addFirst” inserts in the front of the queue.
    –> “poll” and “pop” removes the first element from the queue and also returns it.
    –> “removeFirst” removes the first element from the queue and also returns it.
    –> “removeLast” removes the last element from the queue and also returns it.
    –> addFirst and addLast method are in the Deque interface, but they are not in Queue interface
  2. Arrays
    –> The array brackets, [], are not allowed to appear before the type, making the declaration incorrect.
    –> When using an array initializer with braces, {}, you are not allowed to specify the size separately. The size is inferred from the number of elements listed.
    –> When you’re not using an array initializer, the size is required.
    –> An empty array initializer is allowed.
    –> For multidimensional array declaration, [][] may appear before and/or after variable declaration.
    –> For declaration var[][] data; is not valid. if you are using var, then it should be just var data and ofcourse it has to be initialized on the same line.
    –> When creating an array object, a set of elements or size is required. The brackets containing the size are required to be after the type.
    –> Arrays.compare(array1, array2) method looks at each element in turn, even though array 1 and array 2 are of differen size. Since the first elements are different, we get the result of comparing them. In this case, we get a positive number because 3 is larger than 2.
    –> The Arrays.mismatch() method returns -1 if the arrays are the same and returns the index of the first difference if they are not.
    –> Arrays.sort() and Arrays.binarySearch() are the methods. linearSort is not a valid method and search is also not a valid method.
    –> The array must be sorted before calling Arrays.binarySearch() to get an accurate result.
    –> if no arguments are passed from the command line, this creates an empty array of that String … args. so args is an empty array and not a null.
  3. public class News<__________> {}.
    –> Here in the <> they are expecting a valid variable name, so N is ok.
    –> Even News is ok, remember News here doesn’t represent the News class but its just a variable name. Even Object can be used as variable name. It should follow standard java naming rules.
    –> Using “News” and “Object” is wierd but it is valid. You cannot use ? as ? is not valid java identifer.
  4. the ? wildcard
    –> cannot be used when instantiating a type on the right side of the assignment operator
    –> The generic declaration if the question mark (?) wildcard is not allowed when specifying a constraint on a class. While a question mark is allowed on the left side of a declaration, it is not allowed when specifying a constraint on a class
  5. Java talks about the Collections Framework, but the Map interface does not actually implement the Collection interface. TreeMap has different method
  6. Collections.binarySearch
    –> It needs the list to be sorted or it returns -1 (or undefined), even if the elements is present.
    –> if the list is sorted, it returns the correct index. if the list is sorted and the element is not present, then it returns the index as follows. so if that element which is not found can be inserted at position 2 (0 based). Then take 2, make it minus. so -2 and subtract 1. so -3 would be returned.
    –> Collections.binarySearch (list), assumes that list is sorted in ascending order and tries to search
    –> if you pre-sort a list in different order using different comparator, you also have to give same comparator during search then. So Collections.binarySearch(list, comparator)
    –> if you pre-sort a list in different order using different comparator, and simply call Collections.binarySearch(list) the result is undefined.
    –> When a different sort order is used for searching and sorting, the result is undefined.
  7. Tree sets sorts and doesn’t allow duplicates
  8. While varargs is used like an array from within the method, it can only be used as a method parameter. This syntax is not allowed for a variable declaration
  9. –> TreeSet does not allow null values because it compares the values when they are added, leading to a NPE.
    –> HashSet does support null values, although you can have only one as HashSet guarantees uniqueness.
    –> ArrayList and LinkedList both support any number of null
  10. Elements of Set are unordered, they do not have index. so Set.set(1, ‘a’) is not a valid method. set(index, ‘a’) is valid only for List as list maintains the order.
  11. if the class/objects beind added in treeSet doesn’t implement comparable interface, it will still compile but will throw an class cast exception during runtime, when you try to add elements to treeset of that class/object. TreeMap and TreeSet sort as you insert elements. TreeMap sorts the keys and TreeSet sorts the objects in the set.
  12. Comparable vs Comparator,
    –> When implementing Comparable you implement the compareTo() method.
    –> When implementing Comparator interface uses the compare() method, and the method takes two parameters.
    –> the negative value says that s1 should sort first, positive value indicates swap, 0 indicates stay at same position.
    –> Comparator.comparing(String::length) sorts ascendingly by length
    –> Comparator.comparing(String::length).reversed() sorts descendingly by length
    –> Usually when you specify the second parameter minus first parameter the order is descending then. so (x, y) -> y – x; when used for list of integers it will sort in descending order.
    –> Numbers sort before letters. Second, uppercase sorts before lowercase.
    –> Shorter strings sort before longer strings, so “1” is before “10”
  13. When you declare formal type parameter for a class using generics, the best way is to declare the type of that formal parameter in declaration.
    –> You can also not specify the type, it will still compile but will show you a warning. if you do not specify the parameter it will treat as “Object”.
    –> So final Wash wash = new Wash(); this is still not the best way, as Wash type parameter is still Object and not String, because it is important that is should be on declaration side, so something like this final Wash wash = new Wash();
    –> But you cannnot have empty diamond operator <> on declartion side. So this is NOT ok final Wash<> wash = new Wash();
    –> The <> is known as the diamond operator. Here, it works as a shortcut to avoid repeating the generic type twice for the same declaration. On the right side of the expression, this is a handy shortcut. Java still needs the type on the left side, so there is something to infer. P
  14. Immutable list/set
    –> List.of() and Set.of() returns immutable list, but they accept varargs as parameter.
    –> List.copyOf() and Set.copyOf() also returns immutable list, but they accept list as parameter.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.