YouTip LogoYouTip

Collection Compare

body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; line-height: 1.6; color: #333; max-width: 1200px; margin: 0 auto; padding: 20px; } a { color: #007bff; text-decoration: none; } a:hover { text-decoration: underline; } h1, h2, h3 { color: #2c3e50; } pre { background-color: #f8f9fa; border: 1px solid #e9ecef; border-radius: 4px; padding: 15px; overflow-x: auto; font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 0.9em; } code { font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; } .nav-links { margin-bottom: 30px; padding: 10px; background-color: #f8f9fa; border-radius: 4px; } .nav-links a { margin-right: 15px; font-size: 0.95em; } .sidebar { margin-bottom: 30px; } .sidebar h3 { margin-top: 25px; border-bottom: 2px solid #eee; padding-bottom: 5px; } .sidebar ul { list-style-type: none; padding-left: 0; } .sidebar li { margin-bottom: 8px; } .example-output { background-color: #f1f1f1; padding: 10px; border-left: 4px solid #007bff; margin: 15px 0; } .footer { margin-top: 50px; padding-top: 20px; border-top: 1px solid #eee; font-size: 0.9em; color: #666; }

Java Example - Collection Comparison |

Java Example - Collection Comparison

Java Examples Java Examples

The following example converts strings to a set and uses the Collection class's Collection.min() and Collection.max() methods to compare elements in the collection:

Main.java File

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

class Main {
    public static void main(String[] args) {
        String[] coins = {"Penny", "nickel", "dime", "Quarter", "dollar"};
        Set<String> set = new TreeSet<String>();
        for (int i = 0; i < coins.length; i++) {
            set.add(coins);
        }
        System.out.println(Collections.min(set));
        System.out.println(Collections.min(set, String.CASE_INSENSITIVE_ORDER));
        for (int i = 0; i <= 10; i++) {
            System.out.print("-");
        }
        System.out.println("");
        System.out.println(Collections.max(set));
        System.out.println(Collections.max(set, String.CASE_INSENSITIVE_ORDER));
    }
}

The output of the above code is:

Penny
dime
-----------
nickel
Quarter

Java Examples Java Examples

← Collection PrintMethod Varargs1 β†’