TreeSet of Custom Objects





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I created a TreeSet of custom objects (KCtrSrv) for a project I'm working on, and I'm trying to use .higher(KCtrSrv) and .lower(KCtrSrv). So a KCtrSrv Object would be passed in to compare to the other KCtrSrv objects that are already inside of the TreeSet. I would like to be able to isolate it to only look at the center field, is this possible?



**My tree contains three objects with centers 0.666, 2.0, 3.333 and Currently when I do tree.higher(newRequest) it returns 3.333. newRequest has values center = 0 and serverPosition = 0. It should be returning 0.666.



I'm not 100% positive how .higher() and .lower() operate, I've tried looking through JavaDocs however it hasn't really helped much. I've also thought about making a class for TreeSet and overriding .higher() and .lower(), however I wasn't quite sure where to begin the comparisons for that.



Any help would be greatly appreciated thanks!



My Object:



class KCtrSrv implements Comparable {



    private double center;
private double serverPosition;

public KCtrSrv(double center, double serverPosition) {

this.center = center;
this.serverPosition = serverPosition;
}

private void setServerPosition(double position) {
this.serverPosition = position;
}

private double getServerPosition() {
return this.serverPosition;
}

private double getCenter() {
return this.center;
}

public int compareTo(KCtrSrv x) {
return (int) Math.abs(this.serverPosition - x.center);
}


}










share|improve this question




















  • 1





    Please post your code as text in the question, not as a linked screenshot

    – Joni
    Nov 29 '18 at 2:43


















0















I created a TreeSet of custom objects (KCtrSrv) for a project I'm working on, and I'm trying to use .higher(KCtrSrv) and .lower(KCtrSrv). So a KCtrSrv Object would be passed in to compare to the other KCtrSrv objects that are already inside of the TreeSet. I would like to be able to isolate it to only look at the center field, is this possible?



**My tree contains three objects with centers 0.666, 2.0, 3.333 and Currently when I do tree.higher(newRequest) it returns 3.333. newRequest has values center = 0 and serverPosition = 0. It should be returning 0.666.



I'm not 100% positive how .higher() and .lower() operate, I've tried looking through JavaDocs however it hasn't really helped much. I've also thought about making a class for TreeSet and overriding .higher() and .lower(), however I wasn't quite sure where to begin the comparisons for that.



Any help would be greatly appreciated thanks!



My Object:



class KCtrSrv implements Comparable {



    private double center;
private double serverPosition;

public KCtrSrv(double center, double serverPosition) {

this.center = center;
this.serverPosition = serverPosition;
}

private void setServerPosition(double position) {
this.serverPosition = position;
}

private double getServerPosition() {
return this.serverPosition;
}

private double getCenter() {
return this.center;
}

public int compareTo(KCtrSrv x) {
return (int) Math.abs(this.serverPosition - x.center);
}


}










share|improve this question




















  • 1





    Please post your code as text in the question, not as a linked screenshot

    – Joni
    Nov 29 '18 at 2:43














0












0








0








I created a TreeSet of custom objects (KCtrSrv) for a project I'm working on, and I'm trying to use .higher(KCtrSrv) and .lower(KCtrSrv). So a KCtrSrv Object would be passed in to compare to the other KCtrSrv objects that are already inside of the TreeSet. I would like to be able to isolate it to only look at the center field, is this possible?



**My tree contains three objects with centers 0.666, 2.0, 3.333 and Currently when I do tree.higher(newRequest) it returns 3.333. newRequest has values center = 0 and serverPosition = 0. It should be returning 0.666.



I'm not 100% positive how .higher() and .lower() operate, I've tried looking through JavaDocs however it hasn't really helped much. I've also thought about making a class for TreeSet and overriding .higher() and .lower(), however I wasn't quite sure where to begin the comparisons for that.



Any help would be greatly appreciated thanks!



My Object:



class KCtrSrv implements Comparable {



    private double center;
private double serverPosition;

public KCtrSrv(double center, double serverPosition) {

this.center = center;
this.serverPosition = serverPosition;
}

private void setServerPosition(double position) {
this.serverPosition = position;
}

private double getServerPosition() {
return this.serverPosition;
}

private double getCenter() {
return this.center;
}

public int compareTo(KCtrSrv x) {
return (int) Math.abs(this.serverPosition - x.center);
}


}










share|improve this question
















I created a TreeSet of custom objects (KCtrSrv) for a project I'm working on, and I'm trying to use .higher(KCtrSrv) and .lower(KCtrSrv). So a KCtrSrv Object would be passed in to compare to the other KCtrSrv objects that are already inside of the TreeSet. I would like to be able to isolate it to only look at the center field, is this possible?



**My tree contains three objects with centers 0.666, 2.0, 3.333 and Currently when I do tree.higher(newRequest) it returns 3.333. newRequest has values center = 0 and serverPosition = 0. It should be returning 0.666.



I'm not 100% positive how .higher() and .lower() operate, I've tried looking through JavaDocs however it hasn't really helped much. I've also thought about making a class for TreeSet and overriding .higher() and .lower(), however I wasn't quite sure where to begin the comparisons for that.



Any help would be greatly appreciated thanks!



My Object:



class KCtrSrv implements Comparable {



    private double center;
private double serverPosition;

public KCtrSrv(double center, double serverPosition) {

this.center = center;
this.serverPosition = serverPosition;
}

private void setServerPosition(double position) {
this.serverPosition = position;
}

private double getServerPosition() {
return this.serverPosition;
}

private double getCenter() {
return this.center;
}

public int compareTo(KCtrSrv x) {
return (int) Math.abs(this.serverPosition - x.center);
}


}







java treeset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 2:48







Nick Pav

















asked Nov 29 '18 at 2:38









Nick PavNick Pav

34




34








  • 1





    Please post your code as text in the question, not as a linked screenshot

    – Joni
    Nov 29 '18 at 2:43














  • 1





    Please post your code as text in the question, not as a linked screenshot

    – Joni
    Nov 29 '18 at 2:43








1




1





Please post your code as text in the question, not as a linked screenshot

– Joni
Nov 29 '18 at 2:43





Please post your code as text in the question, not as a linked screenshot

– Joni
Nov 29 '18 at 2:43












1 Answer
1






active

oldest

votes


















0














To make the treeset compare only based on center, you can write your compareto method like this:



public int compareTo(KCtrSrv x) {
return Double.compare(this.center, x.center);
}


On a side note, a.compareTo(b) is supposed to return a negative number when a is "less than" b. I don't see how an implementation that uses Math.abs could work






share|improve this answer


























  • Would I then have to override compareTo(Object o) in order for that comparison to happen? I don't seem to be able to because I'm passing in KCtrSrv and not an Object..

    – Nick Pav
    Nov 29 '18 at 3:10











  • I changed the parameter to Object x and casted it to KCtrSrv within the method and I was then able to override, and now the comparison seems to be working. Thank you so much!

    – Nick Pav
    Nov 29 '18 at 3:14











  • Glad you could make it work. An alternative to what you did is changing implements Comparable to implements Comparable<KCtrSrv>. With the first one you're saying "objects of this class can be compared with anything that exists", with the second you'd be restricting it to objects of the same class and wouldn't need to cast.

    – Joni
    Nov 29 '18 at 13:53












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531043%2ftreeset-of-custom-objects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














To make the treeset compare only based on center, you can write your compareto method like this:



public int compareTo(KCtrSrv x) {
return Double.compare(this.center, x.center);
}


On a side note, a.compareTo(b) is supposed to return a negative number when a is "less than" b. I don't see how an implementation that uses Math.abs could work






share|improve this answer


























  • Would I then have to override compareTo(Object o) in order for that comparison to happen? I don't seem to be able to because I'm passing in KCtrSrv and not an Object..

    – Nick Pav
    Nov 29 '18 at 3:10











  • I changed the parameter to Object x and casted it to KCtrSrv within the method and I was then able to override, and now the comparison seems to be working. Thank you so much!

    – Nick Pav
    Nov 29 '18 at 3:14











  • Glad you could make it work. An alternative to what you did is changing implements Comparable to implements Comparable<KCtrSrv>. With the first one you're saying "objects of this class can be compared with anything that exists", with the second you'd be restricting it to objects of the same class and wouldn't need to cast.

    – Joni
    Nov 29 '18 at 13:53
















0














To make the treeset compare only based on center, you can write your compareto method like this:



public int compareTo(KCtrSrv x) {
return Double.compare(this.center, x.center);
}


On a side note, a.compareTo(b) is supposed to return a negative number when a is "less than" b. I don't see how an implementation that uses Math.abs could work






share|improve this answer


























  • Would I then have to override compareTo(Object o) in order for that comparison to happen? I don't seem to be able to because I'm passing in KCtrSrv and not an Object..

    – Nick Pav
    Nov 29 '18 at 3:10











  • I changed the parameter to Object x and casted it to KCtrSrv within the method and I was then able to override, and now the comparison seems to be working. Thank you so much!

    – Nick Pav
    Nov 29 '18 at 3:14











  • Glad you could make it work. An alternative to what you did is changing implements Comparable to implements Comparable<KCtrSrv>. With the first one you're saying "objects of this class can be compared with anything that exists", with the second you'd be restricting it to objects of the same class and wouldn't need to cast.

    – Joni
    Nov 29 '18 at 13:53














0












0








0







To make the treeset compare only based on center, you can write your compareto method like this:



public int compareTo(KCtrSrv x) {
return Double.compare(this.center, x.center);
}


On a side note, a.compareTo(b) is supposed to return a negative number when a is "less than" b. I don't see how an implementation that uses Math.abs could work






share|improve this answer















To make the treeset compare only based on center, you can write your compareto method like this:



public int compareTo(KCtrSrv x) {
return Double.compare(this.center, x.center);
}


On a side note, a.compareTo(b) is supposed to return a negative number when a is "less than" b. I don't see how an implementation that uses Math.abs could work







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 3:07

























answered Nov 29 '18 at 3:00









JoniJoni

77.8k999155




77.8k999155













  • Would I then have to override compareTo(Object o) in order for that comparison to happen? I don't seem to be able to because I'm passing in KCtrSrv and not an Object..

    – Nick Pav
    Nov 29 '18 at 3:10











  • I changed the parameter to Object x and casted it to KCtrSrv within the method and I was then able to override, and now the comparison seems to be working. Thank you so much!

    – Nick Pav
    Nov 29 '18 at 3:14











  • Glad you could make it work. An alternative to what you did is changing implements Comparable to implements Comparable<KCtrSrv>. With the first one you're saying "objects of this class can be compared with anything that exists", with the second you'd be restricting it to objects of the same class and wouldn't need to cast.

    – Joni
    Nov 29 '18 at 13:53



















  • Would I then have to override compareTo(Object o) in order for that comparison to happen? I don't seem to be able to because I'm passing in KCtrSrv and not an Object..

    – Nick Pav
    Nov 29 '18 at 3:10











  • I changed the parameter to Object x and casted it to KCtrSrv within the method and I was then able to override, and now the comparison seems to be working. Thank you so much!

    – Nick Pav
    Nov 29 '18 at 3:14











  • Glad you could make it work. An alternative to what you did is changing implements Comparable to implements Comparable<KCtrSrv>. With the first one you're saying "objects of this class can be compared with anything that exists", with the second you'd be restricting it to objects of the same class and wouldn't need to cast.

    – Joni
    Nov 29 '18 at 13:53

















Would I then have to override compareTo(Object o) in order for that comparison to happen? I don't seem to be able to because I'm passing in KCtrSrv and not an Object..

– Nick Pav
Nov 29 '18 at 3:10





Would I then have to override compareTo(Object o) in order for that comparison to happen? I don't seem to be able to because I'm passing in KCtrSrv and not an Object..

– Nick Pav
Nov 29 '18 at 3:10













I changed the parameter to Object x and casted it to KCtrSrv within the method and I was then able to override, and now the comparison seems to be working. Thank you so much!

– Nick Pav
Nov 29 '18 at 3:14





I changed the parameter to Object x and casted it to KCtrSrv within the method and I was then able to override, and now the comparison seems to be working. Thank you so much!

– Nick Pav
Nov 29 '18 at 3:14













Glad you could make it work. An alternative to what you did is changing implements Comparable to implements Comparable<KCtrSrv>. With the first one you're saying "objects of this class can be compared with anything that exists", with the second you'd be restricting it to objects of the same class and wouldn't need to cast.

– Joni
Nov 29 '18 at 13:53





Glad you could make it work. An alternative to what you did is changing implements Comparable to implements Comparable<KCtrSrv>. With the first one you're saying "objects of this class can be compared with anything that exists", with the second you'd be restricting it to objects of the same class and wouldn't need to cast.

– Joni
Nov 29 '18 at 13:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531043%2ftreeset-of-custom-objects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks