fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. List<LinkedHashMap<String, String>> haystack = new ArrayList<>();
  12.  
  13. LinkedHashMap<String, String> currentHashMap = new LinkedHashMap<String, String>();
  14. currentHashMap.put("id","12345");
  15. currentHashMap.put("firstName","John");
  16. currentHashMap.put("lastName", "Doe");
  17.  
  18. haystack.add(currentHashMap);
  19.  
  20. currentHashMap = new LinkedHashMap<String, String>();
  21. currentHashMap.put("id","54321");
  22. currentHashMap.put("firstName","Jack");
  23. currentHashMap.put("lastName", "Sparrow");
  24.  
  25. haystack.add(currentHashMap);
  26.  
  27.  
  28. TreeSet<String> result = searchWithinCollection(haystack, "12345", "id");
  29. System.out.println("result = " + result);
  30. }
  31.  
  32. private static TreeSet<String> searchWithinCollection(List<LinkedHashMap<String, String>> haystack, String needle, String needleKey) {
  33.  
  34. TreeSet<String> returnValueSet = new TreeSet<>();
  35.  
  36. for (Map<String, String> mappedData : haystack) {
  37. String value = mappedData.get(needleKey);
  38. if (needle.equals(value)) {
  39. returnValueSet.addAll(mappedData.values());
  40. }
  41. }
  42.  
  43. return returnValueSet;
  44. }
  45. }
Success #stdin #stdout 0.13s 320576KB
stdin
Standard input is empty
stdout
result = [12345, Doe, John]