removeall - Java LinkedHashSet remove some elements from the end -


i working on problem i'm required store elements requirements of no duplication , maintaining order. chose go linkedhashset since fulfilled both requirements.

let's have code:

 linkedhashset hs = new linkedhashset();   hs.add("b");   hs.add("a");   hs.add("d");   hs.add("e");   hs.add("c");   hs.add("f");   if(hs.contains("d")){        //do remove elements added after"d" i-e remove "e", "c" , "f"        //maybe hs.removeall(collection<?>c) ??    } 

can please guide me logic remove these elements?

am using wrong datastructure? if so, better alternative?

i think may need use iterator removal if using linkedhashset. find element, keep removing until tail. o(n), if wrote own linkedhashset (with doubly linked list , hashset) have access raw linking structure cut linked list in o(1), still need remove elements cut linked list hashset o(n) cost arise again.

so in summary, remove element, keep iterator element , continue walk down removing elements until end. i'm not sure if linkedhashset exposes required calls, can figure out.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -