Skip to main content

Posts

Showing posts with the label Utility Classes

What is the Simplest Way to Reverse an ArrayList?

 What is the Simplest Way to Reverse an ArrayList? Do Check My Video On Utility Classes: Reverse the element of the list mehtod : public static void reverse(List l) Example:    import java.util.ArrayList; import java.util.Collections; public class Test{     public static void main(String[] args) {         ArrayList l = new ArrayList();         l.add(3);         l.add(333);         l.add(23);         l.add(33);         l.add(43);         l.add(36);         System.out.println(l);//[3, 333, 23, 33, 43, 36]         Collections.reverse(l);         System.out.println(l);//[36, 43, 33, 23, 333, 3]     } } o/p: [3, 333, 23, 33, 43, 36] [36, 43, 33, 23, 333,3]

Sorting an ArrayList of objects using a custom sorting order in java

 Sorting an ArrayList of objects using a custom sorting order in java Do Check My Video On Utility Classes: Example: import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; public class Test{     public static void main(String[] args) {         ArrayList al = new ArrayList();         al.add(25);         al.add(35);         al.add(15);         al.add(5);         al.add(40);         System.out.println(al);//[25, 35, 15, 5, 40]         Collections.sort(al);         System.out.println(al);//[5, 15, 25, 35, 40]         System.out.println(Collections.binarySearch(al,15,new MyComparator()));//1         System.out.println(Collections.binarySearch(al,5,new MyComparator()));//0         System.out.println(Collections.binarySearch(al,17));//-3     } } class MyComparator implements Comparator{     public int compare(Object obj1,Object obj2){         Integer i1 = (Integer) obj1;         Integer i2 = (Integer) obj2;         return i1.compareTo

How to search for a string in an ArrayList in java?

 How to search for a string in an ArrayList in java? Do Check My Video On Utility Classes in java Collection class defines the following binary search method 1)public static int binarySearch(List l,Object target): if the list is sorted acc. default natural sorting order then we have to use this method 2)public static int binarySearch(List l,Object target,Comparator c): we have to use this method if the list is sorted acc. to customized sorting Conclusions:1)above method internally use binary search algorithm 2)Succesful search returns index 3)Unsuccesful search returns insertion point 4)insertion pt. is the location where we can place target elementin the sorted list 5)before calling the method the list Should be sorted otherwise we will gwt unpredictable result Note:if the list is sorted acc. to comparator object then at the time of search we have to pass same comparator object   Example: import java.util.Collections; import java.util.Comparator; import java.util.ArrayList; public cla

How to sort an ArrayList? java

How to sort an ArrayList?  Collection class defines the following two sort methods    1)public static void sort(list l)     to sort acc. to default natural sorting order     in this list should contains homogeneous object and comparable object otherwise we will     get classCastexception     list should not contain null otherwise we will get nullPointerException     2)public static void sort(List l,Comparator c)     to sort based on customized sorting Do Check My Video On Utility Classes: Example: import java.util.ArrayList; import java.util.Collection; import java.util.Collections; public class Test{          public static void main(String[] args) {         ArrayList<String> l = new ArrayList<>();         l.add("z");         l.add("a");         l.add("k");         l.add("n");         // l.add(new Integer(10));//CCE         //l.add(null);//NPE         System.out.println("Before Sorting: "+l);//[z, a, k, n]         Collection