1

I would like to implement a method isInBlackList(String element), which return true, if element is founded in the blacklist. I have the following code:

public boolean isInBlackList(String element) {
        Set<String> blackList = new HashSet<String>();
        blackList.add("element1");
        blackList.add("element2");
        ...
        .....
        blackList.add("element20");

        return blackList.contains(element);
    }

What is bothering me is that I have to write add() method 20 times. Any better idea? it does not have to be implemented with Set.

7
  • 3
    Why are you adding, what appears to be, random elements to your blacklist when checking if an element is in the blacklist? Commented Apr 4, 2014 at 9:41
  • Well, is there better way to initial the blacklist with my 20 Elements? Commented Apr 4, 2014 at 9:42
  • 1
    I'm guessing you only want to initialize it once Commented Apr 4, 2014 at 9:42
  • @ᴋᴇʏsᴇʀ that is right Commented Apr 4, 2014 at 9:43
  • Then look at my first comment :p Commented Apr 4, 2014 at 9:43

5 Answers 5

3

You may try like this:

Set<String> blackList = new HashSet<String>(Arrays.asList("element1", "element2", "element3"));
Sign up to request clarification or add additional context in comments.

Comments

2

The simplest way I can think of is the following:

final List<String> blackList = Arrays.asList("element1", "element2", "element3");

As you can see, that's... really minimalist, and still works :)

1 Comment

You missed the part about moving it outside of the method
1

You can make blacklist a field of your class and initialize it with :

Set<String> blackList = new HashSet<String>(Arrays.asList("element1", ...));

Comments

1

Try this way,

Set<String> blackList = new HashSet<String>();

    public static void main(String[] args)
    {
        Test12 ss = new Test12();
        ss.populateBlackList();  // if the blacklist is fixed means you can call this in constructor 
        System.out.println(ss.isInBlackList("element1"));
    }

    private void populateBlackList() // make it as an seperate method dont populate at all time
    {
        for (int i = 1; i <= 20; i++)
        {
            blackList.add("element" + i);
        }
    }

    public boolean isInBlackList(String element) //finding the blackList
    {
        return blackList.contains(element);
    }

1 Comment

I think you misunderstood the question. By my understanding, the "element 1", "element 2" etc. was just an example
0
  1. declare blackList as a member variable and initialize it in constructor.

  2. just use a for loop to add the elements

    for( int i = 1; i<=20; i++){

    string s = "element"+i;
        blackList.add(s);
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.