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. for (int j = 0; j < m; j++){
  40. String key = keys[j];
  41. // 検索してみましょう
  42.  
  43. // TODOだった場所
  44. String found = null;
  45. for (int i = 0; i < n; i++) {
  46. if (Objects.equals(key, customer[i][0])) {
  47. found = customer[i][1];
  48. }
  49. }
  50.  
  51. if (j % 100 == 0) {
  52. System.out.println(key + "の名前は" + Objects.toString(found, "不明"));
  53. }
  54. }
  55. }
  56. }
Success #stdin #stdout 3.9s 2184192KB
stdin
Standard input is empty
stdout
a127@ab.comの名前は127さん
a127@ab.comの名前は127さん
確認終わり
q37404@ab.comの名前は37404さん
e94702@ab.comの名前は94702さん
r85472@ab.comの名前は85472さん
a37276@ab.comの名前は37276さん
v95985@ab.comの名前は95985さん
i86565@ab.comの名前は86565さん
g6720@ab.comの名前は6720さん
v83573@ab.comの名前は83573さん
h47992@ab.comの名前は47992さん
t78449@ab.comの名前は78449さん