1
\$\begingroup\$

When I want to sort data on linked list by using collections.sort() and comparator will working after write (collections.sort (...)) and then use it again with comparator?

Is it possible to just use a comparator without collection.sort (...) first?

public class JavaApplication {
   public static void main(String[] args) {
      LinkedList lList = new LinkedList();
      lList.add("1");
      lList.add("2");
      lList.add("3");
      lList.add("4");
      lList.add("5");
      System.out.println(lList);
      lList.addFirst("0");
      System.out.println(lList);
      lList.addLast("6");
      System.out.println(lList);

      lList.addFirst("2");
      System.out.println(lList);

      lList.add(3, "10");
      System.out.println(lList);

      lList.add(4,"25");
      System.out.println(lList);

      getIterator( lList ).add( "60" );
      System.out.println(lList);

      lList.addLast("10");
      System.out.println(lList);
      lList.addLast("15");
      System.out.println(lList);


      lList.add(6,"40");
      System.out.println(lList);

      getIterator( lList ).add( "70" );
      System.out.println(lList);

      lList.addFirst("75");
      System.out.println(lList);

      Collections.sort(lList);

      Collections.sort(lList, new AComparator());
      System.out.println("LinkedList (after sorting using Comparator): "+lList);
   }


   private static ListIterator getIterator(LinkedList lList) {
       return lList.listIterator( lList.size() / 2 );

   }
}

class AComparator implements Comparator<String> {
    public int compare(String s1, String s2) {
        return s1.length() - s2.length();
    }
}

And can someone give me a suggest how to efficiency the code.

Output that I want :

Unsorted [75, 2, 0, 1, 10, 25, 60, 40, 70, 2, 3, 4, 5, 6, 10, 15]
LinkedList (after sorting using Comparator): [0, 1, 2, 2, 3, 4, 5, 6, 10, 10, 15, 25, 40, 60, 70, 75]
\$\endgroup\$
  • 1
    \$\begingroup\$ Does your code currently work as you expect it to, or is it broken or incomplete? \$\endgroup\$ – Phrancis Oct 19 '17 at 15:04
  • \$\begingroup\$ It's working complete, but when I trying to reverse order it, the linked list sorted will be broken. Can you suggest me, what should i do to look perfect? \$\endgroup\$ – Dicky Alamsyah Oct 19 '17 at 15:32
0
\$\begingroup\$

If you're using Java Collections in any Java version later than 5, you should be using generic typing. It's an easy way to provide type safety without dealing with casting, multiple class-specific implementations of collections, or the other hassles that have long since been forgotten. And with the latest Java versions, you don't even need to specify the generic type of the assigned object due to type inheritance, so adding generics support is as simple as:

LinkedList<String> lList = new LinkedList<>();

and:

private static <E> ListIterator<E> midpointIterator(LinkedList<E> lList) {
    return lList.listIterator( lList.size() / 2 );
}

Which brings me to another point: use descriptive method and object names! Looking at the name of the getIterator method made me wonder why the objects weren't being inserted at the beginning when I ran the code. midpointIterator (or something similar) lets me know that the ListIterator I'm getting back starts at the midpoint.

Likewise with the comparator:

class StringLengthComparator implements Comparator<String>{
    public int compare(String s1, String s2) {
        return s1.length() - s2.length();
    }
}

Now I know that it compares the length of the Strings passed to it.

On the topic of Strings: are you sure you want to use them? Because it looks like you're trying to sort a list of numbers, in which case you should use a LinkedList<Integer> and a Comparator<Integer>, although Collections.sort(List)'s default comparator will do the job fine. Right now you're calling Collections.sort(List), which sorts in character order, and Collections.sort(List, Comparator) using your String length comparator, which sorts by length. This puts them in numerical order:

LinkedList before sort:     [75, 2, 0, 1, 10, 25, 60, 40, 70, 2, 3, 4, 5, 6, 10, 15]
LinkedList default sort:    [0, 1, 10, 10, 15, 2, 2, 25, 3, 4, 40, 5, 6, 60, 70, 75]
LinkedList comparator sort: [0, 1, 2, 2, 3, 4, 5, 6, 10, 10, 15, 25, 40, 60, 70, 75]

I think what you're really looking to do is something like this:

import java.util.Collections;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListSort {
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.add(5);
        linkedList.addFirst(0);
        linkedList.add(6);
        linkedList.addFirst(2);
        linkedList.add(3, 10);
        linkedList.add(4,25);
        midpointIterator( linkedList ).add( 60 );
        linkedList.add(10);
        linkedList.add(15);
        linkedList.add(6,40);
        midpointIterator( linkedList ).add(70);
        linkedList.addFirst(75);

        System.out.println("LinkedList before sort:  " + linkedList);
        Collections.sort(linkedList);
        System.out.println("LinkedList default sort: " + linkedList);
    }

    private static <E> ListIterator<E> midpointIterator(LinkedList<E> lList) {
        return lList.listIterator( lList.size() / 2 );
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.