adsense


Showing posts with label JAVA EXAMPLES COLLECTION COMPARISON. Show all posts
Showing posts with label JAVA EXAMPLES COLLECTION COMPARISON. Show all posts

Saturday, 31 October 2015

JAVA EXAMPLES COLLECTION COMPARISON PAGE 155

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES COLLECTION COMPARISON








Java Examples - Collection Comparison



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;

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