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.  
  46. if (j % 100 == 0) {
  47. System.out.println(key + "の名前は" + Objects.toString(found, "不明"));
  48. }
  49. }
  50. }
  51. }
Success #stdin #stdout 0.9s 2184192KB
stdin
Standard input is empty
stdout
a12195@ab.comの名前は12195さん
a12195@ab.comの名前は12195さん
確認終わり
a33858@ab.comの名前は不明
n73809@ab.comの名前は不明
t85175@ab.comの名前は不明
r40300@ab.comの名前は不明
z11436@ab.comの名前は不明
y69998@ab.comの名前は不明
i94208@ab.comの名前は不明
f688@ab.comの名前は不明
r6300@ab.comの名前は不明
n76526@ab.comの名前は不明