How can I let a set retain all strings that contain my specified substring in java? -
i use hashset dictionary. filter out words not start substring. should this:
string word = 'ab'; list<string> list = arrays.aslist(word); boolean result = lexiconset.retainall(list); and instead of resulting in lexicon containing word 'ab', keep words beginning 'ab'. how can this?
i know can convert set string arraylist, , loop on elements see if strings starts 'ab', since think can time consuming , not efficient, hear better solutions. thank in advance!
with java 8, life easy:
list.removeif(s -> !s.startswith("ab")); this remove elements don't begin "ab".
note can use values() retrieve map's values , work directly on them, without need convert arraylist.
Comments
Post a Comment