I have implemented a method to split HashMap
type container into same size of chunks. However, this method is not enough for my application, now. It restrict my application to scale. Thus, I have tried to change its generic type into <T extends Map<?,?>
. However, whenever I change this new type, I have faced with more hard problem like "how to create a generic type array or instance". I have not get rid of even if I have searched the web. How can I change my method to new Generic type?
My method;
public static <Type1, Type2>
HashMap<Type1, Type2>[]
split(HashMap<Type1, Type2> hashTable, int chunkSize) {
int numberOfChunks = ((int) Math.ceil( hashTable.size() / (double)chunkSize))
HashMap<Type1, Type2> []chunks = new HashMap[numberOfChunks];
Set<Type1> keys = hashTable.keySet();
int i = 0, j = -1;
for(Type1 key : keys){
if((i % chunkSize) == 0 ){
j = j + 1;
chunks[j] = new HashMap<Type1, Type2>();
}
chunks[j].put(key, (hashTable.get(key)));
i = i + 1;
}
return chunks;
}
I want to change this method to
public static <T extends Map<?,?>>
T[]
split(T hashTable, int chunkSize) {
int numberOfChunks = ((int) Math.ceil( hashTable.size() / (double)chunkSize))
T []chunks = new T[numberOfChunks]; // Problem I
Set<Type1> keys = hashTable.keySet();
int i = 0, j = -1;
for(Type1 key : keys){
if((i % chunkSize) == 0 ){
j = j + 1;
chunks[j] = new T(); // Problem II
}
chunks[j].put(key, (hashTable.get(key)));
i = i + 1;
}
return chunks;
}
How can I revolve to this new generic type, or how can I fix source of these problem, marked with // Problem
?