Last active
May 12, 2023 07:06
-
-
Save TGITS/63482004d7d0997d6d9e832295081906 to your computer and use it in GitHub Desktop.
Exemple de création d'un tableau de fréquence en Java avec une boucle for classique
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
/** | |
* A exécuter avec JBang : jbang frequencies-map-with-for.jsh | |
* <p> | |
* Ou ouvrir dans JShell : /open frequencies-map-with-for.jsh | |
*/ | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
List<String> daysOfWeek = List.of("Friday", "Thursday", "Thursday", "Saturday", "Thursday", "Thursday", "Monday", "Saturday", "Friday", "Saturday"); | |
Map<String, Integer> frequencies = new HashMap<>(); | |
int previousCount; | |
for (String day : daysOfWeek) { | |
previousCount = frequencies.getOrDefault(day, 0); | |
frequencies.put(day, previousCount + 1); | |
} | |
frequencies.entrySet().iterator().forEachRemaining(System.out::println); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment