Advertisement
desislava_topuzakova

03. Periodic Table

Jan 18th, 2022
657
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package SetsAndMapAdvanced_Exercise;
  2.  
  3. import java.util.*;
  4.  
  5. public class PeriodicTable_03 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         Set<String> uniqueElements = new TreeSet<>();
  9.         int n = Integer.parseInt(scanner.nextLine()); //брой на редовете с химични елементи
  10.  
  11.         for (int row = 1; row <= n; row++) {
  12.             String []chemicalElements = scanner.nextLine().split("\\s+"); // "Mo O Ce".split("\\s+") -> ["Mo", "O", "Ce"]
  13.             //начин 1:
  14.             /*for (String el : chemicalElements) {
  15.                 uniqueElements.add(el);
  16.             }*/
  17.  
  18.             //начин 2:
  19.             //Collections.addAll(Arrays.asList(chemicalElements), uniqueElements);
  20.  
  21.             //начин 3:
  22.             uniqueElements.addAll(Arrays.asList(chemicalElements));
  23.         }
  24.  
  25.         //сет с всички хим. елементи по 1 път
  26.         uniqueElements.forEach(el -> System.out.print(el + " "));
  27.     }
  28. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement