Week 13 sets the job

Question 1

Title Description

Create two linear form, are stored { "chen", "wang", "liu", "zhang"} and { "chen", "hu", "zhang"}, intersection of these two tables and linear and set.

Source

package homework.thirteen;

import java.util.HashSet;
import java.util.Set;

public class One {
    public static void main(String[] args) {
        Set<String> one = new HashSet<>() {{
            add("chen");
            add("wang");
            add("liu");
            add("zhang");
        }};
        Set<String> two = new HashSet<>() {{
            add("chen");
            add("hu");
            add("zhang");
        }};
        Set<String> result = new HashSet<>(one);
        result.retainAll(two);
        System.out.println("交集:" + result);

        result.clear();
        result.addAll(one);
        result.addAll(two);
        System.out.println("并集:" + result);
    }
}

Run shot

Question 2

Title Description

Writing an application, enter a string, the string of at least a digital, three kinds of uppercase and lowercase characters is configured as "123", "a23", "56aD", "DLd", "wq" , "SSS", "4NA20", analyzes the content of the input, count the number of each of the characters, and each character and the number of display outputs. Such as: the input content is "34Ah5yWj", then the output is: Digital - a total of three, are 3,4,5; lowercase - a total of three, respectively, h, y, j; capital letters - co 2, respectively, A, W.

Source

package homework.thirteen;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

enum State {
    Lowercase("小写字母", "[a-z]"),
    Uppercase("大写字母", "[A-Z]"),
    Integer("数字", "\\d"),
    ;
    private String name;
    private String regex;

    State(String name, String regex) {
        this.name = name;
        this.regex = regex;
    }

    public String getName() {
        return name;
    }

    public String getRegex() {
        return regex;
    }

    public static int getSize() {
        return 3;
    }
}

public class Two {
    public static final ArrayList<State> STATES = new ArrayList<>(State.getSize()) {{
        add(State.Integer);
        add(State.Lowercase);
        add(State.Uppercase);
    }};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        final String input = scanner.nextLine();
        if (!input.matches("\\w+")) {
            System.out.println("输入的字符串至少由数字、大写字母和小写字母三种字符中的一种构成!");
            return;
        }

        Map<String, ArrayList> result = new HashMap<>(State.getSize());

        Function<String, String> match = s -> STATES.stream()
            .filter(e -> s.matches(e.getRegex()))
            .collect(Collectors.toList()).get(0).getName();

        Arrays.stream(input.split("")).forEach(e -> {
            result.compute(match.apply(e), (k, v) -> {
                ArrayList list = Optional.ofNullable(v).orElseGet(ArrayList::new);
                list.add(e);
                return list;
            });
        });

        result.forEach((key, value) ->
            System.out.println(key + "——共" + value.size() + "个,分别为 " +
                value.toString().replaceAll("[\\[\\]]", "")));

    }
}

Run shot

Guess you like

Origin www.cnblogs.com/jinma/p/11930563.html