首页 > 其他 > 详细

The Collections Framework overview Blacklist

时间:2020-09-27 12:45:29      阅读:30      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!