Hierarchy package: java.lang.Comparable
Definition Sorting interface that designed for a condition by overriding compareTo method. For example, you can sort list in ascending order of size.
about the compareTo() method if compareTo() method return positive number, swap the input parameters
else will be remain same
condition return First parameter < second parameter negative First parameter == second parameter 0 First parameter > second parameter positive How to use customObject implements Comparable<>
Hierarchy package: java.util.Comparator
Definition Sorting interface that designed for mutiple special conditions by creating compare method. For example, you can sort list in ascending order of size and descending order of letters.
about the compare() method if compare() method return positive number, swap the input parameters
else will be remain same
condition return First parameter < second parameter negative First parameter == second parameter 0 First parameter > second parameter positive How to use MyComparator implements Comparator<>
Definition Interface that can read any class in the collection framework.
Thus, we can read arraylist or linkedlist or hashmap with iterator.
why we use iterator? Since we can read any list or set with iterator, we do not have to fix or change code when you work with big projects.
Hierarchy method hasNext() next() remove()
method Example List list = new ArrayList();// arrayList list.add("1"); list.add("2"); list.add("3"); Iterator <string> itr = list.
Definition enum class is for define constant. It will contain the value that will not change during the program is working
Reason for using the enum class Sometimes, the program need constant value that should not changed. We use enum class for define the value. We calls, “define Constant”
History The notion of constant is came from C language. C used define Preprocessor and const keyward.
C code example #include <stdio.
Definition Both interfaces are very similer because they are for sorting lists and arrays. However, one is for one condition and other one is for mutiple conditions.
Difference Comparable Comparator java.lang package. java.util package. Comparable affects the original class Comparator doesn’t affect the original class compareTo() method with 1 parameter compare() method with 2 parameters Collections.sort(List) Collections.sort(List, Comparator) Arrays.
Reason Thrown to indicate that a method has been passed an illegal or inappropriate argument.
argument is actual values that are passed to variables If you put any actual values that is not correct for the variable, it will cause IllegalArgumentException
Example int a = 2147483649; // value is too big int a = -2147483649; // value is too small String date="08-07-1990"; // format is dd-MM-yyyy Date format=new SimpleDateFormat("dd/MM/yyyy").parse(date);// format is different Solution check input value range check calculation that can be over the variable limitation use try-catch block
Kinds of Data Type in Java Primitive type Java provides 8 kinds of Primitive type. Primitive data types cannot contain null actual value will saved in Stack memory. data type base memory defult value range of data range of data in number boolean 1 byte false true, false true, false byte 1 byte 0 -127 ~ 128 $-2^{7}$~ $(2^{7}-1)$ short 2 byte 0 -32,768 ~ 32,767 $-2^{15}$~ $(2^{15}-1)$ int 4 byte 0 -2,147,483,648 ~ 2,147,483,647 $-2^{31}$~ $(2^{31}-1)$ long 8 byte 0L -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 $-2^{63}$~ $(2^{63}-1)$ float 4 byte 0.
Result code result char type + int type return unicode char type - int type error int type + char type return unicode int type - char type error char type + char type return unicode char type - char type return unicode String is not Primitive Data Type. This is object example of use For lexicographical order for (int i = 0; i < order.
What is Error Programs run something that is not what programmers wanted or shutdown unexpectedly. Any causes of that action are Error.
Kinds of Errors compile-time error compile failed. usually, wrong lanuage keyword used. python does not understand “System.out.println” This code is for JAVA.
runtime error crash during the programs run. put values in int arr[5] that array size is 3
logical error programmer created wrong process. you wanted to put blue in variable x.
public static ListNode reverse(ListNode head) { ListNode prev = null; while (head != null) { ListNode next = head.next; head.next = prev; prev = head; head = next; } return prev; } Time complexity : $O(n)$ Space complexity : $O(1)$ input : head node of LinkedList output : head node of LinkedList