fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. // n人分のデータを作り
  10. int n = 100000;
  11. Random rand = new Random();
  12. String[][] customer = new String[n][2];
  13. for (int i = 0; i < n; i++)
  14. customer[i][0] = String.valueOf((char) (rand.nextInt(26)+'a')) + i + "@ab.com";
  15. for (int i = 0; i < n; i++)
  16. customer[i][1] = i + "さん";
  17.  
  18. // メールアドレスでソートします
  19. Arrays.sort(customer, (c1, c2) -> c1[0].compareTo(c2[0]));
  20.  
  21. // キーをメールアドレス、値を名前としたハッシュマップも作ります
  22. Map<String, String> hashmap = new HashMap<>(n);
  23. for (String[] c : customer)
  24. hashmap.put(c[0], c[1]);
  25.  
  26. // 同じcustomer(101番目)の名前が取れる
  27. System.out.println(customer[101][0] + "の名前は" + customer[101][1]);
  28. // ハッシュマップからメールアドレスで取ることもできる
  29. System.out.println(customer[101][0] + "の名前は" + hashmap.get(customer[101][0]));
  30. System.out.println("確認終わり");
  31.  
  32. // では、m人分のメールアドレスを適当に選んで
  33. int m = 1000;
  34. String[] keys = new String[m];
  35. for (int j = 0; j < m; j++)
  36. keys[j] = customer[rand.nextInt(n)][0];
  37.  
  38. // 1人ずつ
  39. String[] keyCustomer = new String[2];
  40. for (int j = 0; j < m; j++){
  41. String key = keys[j];
  42. // 検索してみましょう
  43.  
  44. // TODOだった場所
  45. String found = null;
  46. keyCustomer[0] = key;
  47. int index = Arrays.binarySearch(customer, keyCustomer, (c1, c2) -> c1[0].compareTo(c2[0]));
  48. found = customer[index][1];
  49.  
  50. if (j % 100 == 0) {
  51. System.out.println(key + "の名前は" + Objects.toString(found, "不明"));
  52. }
  53. }
  54. }
  55. }
Success #stdin #stdout 1s 2184192KB
stdin
Standard input is empty
stdout
a12294@ab.comの名前は12294さん
a12294@ab.comの名前は12294さん
確認終わり
k84115@ab.comの名前は84115さん
o96078@ab.comの名前は96078さん
o81546@ab.comの名前は81546さん
t83379@ab.comの名前は83379さん
m69655@ab.comの名前は69655さん
e25711@ab.comの名前は25711さん
i52249@ab.comの名前は52249さん
f49455@ab.comの名前は49455さん
q61048@ab.comの名前は61048さん
u13342@ab.comの名前は13342さん