daily pastebin goal
69%
SHARE
TWEET

String Sorting V2 With Interfaces and Lambdas

a guest Sep 23rd, 2017 61 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. package chapter7;
  2.  
  3. import java.util.*;
  4.  
  5. public class StringSorting2 {
  6.  
  7.     private static final Random random = new Random();
  8.     private static final char[] symbols = new char[62];
  9.  
  10.     static {
  11.         for (int idx = 0; idx < 10; ++idx)
  12.             symbols[idx] = (char) ('0' + idx);
  13.         for (int idx = 10; idx < 36; ++idx)
  14.             symbols[idx] = (char) ('a' + idx - 10);
  15.         for (int idx = 36; idx < 62; ++idx)
  16.             symbols[idx] = (char) ('A' + idx - 36);
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         Scanner sc = new Scanner(System.in);
  21.         System.out.print("Enter array size: ");
  22.         int size = sc.nextInt();
  23.         System.out.print("Min String Length: ");
  24.         int minlen = sc.nextInt();
  25.         System.out.print("Max String Length: ");
  26.         int maxlen = sc.nextInt();
  27.         sc.close();
  28.        
  29.         String baseData[] = generateRandomStrings(size, minlen, maxlen);
  30.         printData("Base Data", baseData);
  31.        
  32.         String data[] = copyArray(baseData);
  33.         sortGeneric(data, new Sensitive());
  34.         printData("Case Insensitive Generic Sort", data);
  35.        
  36.         data = copyArray(baseData);
  37.         sortGeneric(
  38.             data,
  39.             (x, y) -> {return x.compareToIgnoreCase(y) > 0;}
  40.             );
  41.         printData("Case Insensitive Generic Sort", data);
  42.        
  43.     }
  44.  
  45.     private static void sortGeneric(String[] data, Sorter method) {
  46.         for (int i = 0; i < data.length - 1; i++)
  47.             for (int j = i + 1; j < data.length; j++) {
  48.                 if (method.compareto(data[i], data[j])) {
  49.                     String temp = data[i];
  50.                     data[i] = data[j];
  51.                     data[j] = temp;
  52.                 }
  53.             }
  54.     }
  55.    
  56.     private static String[] copyArray(String[] baseData) {
  57.         String data[] = new String[baseData.length];
  58.         for (int i = 0; i < baseData.length; i++) {
  59.             data[i] = baseData[i];
  60.         }
  61.         return data;
  62.     }
  63.  
  64.     private static String[] generateRandomStrings(int size, int min, int max) {
  65.         String data[] = new String[size];
  66.         for (int i = 0; i < data.length; i++) {
  67.             data[i] = generateString(min, max);
  68.         }
  69.         return data;
  70.     }
  71.  
  72.     private static String generateString(int min, int max) {
  73.         if (min > max)
  74.             throw new IllegalArgumentException("min: " + min + " > max: " + max);
  75.         int length = min + random.nextInt(max - min + 1);
  76.         String x = "";
  77.         for (int i = 0; i < length; i++) {
  78.             x += symbols[random.nextInt(symbols.length)];
  79.         }
  80.         return x;
  81.     }
  82.  
  83.     private static void printData(String header, String[] data) {
  84.         System.out.println(header);
  85.         System.out.println();
  86.         for (int i = 0; i < data.length; i++) {
  87.             System.out.println(data[i]);
  88.         }
  89.         System.out.println();
  90.         System.out.println();
  91.     }
  92.    
  93. }
  94.  
  95.  
  96. //************************************************************************
  97.  
  98. interface Sorter {
  99.     boolean compareto(String x, String y);
  100. }
  101.  
  102. class Sensitive implements Sorter {
  103.     @Override
  104.     public boolean compareto(String x, String y) {
  105.         return x.compareTo(y) > 0;
  106.     }
  107. }
  108.  
  109. class Insensitive implements Sorter {
  110.     @Override
  111.     public boolean compareto(String x, String y) {
  112.         return x.compareToIgnoreCase(y) > 0;
  113.     }  
  114. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top