So I'm trying to read data from an excel file and I use Treemap to read the file. But the problem is that treemap puts my data random into its list. Although, I set an index when I use it.
//This writes the data from an old file
//writer is my treemap
//sReadData is a List with the old data
for (int i = 0; i < (sReadData.size()+1)/5; i++) {
writer.put(String.valueOf(i),new Object[] { sReadData.get(i*5),sReadData.get(i*5+1),sReadData.get(i*5+2),sReadData.get(i*5+3),sReadData.get(i*5+4)});
}
writer.put(String.valueOf(iCounter +1), new Object[] { "Name", "Date","Time", "Type","Owner"});
//This writes the new data
for (int i = 0; i < sName.length; i++) {
//iCounter is just the number of rows from the old file|These are just some Array with some data
writer.put(String.valueOf(iCounter+2+i), new Object[] { sName[i], sDate[i],sTime[i], sTypes[i],sOwners[i]});
}
My mistake was that I initialized my treemap wrong. It should be like this: Map<Integer, Object[]> tMap = new TreeMap<Integer, Object[]>();
The order won't be random. But it may not be what you expect. Strings are ordered lexically, so that "1" < "10" < "100" < ... < "11" ... < "2".
If you want the TreeMap
entries to be returned in numerical order, use Integer
as the key type.
When I try to use an
int
instead of aString
, I get this error:The method put(String, Object[]) in the type Map<String,Object[]> is not applicable for the arguments (int, Object[])
You also need to change the declaration of the map to TreeMap<Integer, Object[]>
.