Skip to content

Instantly share code, notes, and snippets.

@fmaylinch
Created December 18, 2019 21:25
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 fmaylinch/6fa964ed4b82107fc9d68baf68c3f283 to your computer and use it in GitHub Desktop.
Save fmaylinch/6fa964ed4b82107fc9d68baf68c3f283 to your computer and use it in GitHub Desktop.
import java.util.*; // This is necessary to use collections like List, Set, Map
public class Collections {
public static void main(String... args) {
// Wrapper classes ( use them between angle brackets < > )
// Integer, Double, Boolean
// Long, Character, Float
lists();
sets();
maps();
}
private static void maps() {
final Map<String, Integer> points;
points = new HashMap<>();
points.put("Angel", 10);
points.put("Ferran", 9);
points.put("Maria", 15);
int ferranPoints = points.get("Ferran");
System.out.println(ferranPoints);
// Suppose Angel gets 5 points more
int angelPoints = points.get("Angel");
angelPoints += 5;
points.put("Angel", angelPoints);
System.out.println(points);
}
private static void sets() {
final Set<String> words; // declare variable
words = new HashSet<>(); // create an empty list
words.add("zero");
words.add("one");
words.add("two");
words.add("three");
words.add("four");
words.add("five");
words.add("six");
if (words.contains("one")) {
System.out.println("Yes!");
}
if (words.contains("uno")) {
System.out.println("Sí!");
}
System.out.println("-- for in --");
int i = 0; // artificial index
for (String word : words) {
System.out.println(i + " - " + word);
i++;
}
}
private static void lists() {
final List<String> words; // declare variable
words = new ArrayList<>(); // create an empty list
words.add("zero");
words.add("one");
words.add("two");
words.add("three");
// words = Arrays.asList("cero", "uno", "dos", "tres");
if (words.contains("one")) {
System.out.println("Yes!");
}
if (words.contains("uno")) {
System.out.println("Sí!");
}
System.out.println("-- for in --");
for (String word : words) {
System.out.println(word);
}
System.out.println("-- for i --");
for (int i = 0; i < words.size(); i++) {
System.out.println(i + " -> " + words.get(i));
}
System.out.println("-- while --");
int i = 0;
while (i < words.size()) {
System.out.println(i + " -> " + words.get(i));
i++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment