A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/collection_compare.htm below:

How to compare elements in a collection using Java

How to compare elements in a collection using Java Problem Description

How to compare elements in a collection?

Solution

Following example compares the element of a collection by converting a string into a treeset using Collection.min() and Collection.max() methods of Collection class.

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

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

The above code sample will produce the following result.

Penny
dime
----------
nickle
Quarter

java_collections.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4