www.webdeveloper.com
Results 1 to 15 of 15

Thread: SortedSet/TreeMap problem

  1. #1
    Join Date
    Jun 2004
    Posts
    51

    SortedSet/TreeMap problem

    I am writing code for Displaying data in ascending order using a TreeMap(). And am in a fix to resolve it. The scenario is

    like this i am calling java classes in my application from another application whic is fetching data for me from their

    database where i can't intervene. From thisapplication I am calling a class color.java in which objects are defined as

    string . And I need to get the data and display the same using my servlet in its ascending order based on its color number.

    Like this

    ColorNumber Location Qty Price

    100 C422 100 20
    150 A1100 0575 15
    200 D3760 130 500

    Also While making call to color.java i get data of following types

    colorlocation - Varchar
    colornumber - number.
    colorqty - number
    colorPrice. - number


    The piece of java code in my file details.java is like this

    Color[] Range= colorenquiry.getColordata();
    SortedMap ColorMap = new TreeMap();

    for (int i=0; i<Range.length(); i++){

    ColorMap.put(Range[i].colornumber, Range[i].colorlocation.toString()+""+

    Range[i].colorqty.toString()+""+Range[i].colorprice.toString());

    }
    System.out.println(ColorMap.keySet());
    System.out.println(ColorMap.values());
    System.out.println(ColorMap.entrySet());
    ----------------------------------------------------------

    Now the Problem is for few querries iam getting the sorted output as above but for queries where data is like 50- 60 rows am finding sorted data in patches like its 100,150, 200, 300, 500, 350, 600 700 425, 800, 900, 1000, 1100, 1200, 1500,250....
    Please advise or have any such working examples where i can refer.

    Thanks & Regards,

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    It sounds like your problem is related to the types of attributes of the Color class. I think you need to write a Comparator class and use the TreeMap constructor that takes a Comparator as a parameter.

  3. #3
    Join Date
    Jun 2004
    Posts
    51

    Smile Thanks ray.

    Thanks Ray,

    I tried with a Comparator to avoid natural order of sorting pls see the code below but still geting a Clascast exception. Is there any problem with this code too?

    Overall if there is any other approach to resolve sorting of rows in ascening order. Please suggest or any other working code which i can look into.

    int[] s1;
    bolean[] s2;

    public void CompareAscOrder(int[] s1, boolean[] s2){

    if (s1.length != s2.length){

    throw new IllegalArgumentException("Wrong length...");
    }
    this.s1=s1;
    this.s2=s2;

    }



    final Comparator C2 = new Comparator() {
    public int compare(Object o1, Object o2) {
    Object rowa[] = (Object[]) a;
    Object rowb[] = (Object[]) b;

    for(int i=0; i<s1.length; i++)
    {
    int col = s1[i];
    int result=((Comparable)rowa[col].compareTo(rowb[col]);
    if (result !=0){
    if(s2[i]) return result;
    else return -result;
    }
    }
    return 0;

    }
    };

  4. #4
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Well I would expect you to pass two Colors to the comparator, not those arrays.

  5. #5
    Join Date
    Jun 2004
    Posts
    51

    Question Hello Ray.

    Can you please modify my comparator code where i need to make changes.??

    Thanks

  6. #6
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Does Color look something like this? I.e. all attributes declared String?
    Code:
    class Color {
      String num;
      String loc;
      String qty;
      String price;
    }
    If so can you make it look like this, so the types of the attributes are meaningful?
    Code:
    class Color {
      int num;
      String loc;
      int qty; // or long
      java.math.BigDecimal price;
    }
    If you can't then you have to do the String to number conversion in the comparator.

  7. #7
    Join Date
    Jun 2004
    Posts
    51

    Smile Hello Ray

    At the outset i really appreciate your intrest in my code and problem . And Thanks a Lot for the same.

    You figured correctly . My clas is exactly defined as stings and i cannot modify any thing there since its being called from another application.

    well i tried to type cast the comparator with Integer type . Which is modified like this.ut its not working since code line marked with *** is not allowing to write d1 or d2 to let me have my piece of mind.


    What do you say?
    ------------------------------------------------
    final Comparator C1 = new Comparator(){
    public int compare(Object a, Object b){

    Color col1 =(Color) a;
    Color col2 =(Color) b;

    int D1 = integer.parseInt(col1.colornumber);
    int D2 = integer.parseInt(col2.colornumber);

    int Order = (col2.colornumber.CompareTo(col1.Colornumber)); *****
    //int Order = (D2.CompareTo(D1)); *** It gives error ***
    if (Order !=0)
    return Order;
    return(D2<D1 ? -1: (D2==D1 ? 0:1));
    }
    };

  8. #8
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    The type int is not an object so the comparison is actually easier. I believe it's something like so.
    Code:
    int d1 = Integer.parseInt(col1.colornumber);
    int d2 = Integer.parseInt(col2.colornumber);
    
    return d1 - d2;

  9. #9
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    One thing I noticed is that instead of retrieving values by getting the property, get the value by calling a getter method from the class Color..
    instead of this

    col1.colornumber

    it should be

    col1.getColorNumber();

    thats a better coding practice
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  10. #10
    Join Date
    Jun 2004
    Posts
    51

    not yet resolved.

    Hello Ray , Khalid.

    its Great to have your responses . But unfortunately this problem is requires Hack of a good job.
    Khalid , The getColor() method is not available else i could have used it on first place.

    Ray d1 - d2 is not acceptable as it need type casting as integer and when i do like this as shown in my comparator code it gives error .

    Thanks.

  11. #11
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Post the current version of your comparator because it should return an int and d1, d2 and d1-d2 are all ints. Oh, and put it inside a code block so it's easier to understand.
    Last edited by ray326; 12-21-2004 at 10:15 PM.

  12. #12
    Join Date
    Jun 2004
    Posts
    51

    Thumbs up Hi

    Curently for this problem i have ignored the comparator and based on natural sorting of string in my class am getting partial results.

    Can you suggest a Comparator code which can take care of String Color.number.

    I look forward may be you can nail it.

  13. #13
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    I think it would look something like this untested code.
    Code:
    class ColorComparator implements Comparator 
    {
        public int compare(Object o1, Object o2)
        {
            if ((o1 instanceof Color) && (o2 instanceof Color))
            {
                Color color1 = (Color) o1;
                Color color2 = (Color) o2;
                int c1 = Integer.parseInt(color1.colornumber);
                int c2 = Integer.parseInt(color2.colornumbet);
                return c1 - c2;
            }
            else
                throw(new ClassCastException());
        }
    }

  14. #14
    Join Date
    Jun 2004
    Posts
    51
    Hello ray,

    I am still getting a classcast Exception.

  15. #15
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Which line?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles