Java Iterator begins with a specific element of the sorted set

I have a sorted set in Java (e.g. TreeSet). I want the iterator to start from a specific element of the tree and be able to go to the next or previous element (in sort order).

Are there any ideas on how to do this without implementing my own data structure?

thanks


EDIT:

I forgot to mention that I want to do it in a naive way (if any), since I care about performance. If I had access to the tree implementation, this would take O (1) time. I want something like that.

Also feel free to suggest other (third-party) java Sorted Trees implementations that support this.

+4
source share
5 answers

To go from element x to SortedSet s , use s.tailSet(x).iterator() .

To go back from the x of NavigableSet s element, use s.descendingSet().tailSet(x).iterator() .

tailSet() and descendingSet() create views of the source set, s . Therefore, their implementation cannot create copies of the original set (otherwise the copy will become obsolete if the set in question has been modified), and therefore will have O (1) performance.

A TreeSet is a NavigableSet and therefore a SortedSet .

+1
source

You can continue to call to skip this iteration until you reach the Object you are looking for. It is difficult to give much more information without additional context.

 ArrayList list = new ArrayList(); Iterator itr = list.iterator(); boolean skipElement = true; while(itr.hasNext()) { Object element = itr.next(); if(element.equals(myTargetElement)){ //target element is what you're comparing against to see where you want to start iterating (you didn't supply that info so not sure how you want to handle this) skipElement=false; } if(skipElement){ continue; } System.out.print(element); } 

this will display the item you want to start the first time.

0
source

The iterator returned by the TreeSet does not support moving forward and backward. Therefore, if going back and forth is a difficult requirement, you need to first create a list and get a list iterator from it:

 List<T> list = new ArrayList<>(treeSet); int index = list.indexOf(object); ListIterator<T> iterator = list.listIterator(index); 

If you want to use an iterator to change the set, this is not an option, because obviously you have to change the list, not the set.

If TreeSet is not a strict requirement, you can create a subset of your TreeSet and get an iterator from it:

 Navigable<T> subset = treeSet.tailSet(object); Iterator<T> iterator = subset.iterator(); 

This is pretty fast because tailSet returns a representation of this subset, so it does not require a copy of part of the set.

0
source
  TreeSet<Integer> set = new TreeSet<>(IntStream.range(0, 10).boxed().collect(Collectors.toList())); for (int n : set.stream().skip(5).collect(Collectors.toList())) { System.out.println(n); } 
0
source

You can try something like this:

 List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L); TreeSet<Long> tree = new TreeSet<>(list); tree.tailSet(4L).iterator() 
0
source

All Articles