fork download
  1. // package seupackage.
  2.  
  3. import java.util.Arrays;
  4. import java.util.LinkedHashSet;
  5. import java.util.Set;
  6.  
  7. public class Main {
  8.  
  9. public static class Party {
  10.  
  11. private final Set<String> players;
  12.  
  13. public Party() {
  14. this.players = new LinkedHashSet<>();
  15. }
  16.  
  17. public void toggle(String player) {
  18. if (players.contains(player)) {
  19. players.remove(player);
  20. } else {
  21. players.add(player);
  22. }
  23. }
  24.  
  25. public String[] toArray() {
  26. return players.toArray(new String[players.size()]);
  27. }
  28.  
  29. public Set<String> toSet() {
  30. return new LinkedHashSet<>(players);
  31. }
  32. }
  33.  
  34. public static void main(String[] args) {
  35. Party p = new Party();
  36. p.toggle("Pedro"); // Adiciona o Pedro
  37. System.out.println(Arrays.asList(p.toArray())); // Imprime [Pedro]
  38. p.toggle("Maria"); // Adiciona a Maria
  39. System.out.println(Arrays.asList(p.toArray())); // Imprime [Pedro, Maria]
  40. p.toggle("Pedro"); // Remove o Pedro
  41. System.out.println(Arrays.asList(p.toArray())); // Imprime [Maria]
  42. p.toggle("Carlos"); // Adiciona o Carlos
  43. p.toggle("Fernando"); // Adiciona o Fernando
  44. System.out.println(p.toSet()); // Imprime [Maria, Carlos, Fernando]
  45. p.toggle("Carlos"); // Remove o Carlos
  46. System.out.println(p.toSet()); // Imprime [Maria, Fernando]
  47. }
  48. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
[Pedro]
[Pedro, Maria]
[Maria]
[Maria, Carlos, Fernando]
[Maria, Fernando]