in Education by
Just curious to know on whether we can sort a List according to the frequency of repeated numbers using Java 8 without writing a Custom Comparator class. I need to sort the given integers based on its frequency and then by the natural numerical order. I'm getting error at Comparator.naturalOrder(); Here is the code which I tried: Integer[] given = new Integer[]{0,0,1,22,11,22,22,11,44,555,55,66,77,88,99}; List intList = Arrays.asList(given); Map frequencyMap = intList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); List newList = intList.stream().sorted(Comparator.comparing(frequencyMap::get).thenComparing(Comparator.naturalOrder())).collect(Collectors.toList()); System.out.println(newList.toString()); The expected output is [1, 44, 55, 66, 77, 88, 99, 555, 0, 0, 11, 11, 22, 22, 22] PS: Used arrays in first line in order to avoid list.add() in multiple lines and for clear understanding. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Unfortunately, Java’s type inference can’t recognize the type of the compared object when chaining Comparator.comparing(frequencyMap::get) with thenComparing(Comparator.naturalOrder()). Since the method signature of Map.get is get(Object), the compiler infers Comparator as result type of Comparator.comparing(frequencyMap::get). You can fix this by inserting an explicit type. But note that you are not using the result of collect(Collectors.toList()) but just printing the original, unaffected List. On the other hand, you don’t need the List when the array is given: Integer[] given = {0,0,1,22,11,22,22,11,44,555,55,66,77,88,99}; Map frequencyMap = Arrays.stream(given) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); Arrays.sort(given, Comparator.comparingLong(frequencyMap::get) .thenComparing(Comparator.naturalOrder())); System.out.println(Arrays.toString(given)); For printing without changing the array you can also use the following alternative Arrays.stream(given) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet().stream() .sorted(Map.Entry.comparingByValue() .thenComparing(Map.Entry.comparingByKey())) .flatMap(e -> LongStream.range(0, e.getValue()).mapToObj(l -> e.getKey())) .forEach(System.out::println); This sorts the groups instead of the individual values and prints identical values as often as they’ve been counted.

Related questions

0 votes
    Apigee cannot modify the backend service implementation without affecting the public API. True False...
asked Sep 3, 2021 in Technology by JackTerrance
0 votes
    Can we run business warehouse without SAP R/3 implementation?...
asked Jan 4, 2021 in Technology by JackTerrance
0 votes
    I am trying to sort an array of objects by the name field but the ordering is wrong. The order I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    An irregular time series data is stored in a pandas.DataFrame. A DatetimeIndex has been set. I need the ... seemingly simple operation? Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
0 votes
    A filter which passes without attenuation all frequencies up to the cut-off frequency fc and attenuates all other ... EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Which class provides thread safe implementation of List? (a) ArrayList (b) CopyOnWriteArrayList (c) HashList (d) ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these can be used to fully abstract a class from its implementation? (a) Objects (b) Packages (c) ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which class provides system independent server side implementation? (a) Socket (b) ServerSocket (c) Server (d) ... Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which class provides thread safe implementation of List? (a) ArrayList (b) CopyOnWriteArrayList (c) HashList ( ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
0 votes
    Which class provides system independent server side implementation? (a) Socket (b) ServerSocket (c) Server ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
+1 vote
    Which is the correct implementation class of BeanFactory in Spring? A - XmlBeanFactory B - ClassPathBeanFactory C - FileSystemBeanFactory D - AdvancedBeanFactory...
asked Oct 14, 2020 in Technology by JackTerrance
0 votes
    for sorting data which component we can use in Talend?...
asked Mar 16, 2021 in Technology by JackTerrance
0 votes
    Which of these class is used for creating a client for a server-client operations? (a) serverClientjava (b) ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
...