Comparator Example :
Sort By Frequency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tvidushi.arrays; | |
import java.util.Arrays; | |
import java.util.Map; | |
import java.util.TreeMap; | |
public class SortByFrequency { | |
public static void main(String[] args) { | |
sortByFrequency(new int[] {3,3,4,2,4,4, 2, 4, 4}); | |
// Sort array based on count of occurrences in ascending order | |
} | |
public static void sortByFrequency(int[] in) { | |
TreeMap<Integer, Integer> map = new TreeMap(); | |
int[] temp = new int[in.length]; | |
for(int i =0;i<in.length;i++){ | |
if(map.containsKey(in[i])){ | |
int count = map.get(in[i]); | |
map.put(in[i], count+1); | |
}else{ | |
map.put(in[i], 1); | |
} | |
} | |
Map<Integer, Integer>rm = map.descendingMap(); | |
for(Map.Entry<Integer, Integer> entry : rm.entrySet()){ | |
int frequency = entry.getValue(); | |
int value = entry.getKey(); | |
//System.out.println(" frequency "+frequency+" value "+value); | |
//System.out.println(); | |
for(int i=0;i<frequency;i++){ | |
temp[i] = value; | |
System.out.print(""+temp[i]); | |
} | |
} | |
} |
Sorting By Comparators in java 8
--
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tvidushi.java8.sorted; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Optional; | |
public class SortingMadeEasy { | |
public static List<Integer> numbers = Arrays.asList(11, 23, 13, 67,46, 51, 36, 71, 81, 19, 101); | |
public static List<String> names = Arrays.asList("Ravish", "Vidushi", "Russel", "Peter","Vir", "Das","Ashi"); | |
public static void main(String[] args) { | |
natrualOrder_num(); | |
natrualOrder_str(); | |
sortInReverseOrder(); | |
sortAsc(); | |
sortDesc() ; | |
} | |
public static void natrualOrder_num() { | |
Optional.ofNullable(numbers).ifPresent(list -> list.sort(Comparator.naturalOrder())); | |
System.out.println(numbers); | |
} | |
public static void natrualOrder_str() { | |
names.sort(Comparator.naturalOrder()); | |
//Optional.ofNullable(names).ifPresent(list -> list.sort(Comparator.naturalOrder())); | |
System.out.println(names); | |
} | |
public static void sortInReverseOrder() { | |
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia"); | |
names.sort(Collections.reverseOrder()); | |
System.out.println(names); | |
} | |
// lambda ways | |
public static void sortELement_3() { | |
System.out.println("Sorting a string array in reverse order"); | |
List<String> names = Arrays.asList("peter", "banna", "mike", "xenia"); | |
Collections.sort(names, (String a, String b) -> { | |
return b.compareTo(a); | |
}); | |
System.out.println(names); | |
} | |
public static void sortAsc() { | |
System.out.println("Sorting a string array in natural order"); | |
List<String> names2 = Arrays.asList("peter", null, "anna", "mike", "xenia"); | |
names2.sort(Comparator.nullsLast(String::compareTo)); | |
System.out.println(" _____"+names2); | |
} | |
// normal way | |
public static void sortDesc() { | |
System.out.println("Sorting a string array in reverse order"); | |
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia"); | |
Collections.sort(names, new Comparator<String>() { | |
@Override | |
public int compare(String a, String b) { | |
return b.compareTo(a); // This desending order of sorting | |
} | |
}); | |
System.out.println(names); | |
} | |
} |
--
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SortUsingLastNames { | |
public void sortLast(ArrayList<String> al) { | |
Collections.sort(al, new Comparator<String>() { | |
public int compare(String o1, String o2) { | |
String[] split1 = o1.split(" "); | |
String[] split2 = o2.split(" "); | |
String lastName1 = split1[1]; | |
String lastName2 = split2[1]; | |
if (lastName1.compareTo(lastName2) > 0) { | |
return 1; | |
} else { | |
return -1; | |
} | |
} | |
}); | |
System.out.println(al); | |
} | |
public static void main(String[] args) { | |
ArrayList<String> al = new ArrayList<String>(); | |
al.add("Daenerys Targaryen"); | |
al.add("Jon Show"); | |
al.add("Tyrion Lannister"); | |
al.add("Joffrey Baratheon"); | |
SortUsingLastNames i = new SortUsingLastNames(); | |
System.out.println("Sorted using Last Name"); | |
i.sortLast(al); | |
} | |
} |
--
No comments:
Post a Comment