How can I take a Java Set of size X and break into X/Y Sets? -
i have java set (specifically hashset). suppose has size of 10k. how can break 5 sets each of size 2k?
this method split elements of set first set contains first 2000, second contains next 2000, etc.
public static <t> list<set<t>> split(set<t> original, int count) { // create list of sets return. arraylist<set<t>> result = new arraylist<set<t>>(count); // create iterator original set. iterator<t> = original.iterator(); // calculate required number of elements each set. int each = original.size() / count; // create each new set. (int = 0; < count; i++) { hashset<t> s = new hashset<t>(original.size() / count + 1); result.add(s); (int j = 0; j < each && it.hasnext(); j++) { s.add(it.next()); } } return result; } //as example, in code... set<integer> myintset = new hashset<integer>(); // [fill set...] list<set<integer>> splitsets = split(originalset, 5); set<integer> first = splitsets.get(0); // etc.
Comments
Post a Comment