JetBrains Academy: Set - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Set

Creating a set:

Create TreeSet by name set in any way known to you and fill it with the following three strings "Gamma", "Alpha", "Omega" .

The code for displaying the set is already written.

import java.util.*;

public class Main {

    public static void main(String[] args) {

        TreeSet<String> set = new TreeSet<>();
        set.add("Gamma");
        set.add("Alpha");
        set.add("Omega");

        System.out.println(set);
    }
}

Output elements:

The set of strings is given.

Set<String> nameSet = new TreeSet<>(Arrays.asList("Mr.Green", "Mr.Yellow", "Mr.Red"));

Output each its element in the loop with a new line.

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Set<String> nameSet = new TreeSet<>(Arrays.asList("Mr.Green", "Mr.Yellow", "Mr.Red"));

        for(String name : nameSet){
            System.out.println(name);
        }
    }
}

Symmetric difference:

Implement a method for finding the symmetric difference of two given sets. Elements in the resulting set can be in any order.

import java.util.*;

class SetUtils {

    /**
     * @return symmetric difference between set1 and set2
     */
    public static <T> Set<T> symmetricDifference(Set<? extends T> set1, Set<? extends T> set2) {
        Set<T> difference = new HashSet<>();
        for (T element : set1) {
            if(!(set2.contains(element))){
                difference.add(element);
            }
        }

        for (T element : set2) {
            if(!(set1.contains(element))){
                difference.add(element);
            }
        }
        return difference;
    }
}

/* Do not change code below */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String strSet1 = scanner.nextLine();
        String strSet2 = scanner.nextLine();
        Set<String> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();
        if (!strSet1.equals("empty")){
            Collections.addAll(set1, strSet1.split(" "));
        }
        if (!strSet2.equals("empty")){
            Collections.addAll(set2, strSet2.split(" "));
        }
        Set<String> resultSet = SetUtils.symmetricDifference(set1, set2);
        resultSet.forEach(e -> System.out.print(e + " "));
    }
}

Processing sets:

Implement two methods. The first method should create a set from a string of numbers separated by a space. The second method should remove all numbers greater than 10 from the given set.

import java.util.*;

class SetUtils {

    public static Set<Integer> getSetFromString(String str) {
        String[] input = str.split(" ");
        SortedSet<Integer> numbers = new TreeSet<>();
        for (String s : input) {
            numbers.add(Integer.parseInt(String.valueOf(s)));
        }
        return numbers;
    }

    public static void removeAllNumbersGreaterThan10(Set<Integer> set) {
        set.removeIf((Integer element) -> element > 10);
    }

}

/* Do not change code below */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String numbers = scanner.nextLine();
        Set<Integer> set = SetUtils.getSetFromString(numbers);
        SetUtils.removeAllNumbersGreaterThan10(set);
        set.forEach(e -> System.out.print(e + " "));
    }
}

Removing duplicates and sorting:

Write a program that reads a sequence of strings from the standard input and displays them in a lexicographic order without duplicates.

Try to write your solution using a set.

Input data format:
The first line contains the size of a string sequence. Next lines contain strings.

Output data format:
A sorted sequence of strings without duplicates. Each string must be in a new line.

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int lines = scanner.nextInt();
        scanner.nextLine();
        SortedSet<String> sortedSet = new TreeSet<>();
        for (int i = 0; i < lines ; i++){
            sortedSet.add(scanner.nextLine());
        }

        for (String s : sortedSet){
            System.out.println(s);
        }
    }
}

⚠️ **GitHub.com Fallback** ⚠️