0
private static LinkedList<Integer> melhorMoto1 = new LinkedList<>();

I know I can do something like create a new LinkedList, use newLinkedList = melhorMoto1;and do Collections.sort(newLinkedList); and newLinkedList.equals(melhorMoto1 ); but as I'm working with recursive function, the newLinkedList = melhorMoto1; is a very slow attribution. Can I check if the LinkedList is ordered with a method or something?

Marcos
  • 39
  • 10
  • 4
    Checking if it's ordered is as simple as iterating it and verifying that the order is maintained between any two adjacent items. – Nir Alfasi Aug 17 '17 at 23:57
  • 1
    "create a new LinkedList, use newLinkedList = melhorMoto1;" that code doesn't create new LinkedList, it assigns to `newLinkedList` reference to LinkedList which is held in melhorMoto1 variable. What you probably think of is something like `LinkedList newLinkedList = new LinkedList<>(melhorMoto1)` – Pshemo Aug 17 '17 at 23:59
  • Nop. When i say 'newLinkedList = melhorMoto1;' I'm whereas that I already created the newLinkedList (with 'LinkedList newLinkedList = new LinkedList<>()' ofc); – Marcos Aug 18 '17 at 00:02
  • Then what's the assignment for? – shmosel Aug 18 '17 at 00:03
  • I think i wasnt clear. I want to know if there is one LinkedList method that I cant use to check if the list is ordered or no. For exemple: melhorMoto1.isOrdered that return 1 if yes or 0 if not. – Marcos Aug 18 '17 at 00:04
  • 1
    Even if you created `LinkedList newLinkedList = new LinkedList<>()` if you later do `newLinkedList = melhorMoto1;` you are discarding that new LinkedList you just created and *assign* to `newLinkedList` reference to list held by `melhorMoto1`. If that is not the case then clarify your question so people wouldn't waste their time on clarifying it. – Pshemo Aug 18 '17 at 00:04
  • It is to assign melhorMoto1 values to newLikedList, but i just realized that I can use `newLikedList= new LinkedList(melhorMoto1);`. w/e – Marcos Aug 18 '17 at 00:06
  • As i said, i CAN do it. Whatever, @BeeOnRope already helped me. Sorry for the not clear question. – Marcos Aug 18 '17 at 00:13

1 Answers1

1

In Java 8, you can use Comparators.isInOrder(Iterable) to achieve this for a List (or any other ordered collection).

Prior to Java 7, do it yourself: to check that any List is sorted in ascending order, simply iterate through it and check that the current element cur is greater or equal to the previous element prev for each adjacent pair of elements.

Change that to less than or equal for descending order. This requires size() - 1 total checks.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • Thank you. I was just looking for a method that do it for me automatically. – Marcos Aug 18 '17 at 00:10
  • There is no built-in method, but writing it is only a few lines of code. Put it in a utility class and you'll have it forever. If you really want a "built in" probably the closest you can is using streams to create a temporary sorted list to compare to, like `list.equals(list.stream().sorted().collect(Collectors.toList()))`, but this is going to be quite inefficient compared to an iterative method. @marcos – BeeOnRope Aug 18 '17 at 00:18
  • @Marcos - apparently I was wrong, Java 8 has `Comparators.isInOrder(Iterable)` which does what you want. – BeeOnRope Aug 18 '17 at 17:46