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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -