0

I have an input stream which I want to use to read specific messages whether it is contained in the map, e.g.: you must consider : Invalid card number. // this is the line.

I want to check whether this line contains the 'Invalid card number' key of the map.

We have to iterate through the map and checks whether the line contains the message.

String line = "you must consider : Invalid card number.";
for (String key : map) {
    if(line.contains(key)) {
        throw new Exception(key.value());
    }
}

But then we have to iterate through this map for every line of the stream.

Is this a good practice?

4

1 回答 1

3

正如其他人在评论中所说,是的,这可能是您实现这一目标的唯一方法。

另一方面,您for statement应该看起来像这样(假设您的地图是 java.util.Map):

for (String key : map.values()) {
于 2013-06-24T08:52:21.843 回答