[Java8] Sort int type array in descending order using stream

Overview

--Memo of the place where you got caught while studying --I could use it with Integer type, but I couldn't use Comparator with int type descending order. --The conclusion seems to be possible with boxed.

Sample program

--Problems such as "Use an int type array to sort the next number in descending order and output it"

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) throws Exception {
        
        //input
        int[] numArray = {4, 3, 2, 5, 6, 7, 3, 2, 1, 9, 7, 8, 10, 6, 4, 3, 5, 8, 9};

        Arrays.stream(numArray)
                .boxed()
                .sorted(Comparator.reverseOrder())
                .forEach(i -> System.out.print(i + " "));
    }
}

Output result

10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 3 2 2 1 

By combining filter and distinct, you can narrow down and remove duplicates.

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) throws Exception {
        
        //input
        int[] numArray = {4, 3, 2, 5, 6, 7, 3, 2, 1, 9, 7, 8, 10, 6, 4, 3, 5, 8, 9};

        Arrays.stream(numArray)
                .boxed()
                .distinct()
                .filter(x -> x >= 5)
                .sorted(Comparator.reverseOrder())
                .forEach(i -> System.out.print(i + " "));
    }
}

Output result

10 9 8 7 6 5 

If you rewrite the array itself, it looks like this

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) throws Exception {
        
        //input
        int[] numArray = {4, 3, 2, 5, 6, 7, 3, 2, 1, 9, 7, 8, 10, 6, 4, 3, 5, 8, 9};
                
        numArray = Arrays.stream(numArray)
            .boxed()
            .distinct()
            .filter(x -> x >= 5)
            .sorted(Comparator.reverseOrder())
            .mapToInt(Integer::intValue)
            .toArray();
        System.out.println(Arrays.toString(numArray));
    }
}

Output result

[10, 9, 8, 7, 6, 5]

reference

Recommended Posts

[Java8] Sort int type array in descending order using stream
Sort List in descending order in Java and generate a new List non-destructively
Map without using an array in java
Sort by multiple conditions using Java Stream
[JAVA] Stream type
[java] sort in list
How to sort in ascending / descending order with SQLite
Sort Map values in ascending key order in Java TreeMap
[Java] List type / Array type conversion
Try using RocksDB in Java
[Neta] Sleep Sort in Java
[Java, stream] Sort object list in Japanese by property name
Try functional type in Java! ①
Java Stream API in 5 minutes
Use Redis Stream in Java
Sorting in ascending order in Java (bubble sort: simple exchange algorithm)
Generate Stream from an array of primitive types in Java
Bubble sort using ArrayList (JAVA)
I tried to sort the data in descending order, ascending order / Rails
Encrypt using RSA cryptography in Java
I tried using Java8 Stream API
HTTPS connection using tls1.2 in Java 6
I tried using JWT in Java
Java array / list / stream mutual conversion list
Do you use Stream in Java?
[Java] Express Enum type without using Enum (enumeration) type
Member description order in Java coding convention
I tried using Elasticsearch API in Java
When seeking multiple in a Java array
Regarding String type equivalence comparison in Java
Data processing using stream API from Java 8
[Easy-to-understand explanation! ] Reference type type conversion in Java
Notes on operators using Java ~ String type ~
Using JavaScript from Java in Rhino 2021 version
Organized memo in the head (Java --Array)
Read Felica using RC-S380 (PaSoRi) in Java
Difference between int and Integer in Java
Implemented basic search / sort algorithm in Java
[Java] How to convert one element of a String type array to an Int type