I am using Collections.sort()in a List of size 100000 and getting StackOverFlow error. How can I scale this up? Here is the code:
This is a part of a big project. Collections.sort() is being called iteratively over the list as follows: The list size decreases at each step and the process iterates until the size of the list becomes 1.
public static Node buildTree(int d, List<kdtrees.DataPoint> list)
{
if(list.isEmpty())
{
return null;
}
else if(list.size() == 1) // S is singleton, return leaf
{
System.out.println(list.get(list.size() - 1).text);
Node t = new Node(0,0,null,null,list.get(list.size() - 1));
return t;
}
else
{
Collections.sort(list, compByX);
double m = findMedian(d, list);
List<kdtrees.DataPoint> left = new ArrayList<kdtrees.DataPoint>();
List<kdtrees.DataPoint> right = new ArrayList<kdtrees.DataPoint>();
for(DataPoint i: list)
{
if(i.Xvalue < m)
{
left.add(i);
}
else
{
right.add(i);
}
}
Node t = new Node(d, m, buildTree((d+1)%3,left)buildTree((d+1)%3,right), null);
return t ;
}