Has it ever bothered you when unknown people call you and try to impose their services? That can be annoying.
Let‘s try to solve the problem by using a blacklist. You should implement the filterPhones method that returns only the phone numbers that are not on the blacklist.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Scanner;
import java.util.stream.Collectors;
class CollectionUtils {
public static Collection<String> filterPhones(Collection<String> phones,
Collection<String> blacklist) {
// write your code here
/*for (String phone : blacklist) { //===================
if (phones.contains(phone)) { // basic for-each
phones.remove(phone); \\ loop implementation
} \\===================
}*/
//phones.removeAll(blacklist); //implementation using method from Collection
//phones.removeIf(phone -> blacklist.contains(phone)); //lambda expression
phones.removeIf(blacklist::contains); // method reference
return phones;
}
}
/* Please, do not modify this I/O code */
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Collection<String> phones = Arrays.asList(scanner.nextLine().split("\\s+"));
Collection<String> blockList = Arrays.asList(scanner.nextLine().split("\\s+"));
Collection<String> nonBlockedPhones = CollectionUtils.filterPhones(
new ArrayList<>(phones), new ArrayList<>(blockList));
System.out.println(nonBlockedPhones.stream()
.map(Object::toString)
.collect(Collectors.joining(" ")));
}
}The Collections Framework overview Blacklist
原文:https://www.cnblogs.com/longlong6296/p/13738701.html