Skip to content

Instantly share code, notes, and snippets.

@TomTasche
Last active May 31, 2016 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomTasche/3018f32ca54fb06e3f91960c643acb01 to your computer and use it in GitHub Desktop.
Save TomTasche/3018f32ca54fb06e3f91960c643acb01 to your computer and use it in GitHub Desktop.
List<String> data = new LinkedList<String>();
// or if you know the amount of data upfront:
List<String> data = new ArrayList<String>(3);
data.add("hello");
data.add("hello");
data.add("bye");
System.out.println(data.size()); // 3
// in order to lookup data you always have to iterate the whole list until you find the value you're looking for
for (String dataString : data) {
if (dataString.equals("bye")) {
System.out.println("data found");
}
}
Map<String, String> data = new HashMap<String, String>();
data.put("greeting", "hello");
data.put("greeting", "hello");
data.put("farewell", "bye");
System.out.println(data.size()); // 2
// you can look up data efficently using contains
if (data.contains("farewell")) { // or: data.get("farewell").equals("bye")
System.out.println("data found");
}
Set<String> data = new HashSet<String>();
data.put("hello");
data.put("hello");
data.put("bye");
System.out.println(data.size()); // 2
// you can look up data efficently using contains
if (data.contains("bye")) {
System.out.println("data found");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment