How to optimize a method written in Java using any of the Algorithm or Data Structure technique? [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • Given a set of rectangles, do any overlap?

    3 answers



  • find overlapping rectangles algorithm

    12 answers




I have a list of rectangles sorted by priority.



While we iterate this List, I will insert a rectangle element into a Set. This set includes discrete rectangles. It means that all rectangles in Set should not overlap each other.



The below code has O(n^2) time complexity. But there seems to be a better way.



public Collection<Box> removeOverlapping(Collection<Box> boxList) {
Collection<Box> boxListRemovedOverlapping = new LinkedList<>();
for (Box box : boxList) {
boolean overlapping = false;
for (Box compBox : boxListRemovedOverlapping) {
if (isBoxOverlapped(box, compBox)) {
overlapping = true;
break;
}
}
if (!overlapping) {
boxListRemovedOverlapping.add(box);
}
}
return boxListRemovedOverlapping;
}









share|improve this question















marked as duplicate by Andreas java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 5:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • "But there seems to be a better way" What way would that be, and if so, why didn't you use it?
    – Andreas
    Nov 13 at 5:26












  • What is condition of overlapped box?
    – htpvl
    Nov 13 at 5:31










  • @Andreas I am looking for it.
    – Bongousse
    Nov 13 at 5:32












  • @htpvl The overlapped means one box includes the other or intersects with it.
    – Bongousse
    Nov 13 at 5:33












  • Consider some BSP data structure like R-tree
    – MBo
    Nov 13 at 5:33

















up vote
-1
down vote

favorite













This question already has an answer here:




  • Given a set of rectangles, do any overlap?

    3 answers



  • find overlapping rectangles algorithm

    12 answers




I have a list of rectangles sorted by priority.



While we iterate this List, I will insert a rectangle element into a Set. This set includes discrete rectangles. It means that all rectangles in Set should not overlap each other.



The below code has O(n^2) time complexity. But there seems to be a better way.



public Collection<Box> removeOverlapping(Collection<Box> boxList) {
Collection<Box> boxListRemovedOverlapping = new LinkedList<>();
for (Box box : boxList) {
boolean overlapping = false;
for (Box compBox : boxListRemovedOverlapping) {
if (isBoxOverlapped(box, compBox)) {
overlapping = true;
break;
}
}
if (!overlapping) {
boxListRemovedOverlapping.add(box);
}
}
return boxListRemovedOverlapping;
}









share|improve this question















marked as duplicate by Andreas java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 5:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • "But there seems to be a better way" What way would that be, and if so, why didn't you use it?
    – Andreas
    Nov 13 at 5:26












  • What is condition of overlapped box?
    – htpvl
    Nov 13 at 5:31










  • @Andreas I am looking for it.
    – Bongousse
    Nov 13 at 5:32












  • @htpvl The overlapped means one box includes the other or intersects with it.
    – Bongousse
    Nov 13 at 5:33












  • Consider some BSP data structure like R-tree
    – MBo
    Nov 13 at 5:33















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:




  • Given a set of rectangles, do any overlap?

    3 answers



  • find overlapping rectangles algorithm

    12 answers




I have a list of rectangles sorted by priority.



While we iterate this List, I will insert a rectangle element into a Set. This set includes discrete rectangles. It means that all rectangles in Set should not overlap each other.



The below code has O(n^2) time complexity. But there seems to be a better way.



public Collection<Box> removeOverlapping(Collection<Box> boxList) {
Collection<Box> boxListRemovedOverlapping = new LinkedList<>();
for (Box box : boxList) {
boolean overlapping = false;
for (Box compBox : boxListRemovedOverlapping) {
if (isBoxOverlapped(box, compBox)) {
overlapping = true;
break;
}
}
if (!overlapping) {
boxListRemovedOverlapping.add(box);
}
}
return boxListRemovedOverlapping;
}









share|improve this question
















This question already has an answer here:




  • Given a set of rectangles, do any overlap?

    3 answers



  • find overlapping rectangles algorithm

    12 answers




I have a list of rectangles sorted by priority.



While we iterate this List, I will insert a rectangle element into a Set. This set includes discrete rectangles. It means that all rectangles in Set should not overlap each other.



The below code has O(n^2) time complexity. But there seems to be a better way.



public Collection<Box> removeOverlapping(Collection<Box> boxList) {
Collection<Box> boxListRemovedOverlapping = new LinkedList<>();
for (Box box : boxList) {
boolean overlapping = false;
for (Box compBox : boxListRemovedOverlapping) {
if (isBoxOverlapped(box, compBox)) {
overlapping = true;
break;
}
}
if (!overlapping) {
boxListRemovedOverlapping.add(box);
}
}
return boxListRemovedOverlapping;
}




This question already has an answer here:




  • Given a set of rectangles, do any overlap?

    3 answers



  • find overlapping rectangles algorithm

    12 answers








java algorithm data-structures collections






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 6:46









Abhinav

296111




296111










asked Nov 13 at 5:24









Bongousse

214




214




marked as duplicate by Andreas java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 5:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Andreas java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 5:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • "But there seems to be a better way" What way would that be, and if so, why didn't you use it?
    – Andreas
    Nov 13 at 5:26












  • What is condition of overlapped box?
    – htpvl
    Nov 13 at 5:31










  • @Andreas I am looking for it.
    – Bongousse
    Nov 13 at 5:32












  • @htpvl The overlapped means one box includes the other or intersects with it.
    – Bongousse
    Nov 13 at 5:33












  • Consider some BSP data structure like R-tree
    – MBo
    Nov 13 at 5:33




















  • "But there seems to be a better way" What way would that be, and if so, why didn't you use it?
    – Andreas
    Nov 13 at 5:26












  • What is condition of overlapped box?
    – htpvl
    Nov 13 at 5:31










  • @Andreas I am looking for it.
    – Bongousse
    Nov 13 at 5:32












  • @htpvl The overlapped means one box includes the other or intersects with it.
    – Bongousse
    Nov 13 at 5:33












  • Consider some BSP data structure like R-tree
    – MBo
    Nov 13 at 5:33


















"But there seems to be a better way" What way would that be, and if so, why didn't you use it?
– Andreas
Nov 13 at 5:26






"But there seems to be a better way" What way would that be, and if so, why didn't you use it?
– Andreas
Nov 13 at 5:26














What is condition of overlapped box?
– htpvl
Nov 13 at 5:31




What is condition of overlapped box?
– htpvl
Nov 13 at 5:31












@Andreas I am looking for it.
– Bongousse
Nov 13 at 5:32






@Andreas I am looking for it.
– Bongousse
Nov 13 at 5:32














@htpvl The overlapped means one box includes the other or intersects with it.
– Bongousse
Nov 13 at 5:33






@htpvl The overlapped means one box includes the other or intersects with it.
– Bongousse
Nov 13 at 5:33














Consider some BSP data structure like R-tree
– MBo
Nov 13 at 5:33






Consider some BSP data structure like R-tree
– MBo
Nov 13 at 5:33



















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?