I have a piece of code which contains
Collection<String> tok=Arrays.asList(tokens);
HashSet<String> lookup=new HashSet<String>();
while(!lookup.containsAll(tok)&&max<N)
{
}
Using toString() I found that even though the HashSet contains a collection still containsAll method returns false.I am using remove method in the code but it is never being called.The complete code is here on pastebin which would be more readable.
The purpose is to take a input string and another k strings and search for minimum sub sequence in input string which contains all k strings
1)Start with index 0 in input string and add first k strings to the HashSet because this is the minimum sequence that can contain k different tokens
2)After that take the range min=0 to max=k and keep adding string at position max and incrementing max until the set contains all tokens
3)When all tokens are found remove string a position min(initially 0) and increment min.If after removal all tokens are not present in HashSet. Set found to false so that step 2 is repeated in next iteration for interval starting with this value of min
4)If max-min is smaller than previous difference the new minimum subsequence is min-max
For input as
This is a test. This is a programming test. This is a programming test in any language.
k=4
this
a
test
programming
The output is
tokens are [this, a, test, programming]
Increasing Max [is, test, a, this] found =false
Increasing Max [is, test, a, this] found =false
Increasing Max [is, test, a, this] found =false
Increasing Max [is, programming, test, a, this] found =false
Increasing Max [is, programming, test, a, this] found =false
Increasing Max [is, programming, test, a, this] found =false
Increasing Max [is, programming, test, a, this] found =false
Increasing Max [is, programming, test, a, this] found =false
Increasing Max [is, programming, test, a, this] found =false
Increasing Max [is, programming, test, a, this] found =false
Increasing Max [is, programming, test, a, in, this] found =false
Increasing Max [is, programming, test, any, a, in, this] found =false
Increasing Max [is, programming, test, any, a, language, in, this] found =false
No subsegment found
The output shows that remove was never called still containsAll() kept returning false even when it contained all strings present in the collection.
Why does it keep returning false even though remove is never called?
Perhaps a HashSet wouldn't work even if the above two issues were resolved.For an input like
This is a this test.
2
this
test
Since the this at index 3 won't be added to the set.The produced min interval would be [0-4] instead of [3-4] So is there a collection which may contain duplicate values and has a containsAll method or Will i have to use a HashMap with indexes of strings as keys?