A suitable java collection for quick quick and quick removal

I would like to know if there is a suitable java interface for quick access (by index) and "quick" deletion. “Fast” I mean better O(n).

EDIT: The get method is only needed to randomly select an item from the collection. In addition, the title should indicate “collection”, not “interface”.

+4
source share
3 answers

A balanced binary search tree has O (log n) "get" and "remove" operations. A hash table implements the same operations O (1) times. In Java, you can use TreeMapeither HashMapclasses. For example:

TreeMap<Integer, String> map = new TreeMap<>();
map.put(0, "hello");
map.put(1, "world");
map.remove(0);

, ArrayList. , "get" - O (1). , , , O (1) "". :

temp = list.remove(list.size()-1);
return list.set(index, temp);
+3

, , , .

get() fast remove() - HashMap<Object, Object> (.. )?

get()/remove() hashCode() equals() O (1)

+1

, JDK List, , O (n) .

, , rope, enfilade. , , , (Guava, Commons Collections, Trove ..).

, , , , - . O (1) ( , ), O (1) O (1) . , ; , .

0

All Articles