Created
December 18, 2019 21:25
-
-
Save fmaylinch/6fa964ed4b82107fc9d68baf68c3f283 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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