I have a Java Set (specifically HashSet). Suppose it has a size of 10k. How can I break it into 5 Sets each of size 2k?
我有一个Java Set(特别是HashSet)。假设它的大小为10k。如何将其分为5套每个尺寸2k?
5 个解决方案
#1
7
This method will split the elements of the set so that the first set contains the first 2000, the second contains the next 2000, etc.
此方法将拆分集合的元素,以便第一个集合包含第一个2000,第二个集合包含下一个2000,等等。
public static <T> List<Set<T>> split(Set<T> original, int count) {
// Create a list of sets to return.
ArrayList<Set<T>> result = new ArrayList<Set<T>>(count);
// Create an iterator for the original set.
Iterator<T> it = original.iterator();
// Calculate the required number of elements for each set.
int each = original.size() / count;
// Create each new set.
for (int i = 0; i < count; i++) {
HashSet<T> s = new HashSet<T>(original.size() / count + 1);
result.add(s);
for (int j = 0; j < each && it.hasNext(); j++) {
s.add(it.next());
}
}
return result;
}
//As example, in your code...
Set<Integer> originalSet = new HashSet<Integer>();
// [fill the set...]
List<Set<Integer>> splitSets = split(originalSet, 5);
Set<Integer> first = splitSets.get(0); // etc.
#2
14
Guava has libraries to partition Iterable classes. The Iterables is a utility class which has static methods to partition Iterable classes. The return value is a Iterable of lists though. The given code shows how to do that.
Guava有用于分区Iterable类的库。 Iterables是一个实用程序类,它具有分区Iterable类的静态方法。返回值虽然是Iterable列表。给定的代码显示了如何执行此操作。
Set<Integer> myIntSet = new HashSet<Integer>();
// fill the set
Iterable<List<Integer>> lists = Iterables.partition(myIntSet, SIZE_EACH_PARTITION);
#3
0
Iterate over the entire set, and add the first 2000 elements to a first new set, the 2nd 2000 elements to the 2nd new set, etc.
迭代整个集合,将前2000个元素添加到第一个新集合,将第二个2000个元素添加到第二个新集合等。
#4
0
If you do not want to write it by your own have a look at guava. The Lists class have a method partition(List, int) to split a list into multiple lists with the specified size. See Guava Lists
如果你不想自己写它看看番石榴。 Lists类有一个方法分区(List,int),用于将列表拆分为具有指定大小的多个列表。参见番石榴名单
#5
0
I've written something that does the splitting of the set.
我写了一些东西,分裂了集合。
It uses intermediate arrays and Lists.
它使用中间数组和列表。
It uses the Arrays.asList and the Arrays.copyOfRange methods.
它使用Arrays.asList和Arrays.copyOfRange方法。
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetSplitTest {
//define and initialize set
private static Set<Integer> largeSet;
static {
largeSet = new HashSet<Integer>();
for (int i = 0; i < 10000; i++) {
largeSet.add(i);
}
}
public static void main() {
System.out.println(largeSet);
int amountOfSets = 5; //amount of subsets wanted
Set<Integer>[] subsets = new Set[amountOfSets]; //array holding the subsets
Integer[] largesetarray = largeSet.toArray(new Integer[largeSet.size()]);
for (int i = 1; i <= amountOfSets; i++) {
int fromIndex = (i-1) * largeSet.size() / amountOfSets;
int toIndex = i * largeSet.size() / amountOfSets - 1;
Set<Integer> subHashSet = new HashSet<Integer>();
subHashSet.addAll(Arrays.asList(Arrays.copyOfRange(largesetarray, fromIndex, toIndex)));
subsets[i - 1] = subHashSet;
}
for (Set<Integer> subset : subsets) {
System.out.println(subset);
}
}
}
This definately not the most elegant solution, but it's the best i could think off at the moment when not wanting to loop the sets yourself.
这绝对不是最优雅的解决方案,但它是我不能想要自己循环设置的那一刻想到的最好的。
#1
7
This method will split the elements of the set so that the first set contains the first 2000, the second contains the next 2000, etc.
此方法将拆分集合的元素,以便第一个集合包含第一个2000,第二个集合包含下一个2000,等等。
public static <T> List<Set<T>> split(Set<T> original, int count) {
// Create a list of sets to return.
ArrayList<Set<T>> result = new ArrayList<Set<T>>(count);
// Create an iterator for the original set.
Iterator<T> it = original.iterator();
// Calculate the required number of elements for each set.
int each = original.size() / count;
// Create each new set.
for (int i = 0; i < count; i++) {
HashSet<T> s = new HashSet<T>(original.size() / count + 1);
result.add(s);
for (int j = 0; j < each && it.hasNext(); j++) {
s.add(it.next());
}
}
return result;
}
//As example, in your code...
Set<Integer> originalSet = new HashSet<Integer>();
// [fill the set...]
List<Set<Integer>> splitSets = split(originalSet, 5);
Set<Integer> first = splitSets.get(0); // etc.
#2
14
Guava has libraries to partition Iterable classes. The Iterables is a utility class which has static methods to partition Iterable classes. The return value is a Iterable of lists though. The given code shows how to do that.
Guava有用于分区Iterable类的库。 Iterables是一个实用程序类,它具有分区Iterable类的静态方法。返回值虽然是Iterable列表。给定的代码显示了如何执行此操作。
Set<Integer> myIntSet = new HashSet<Integer>();
// fill the set
Iterable<List<Integer>> lists = Iterables.partition(myIntSet, SIZE_EACH_PARTITION);
#3
0
Iterate over the entire set, and add the first 2000 elements to a first new set, the 2nd 2000 elements to the 2nd new set, etc.
迭代整个集合,将前2000个元素添加到第一个新集合,将第二个2000个元素添加到第二个新集合等。
#4
0
If you do not want to write it by your own have a look at guava. The Lists class have a method partition(List, int) to split a list into multiple lists with the specified size. See Guava Lists
如果你不想自己写它看看番石榴。 Lists类有一个方法分区(List,int),用于将列表拆分为具有指定大小的多个列表。参见番石榴名单
#5
0
I've written something that does the splitting of the set.
我写了一些东西,分裂了集合。
It uses intermediate arrays and Lists.
它使用中间数组和列表。
It uses the Arrays.asList and the Arrays.copyOfRange methods.
它使用Arrays.asList和Arrays.copyOfRange方法。
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetSplitTest {
//define and initialize set
private static Set<Integer> largeSet;
static {
largeSet = new HashSet<Integer>();
for (int i = 0; i < 10000; i++) {
largeSet.add(i);
}
}
public static void main() {
System.out.println(largeSet);
int amountOfSets = 5; //amount of subsets wanted
Set<Integer>[] subsets = new Set[amountOfSets]; //array holding the subsets
Integer[] largesetarray = largeSet.toArray(new Integer[largeSet.size()]);
for (int i = 1; i <= amountOfSets; i++) {
int fromIndex = (i-1) * largeSet.size() / amountOfSets;
int toIndex = i * largeSet.size() / amountOfSets - 1;
Set<Integer> subHashSet = new HashSet<Integer>();
subHashSet.addAll(Arrays.asList(Arrays.copyOfRange(largesetarray, fromIndex, toIndex)));
subsets[i - 1] = subHashSet;
}
for (Set<Integer> subset : subsets) {
System.out.println(subset);
}
}
}
This definately not the most elegant solution, but it's the best i could think off at the moment when not wanting to loop the sets yourself.
这绝对不是最优雅的解决方案,但它是我不能想要自己循环设置的那一刻想到的最好的。