Q- what is serilization?
ans-
Q-what is syncronization?
ans-
How To Convert ArrayList to String Array In Java
import java.util.ArrayList;
import java.util.List;
public class Java4s {
public static void main(String args[]){
List al = new ArrayList<String>();
al.add("One");
al.add("Two");
al.add("Three");
al.add("Four");
al.add("Five");
String[] stringArrayObject = new String[al.size()];
al.toArray(stringArrayObject);// we converted List data into String array
for(String temp : stringArrayObject)
System.out.println(temp);
}
}
How to Remove Duplicate Values From Java List/ArrayList?
RemDupFromList.java
Explanation
- Take your normal List object
- Pass that List li object to Set [Line number 22] => So finally we have Set object in our hand, just pass this current Set object as argument to ArrayList, so we got new List object li2 without duplicate
- But if you would like to preserve the order of data use LinkedHashSet rather HashS
-
How to Sort Arrays in Java, Arrays Sorting In Java
SortArray.java
package java4s;import java.util.Arrays;public class SortArray {public static void main(String... args){String[] countries = {"India","United States","Malaysia","Australia","Lundon"};Arrays.sort(countries); // Countries array values will be sorted herefor(int i=0;i<countries.length;i++){System.out.println("Countries : "+countries[i]);}}}Output
Countries : Australia
Countries : India
Countries : Lundon
Countries : Malaysia
Countries : United States
How to sort ArrayList in java, Sorting java Collections
SortCollection.java
1234567891011121314151617181920212223242526package java4s;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;public class SortCollection {public static void main(String... args){List li = new ArrayList();li.add("India");li.add("United States");li.add("Malaysia");li.add("Australia");li.add("Lundon");Collections.sort(li);for(String temp: li){
How to Convert a List to a Set in Java with Example
Sort ArrayList in Ascending and Descending order
Code Example of Sorting ArrayList in Java
Here is complete code example of sorting an arraylist in java on both natural and custom order by using Custom comparator.package test;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class ArrayListSortingExample {private static class SmartPhone implements Comparable {private String brand;private String model;private int price;public SmartPhone(String brand, String model, int price){this.brand = brand;this.model = model;this.price = price;}@Overridepublic int compareTo(SmartPhone sp) {return this.brand.compareTo(sp.brand);}@Overridepublic String toString() {return "SmartPhone{" + "brand=" + brand + ", model=" + model + ", price=" + price + '}';}}private static class PriceComparator implements Comparator{@Overridepublic int compare(SmartPhone sp1, SmartPhone sp2) {return (sp1.price < sp2.price ) ? -1: (sp1.price > sp2.price) ? 1:0 ;}}public static void main(String... args) {//creating objects for arraylist sorting exampleSmartPhone apple = new SmartPhone("Apple", "IPhone4S",1000);SmartPhone nokia = new SmartPhone("Nokia", "Lumia 800",600);SmartPhone samsung = new SmartPhone("Samsung", "Galaxy Ace",800);SmartPhone lg = new SmartPhone("LG", "Optimus",500);//creating Arraylist for sorting exampleArrayList smartPhones = new ArrayList();//storing objects into ArrayList for sortingsmartPhones.add(apple);smartPhones.add(nokia);smartPhones.add(samsung);smartPhones.add(lg);//Sorting Arraylist in Java on natural order of objectCollections.sort(smartPhones);//print sorted arraylist on natural orderSystem.out.println(smartPhones);//Sorting Arraylist in Java on custom order defined by ComparatorCollections.sort(smartPhones,new PriceComparator());//print sorted arraylist on custom orderSystem.out.println(smartPhones);}}Output:[SmartPhone{brand=Apple, model=IPhone4S, price=1000}, SmartPhone{brand=LG, model=Optimus, price=500}, SmartPhone{brand=Nokia, model=Lumia 800, price=600}, SmartPhone{brand=Samsung, model=Galaxy Ace, price=800}][SmartPhone{brand=LG, model=Optimus, price=500}, SmartPhone{brand=Nokia, model=Lumia 800, price=600}, SmartPhone{brand=Samsung, model=Galaxy Ace, price=800}, SmartPhone{brand=Apple, model=IPhone4S, price=1000}]How to sort ArrayList in Descending Order in Java
No comments:
Post a Comment