How do I sort items of an ArrayList?

This example shows you how we can sort items of an ArrayList using the Collections.sort() method. Beside accepting the list object to be sorted we can also pass a Comparator implementation to define the sorting behaviour such as sorting in descending or ascending order.

package org.kodejava.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayListSortExample {
    public static void main(String[] args) {
        /*
         * Create a collections of colours
         */
        List<String> colours = new ArrayList<>();
        colours.add("red");
        colours.add("green");
        colours.add("blue");
        colours.add("yellow");
        colours.add("cyan");
        colours.add("white");
        colours.add("black");

        /*
         * We can sort items of a list using the Collections.sort() method.
         * We can also reverse the order of the sorting by passing the
         * Collections.reverseOrder() comparator.
         */
        Collections.sort(colours);
        System.out.println(Arrays.toString(colours.toArray()));

        colours.sort(Collections.reverseOrder());
        System.out.println(Arrays.toString(colours.toArray()));
    }
}

The code will output:

[black, blue, cyan, green, red, white, yellow]
[yellow, white, red, green, cyan, blue, black]

How do I sort array values in case-insensitive order?

By default, when sorting an arrays the value will be ordered in case-sensitive order. This example show you how to order it in case-insensitive order.

package org.kodejava.util;

import java.util.Arrays;

public class SortArrayCaseSensitivity {
    public static void main(String[] args) {
        String[] teams = new String[5];
        teams[0] = "Manchester United";
        teams[1] = "chelsea";
        teams[2] = "Arsenal";
        teams[3] = "liverpool";
        teams[4] = "EVERTON";

        // Sort array, by default it will be sorted in case-sensitive order.
        // [Arsenal, EVERTON, Manchester United, chelsea, liverpool]
        Arrays.sort(teams);
        System.out.println("Case sensitive  : " + Arrays.toString(teams));

        // Sort array in case-insensitive order
        // [Arsenal, chelsea, EVERTON, liverpool, Manchester United]
        Arrays.sort(teams, String.CASE_INSENSITIVE_ORDER);
        System.out.println("Case insensitive: " + Arrays.toString(teams));
    }
}

The result of the code snippet above:

Case sensitive  : [Arsenal, EVERTON, Manchester United, chelsea, liverpool]
Case insensitive: [Arsenal, chelsea, EVERTON, liverpool, Manchester United]

How do I sort array values in descending order?

Here you will find an example on how to sort the values of an array in ascending or descending order.

package org.kodejava.util;

import java.util.Arrays;
import java.util.Collections;

public class SortArrayWithOrder {
    public static void main(String[] args) {
        Integer[] points = new Integer[5];
        points[0] = 94;
        points[1] = 53;
        points[2] = 70;
        points[3] = 44;
        points[4] = 64;
        System.out.println("Original  : " + Arrays.toString(points));

        // Sort the points array, the default order is in ascending order.
        // [44, 53, 64, 70, 94]
        Arrays.sort(points);
        System.out.println("Ascending : " + Arrays.toString(points));

        // Sort the points array in descending order.
        // [94, 70, 64, 53, 44]
        Arrays.sort(points, Collections.reverseOrder());
        System.out.println("Descending: " + Arrays.toString(points));
    }
}

The result of the code snippet above are:

Original  : [94, 53, 70, 44, 64]
Ascending : [44, 53, 64, 70, 94]
Descending: [94, 70, 64, 53, 44]

How do I store properties as XML file?

In the previous example, How do I load properties from XML file? we read properties from XML file. Now it’s the turn on how to store the properties as XML file.

package org.kodejava.io;

import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class PropertiesToXml {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.setProperty("db.type", "mysql");
        properties.setProperty("db.url", "jdbc:mysql://localhost:3306/kodejava");
        properties.setProperty("db.username", "root");
        properties.setProperty("db.password", "");

        FileOutputStream fos = new FileOutputStream("db-config.xml");
        properties.storeToXML(fos, "Database Configuration", StandardCharsets.UTF_8);
    }
}

The saved XML file will look like the properties file below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>Database Configuration</comment>
    <entry key="db.type">mysql</entry>
    <entry key="db.password"></entry>
    <entry key="db.username">root</entry>
    <entry key="db.url">jdbc:mysql://localhost:3306/kodejava</entry>
</properties>

How do I load properties from XML file?

Reading XML properties can be easily done using the java.util.Properties.loadFromXML() method. Just like reading the properties from a file that contains a key=value pairs, the XML file will also contain a key and value wrapped in the following XML format.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>Application Configuration</comment>
    <entry key="data.folder">D:\AppData</entry>
    <entry key="jdbc.url">jdbc:mysql://localhost/kodejava</entry>
</properties>
package org.kodejava.util;

import java.util.Properties;

public class LoadXmlProperties {
    public static void main(String[] args) {
        LoadXmlProperties demo = new LoadXmlProperties();
        try {
            Properties properties = demo.readProperties();

            //Display all properties information
            properties.list(System.out);

            // Read the value of data.folder and jdbc.url configuration
            String dataFolder = properties.getProperty("data.folder");
            System.out.println("data.folder = " + dataFolder);
            String jdbcUrl = properties.getProperty("jdbc.url");
            System.out.println("jdbc.url    = " + jdbcUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Properties readProperties() throws Exception {
        Properties properties = new Properties();
        properties.loadFromXML(getClass().getResourceAsStream("/configuration.xml"));
        return properties;
    }
}

The result of the code snippet above:

-- listing properties --
data.folder=D:\AppData
jdbc.url=jdbc:mysql://localhost/kodejava
data.folder = D:\AppData
jdbc.url    = jdbc:mysql://localhost/kodejava