12

I want to navigate into a list by identifier.

1- I manage/create a list.

2- I create function to get next item of a identifier element from my list

Can you help me to fix this code?

Prepare the list

List<String> myList = new ArrayList<String>();
myList.add("1");
myList.add("2");
myList.add("3");
myList.add("4");
myList.add("5");


public String function getNext(String uid) {

    if (myList.indexOf(uid).hasNext()) {
        return myList.indexOf(uid).nextElement();
    }
    return "";
}

public String function getPrevious(String uid) {
    return myList.indexOf(uid).hasPrevious() ? myList.indexOf(uid).previousElement() : "";
}
BasicCoder
  • 1,661
  • 10
  • 27
  • 42

3 Answers3

17

You could use an index to lookup your String which is faster and simpler however to implement the functions as you have them.

public String getNext(String uid) {
    int idx = myList.indexOf(uid);
    if (idx < 0 || idx+1 == myList.size()) return "";
    return myList.get(idx + 1);
}

public String getPrevious(String uid) {
    int idx = myList.indexOf(uid);
    if (idx <= 0) return "";
    return myList.get(idx - 1);
}

Using a List.get(i) is O(1) which makes keeping the index the fastest option. List.indexOf(String) is O(n). Using a NavigatbleSet might appear attractive as it is O(log n), however the cost of creating an object is so high that the collection has to be fairly large before you would see a benefit. (In which case you would use the first option)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4

If your elements are not repeated, what you need is a NavigableSet:

http://download.oracle.com/javase/6/docs/api/java/util/NavigableSet.html

The methods higher and lower are what you are looking for.

fortran
  • 74,053
  • 25
  • 135
  • 175
  • 1
    NavigableSet extends SortedSet. This is not what the OP is after: NavigableSet shall give him the next element in sorted order, no in insertion order. The OP is after what Peter Lawrey gave as an answer: a method giving him the next/previous element in insertion order. – SyntaxT3rr0r Jun 20 '11 at 13:45
  • @SyntaxT3rr0r In the example elements are inserted in lexicographical order without repetitions, so it was a fair assumption that he was miss-using a list when what he really needed was a sorted set... You know, when all you got is a hammer, everything looks like a nail. – fortran Jun 21 '11 at 07:47
  • are you by any mean trying to imply that all the OP knows would be a hammer? That would be very rude. It would also be ironic that you --who knows more than just the hammer-- don't know when to use more than the hammer and when not to. A bogus answer is a bogus answer: don't try to twist this in your favor. – SyntaxT3rr0r Jun 22 '11 at 13:13
  • 2
    @SyntaxT3rr0r What I am implying is that the OP is clearly a programming novice, at least in Java (judging by the syntax errors like the "function" keyword), and he'd better take a look to what the Collections API provides. Even more, I've seen too many times seasoned Java programmers abusing the ArrayList, so this is clearly a common pitfall and it is worth to warn about it. – fortran Jun 24 '11 at 09:20
1

Lists don't have a nextElement() method. indexOf returns the integer index of the item. You could simply add (or subtract) one to get the next (or previous) item:

public String function getNext(String uid) {
   var index = myList.indexOf(uid);
   if (index > -1) {
     try {
       return myList.get(i+1);
     } catch ( IndexOutOfBoundsException e) {
       // Ignore
     }
   }
   return ""; // consider returning `null`. It's usually a better choice.
}

However looking up an object with indexOf on ArrayList is a very slow process, because it has to check every single entry. There are better ways to this, but that depends on what you are actually trying to achieve.

RoToRa
  • 37,635
  • 12
  • 69
  • 105