尝试创建从一个字符串返回多个字符串的方法[]

时间:2019-09-10 14:14:42

标签: java junit5

我需要创建一种方法,该方法在String数组中搜索“ string1”和“ string2”的实例,然后将其大写。

下面是对该方法的测试。

本课是关于流和lambda的。我已经为之前的8个测试创建了类似的方法,但是对于这一测试,我不知道该怎么做。

@Test
public void testMap() throws Exception {
    String[] sentence = "A long sentence of strings. string1 is repeated twice and so is a string2...".split("\\.");
    assertEquals("[STRING1, STRING1, STRING2, STRING2]", ClassWithMethods.getString1andString2(sentence).toString());

}

1 个答案:

答案 0 :(得分:0)

我已经找到了某种解决方案,它通过了测试。 谢谢任何批评。

public static List<String> getString1andString2(String[] sentence) {

    List<String> list = Arrays.asList(sentence);
    return list.stream().map(x -> x.replace(" ", "")).filter(x -> x.equalsIgnoreCase("string1") || x.equalsIgnoreCase("string2")).map(String::toUpperCase)
            .collect(Collectors.toList());

}
相关问题