简体   繁体   中英

Importing and comparing two text files in java

I am importing two text files in java, one contains multiple grocery lists and the other contains a list of grocery items that I do not want.

I need to separate the various grocery lists and scan each of them to see if they contain any of the items on the list of things I don't want. If a grocery list contains one or more items on the forbidden list, i need to move this grocery list to a junk folder but the lists that do not contain any of the words must stay. Therefore I need to check each list individually.

I do not know how to separate the grocery lists in order to check them separately. I also do not know how to check the imported forbidden list against the other grocery lists.

Thanks.

The lists are separated by the word "Finish" appearing as the last line on the list. I used a delimiter to and set Finish as my pattern but I don't really know if that worked. I know it separates the lists physically if I output them but I don't know if that has made them separate entities that I can scan separately.

I don't know how the grocery lists are formatted so I cannot help you with separating them. If the lists are separated by a blank line then when you call readLine(), if the returned value isEmpty() then start a new list.

As far as comparing the two lists, I would put each item in the forbidden list into a forbidden HashSet and when reading in the new grocery lists, if any of the items you read are also in the HashSet then trash the list.

I choose a HashSet here because each insert into the HashSet will be O(1) repeated for each item in the list which leaves total insertion to be O(n) then getting an item from the list will be O(1) which will be repeated for each item in the new lists which leaves total compare to be O(n). Overall the entire algorithm is O(n) which isn't shabby.

Java比较两个列表 <<<您可以看到如何比较ArrayList或Hashset中的项目。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM