0
    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]]

Arun
  • 21
  • 2
  • 2
    What is the value of `paramenter` that you're using? It can't be zero, as indicated here, because you can't parse the zeroth list elements as strings. – Andy Turner Apr 30 '20 at 07:45
  • 1
    Also: don't make order and paramenter static variables: make paramenter an instance variable, passed to the constructor; and use `Comparator.reverse()` to reverse its order. – Andy Turner Apr 30 '20 at 07:46

3 Answers3

1

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • @AndyTurner - I've just refactored the code inside his compare method but the logic does not make sense to me. I've added a note in the end. – Arvind Kumar Avinash Apr 30 '20 at 08:10
  • no, you've changed it. OP's code can return both 1 and -1 when order is, say, 1. `Comparator.comparingInt(list -> Integer.parseInt(list.get(paramenter)))` would be an easier way to specify the comparator. – Andy Turner Apr 30 '20 at 08:13
  • Thanks, @AndyTurner for finding it. I've updated the answer. – Arvind Kumar Avinash Apr 30 '20 at 08:16
  • Everything looks fine,I made some mistakes,You corrected it.Really thanks.But one main thing I would like to ask is " Using raw Comparator instead of parameterized version." why this ? Though I used raw comparator,Inside compare() method,I am converting and then comparing as per my need and returning integer right ? what makes difference between using "Raw comparator and Parameterized compartor " while runtime ? – Arun Apr 30 '20 at 13:53
  • @Arun - You are most welcome. Instead of explaining the importance of parameterized type, I suggest you go through [this](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – Arvind Kumar Avinash Apr 30 '20 at 14:24
  • @Arun - If the answer resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 14 '20 at 12:31
0

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));
}
yellowvamp04
  • 141
  • 1
  • 4
  • 2
    Using an undefined identifier results in a compiler error. It does not magically make the expression `a.size()` evaluate to zero. This simply is [another indicator](https://stackoverflow.com/questions/61518031/why-treeset-in-java-behaving-like-below#comment108821462_61518031) that the code posted in the question is not matching the code the OP actually ran. – Holger Apr 30 '20 at 07:50
0

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);
Dinesh Shekhawat
  • 504
  • 6
  • 13