Arun
Arun

Reputation: 21

Why TreeSet in java behaving like below?

    ArrayList<String> a1=new ArrayList<String>();
    a1.add("Item1");
    a1.add("58584272");
    a1.add("62930912");

    ArrayList<String> a2=new ArrayList<String>();
    a2.add("Item2");
    a2.add("9425650");
    a2.add("96088250");

    ArrayList<String> a3=new ArrayList<String>();
    a3.add("Item3");
    a3.add("37469674");
    a3.add("46363902");

    ArrayList<String> a4=new ArrayList<String>();
    a4.add("Item4");
    a4.add("18666489");
    a4.add("88046739");

    List<List<String>> a5=new ArrayList<List<String>>();
    a5.add(a1);
    a5.add(a2);
    a5.add(a3);
    a5.add(a4);     


    TreeSet<List<String>> ts=new TreeSet<List<String>>(new mycomparator());
    for(int i=0; i<=a.size()-1; i++){
        ts.add(a5.get(i));
    }
System.out.Println(ts);   // Returns [[Item1, 58584272, 62930912]]





public class mycomparator implements Comparator{

static int order,paramenter=0;
@Override
public int compare(Object o1, Object o2) {
    List<String> a1=(List<String>)o1;
    List<String> a2=(List<String>)o1;
    int b1=Integer.parseInt(a1.get(paramenter));
    int b2=Integer.parseInt(a2.get(paramenter));
    if(b1>b2){ return  order==1?1:-1;}
    else if (b1<b2){return order==1?-1:1;}
    else{return 0;} 
    }
}

In the above code,I am trying to add objects to tree set,After adding all the elements when I try to print the treeset,only the first element get added.Why this is happening ?

Result --> [[Item1, 58584272, 62930912]]

Upvotes: 0

Views: 64

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78995

Your code has so many problems:

  1. Using raw Comparator instead of parametrized version.
  2. Using wrong variable in the for loop.
  3. Using static variables in the comparator.

On a side note, you should follow the Java naming conventions e.g. the class mycomparator should be named as MyComparator.

Given below is the code incorporating these comments:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

class MyComparator implements Comparator<List<String>> {

    int order, paramenter;

    MyComparator(int order, int paramenter) {
        this.order = order;
        this.paramenter = paramenter;
    }

    @Override
    public int compare(List<String> o1, List<String> o2) {
        int b1 = Integer.parseInt(o1.get(paramenter));
        int b2 = Integer.parseInt(o2.get(paramenter));
        if (b1 > b2) {
            return order == 1 ? 1 : -1;
        } else if (b1 < b2) {
            return order == 1 ? -1 : 1;
        } else {
            return 0;
        }
    }
}

public class Main {

    public static void main(String[] args) {

        ArrayList<String> a1 = new ArrayList<String>();
        a1.add("Item1");
        a1.add("58584272");
        a1.add("62930912");

        ArrayList<String> a2 = new ArrayList<String>();
        a2.add("Item2");
        a2.add("9425650");
        a2.add("96088250");

        ArrayList<String> a3 = new ArrayList<String>();
        a3.add("Item3");
        a3.add("37469674");
        a3.add("46363902");

        ArrayList<String> a4 = new ArrayList<String>();
        a4.add("Item4");
        a4.add("18666489");
        a4.add("88046739");

        List<ArrayList<String>> a5 = new ArrayList<ArrayList<String>>();
        a5.add(a1);
        a5.add(a2);
        a5.add(a3);
        a5.add(a4);

        TreeSet<List<String>> ts = new TreeSet<List<String>>(new MyComparator(0, 1));
        for (int i = 0; i < a5.size(); i++) {
            ts.add(a5.get(i));
        }
        System.out.println(ts);
    }
}

Output:

[[Item1, 58584272, 62930912], [Item3, 37469674, 46363902], [Item4, 18666489, 88046739], [Item2, 9425650, 96088250]]

Note: I've just implemented your logic inside your compare method as it is. If you can tell me the exact requirement, I will update the code inside compare or you can update it yourself.

Upvotes: 1

Dinesh Shekhawat
Dinesh Shekhawat

Reputation: 539

You have implemented the comparator incorrectly. Check the following code:

        List<String> a1 = new ArrayList<String>();
        a1.add("Item1");
        a1.add("58584272");
        a1.add("62930912");

        List<String> a2 = new ArrayList<String>();
        a2.add("Item2");
        a2.add("9425650");
        a2.add("96088250");

        List<String> a3 = new ArrayList<String>();
        a3.add("Item3");
        a3.add("37469674");
        a3.add("46363902");

        List<String> a4 = new ArrayList<String>();
        a4.add("Item4");
        a4.add("18666489");
        a4.add("88046739");

        List<List<String>> a = new ArrayList<List<String>>();
        a.add(a1);
        a.add(a2);
        a.add(a3);
        a.add(a4);

        Comparator<List<String>> comparator = new Comparator<List<String>>() {

            @Override
            public int compare(List<String> a1, List<String> a2) {
                String b1 = a1.get(0);
                String b2 = a2.get(0);

                return b1.compareTo(b2);
            }
        };

        TreeSet<List<String>> ts = new TreeSet<List<String>>(comparator);
        for (int i = 0; i <= a.size() - 1; i++) {
            ts.add(a.get(i));
        }

        System.out.println(ts);

Upvotes: 0

yellowvamp04
yellowvamp04

Reputation: 141

It is because of this code loop limit you use "i<=a.size()-1". The "a" is never defined in your code, meaning size to be provided is zero, then you minus 1, so it will be less than zero.

That means this loop will only be triggered once.

for(int i=0; i<=a.size()-1; i++){
    ts.add(a5.get(i));
}

Upvotes: 0

Related Questions