3

I've created a linked list (generic containers) with objects in Java. I need to re-write my insert-method to make the list sorted alphabetically by keys. This is my code so far:

Container:

class Sellbeholder<N extends Comparable<N>, V> implements INF1010samling<N,V> {

private Keeper første;
private int ant = 0;

private class Keeper {
    Keeper neste;
    N n;
    V v;

    Keeper(N n,V v) {
        this.n = n;
        this.v = v;
    }
}

This is my insert method (which I need to rewrite):

public void PutIn(N n, V v) {
    Keeper kep = new Keeper(n,v);
    kep.neste = første;
    første = kep;
    ant++;

This is the Person-object, which I'm putting in the container(linked list):

class Person {

    String name;

    Person(String n) {
        this.name = n;
    }
}

And this is how I create persons and put them in container:

Sellbeholder <String,Person> b1 = new Sellbeholder <String,Person>();
Person a = new Person("William");
b1.PutIn("William",a);

Any help would me much appreciated. I know I need to use the CompareTo-metohod to check where to put the object, but I'm not sure how the structure of the linked list should be set. I've started on this:

for(Keeper nn = første; nn!= null; nn = nn.neste) {

    if(nn.n.compareTo(kep.n) > 0) {
        //Do something here
4
  • Any reason why you can't use a TreeSet instead? docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html Commented Feb 27, 2013 at 15:22
  • If you need a linked list you can use java.util.LinkedList and if you need a Map sorted by the key you can use java.util.TreeMap. I think TreeMap does what you are looking for. Commented Feb 27, 2013 at 15:29
  • @anoopelias Probably he has to implement it himself, e.g. like a homework. Commented Feb 27, 2013 at 15:31
  • I would recommend to use the Java conventions, start methods with lower case letters, and refrain from using national language characters. Commented Feb 27, 2013 at 15:42

2 Answers 2

1

Iterate on the list until you get the proper place:

public void PutIn(N n, V v) {
    Keeper kep = new Keeper(n,v);
    // you will insert between previous and current
    Keeper previous = null;
    Keeper current = første;

    // loop until you get the right place        
    while (current != null && ((current.n).compareTo(n) > 0)) {
        previous = current;
        current = current.neste;
    }

    // insert your stuff there (if there were no previous, then this is the first one)
    if (previous == null) {
        første = kep;
    } else {
        previous.neste = kep;
    }

    // Set the next Keeper
    kep.neste = current;

    ant++;
}

This will keep your list ordered.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to use Collections.sort(List l) or Collections.sort(List l, Comparator c)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.