How can I take a Java set to fit X and split X / Y sets?

I have a Java suite (specifically a HashSet). Suppose it has a size of 10k. How can I split it into 5 sets each size 2k?

+4
source share
5 answers

This method will break the elements of the set so that the first set contains the first 2000, the second - the next 2000, etc.

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. 
+7
source

Guava has libraries for separating Iterable classes. Iterables is a utility class that has static methods for separating Iterable classes. The return value is an iteration of the lists. This code shows how to do this.

 Set<Integer> myIntSet = new HashSet<Integer>(); // fill the set Iterable<List<Integer>> lists = Iterables.partition(myIntSet, SIZE_EACH_PARTITION); 
+11
source

Iterate over the entire set and add the first 2000 elements to the first new set, 2nd 2000 elements to the second new set, etc.

0
source

If you do not want to write as you wish, check out guava . The List class has a method section (List, int) for splitting the list into several lists with the specified size. See Guava Lists

0
source

I wrote something that does set splitting.

It uses intermediate arrays and lists.

It uses the methods Arrays.asList and 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, of course, is not the most elegant solution, but it is the best that I could come up with at that moment when you did not want to loop sets yourself.

0
source

Source: https://habr.com/ru/post/1479819/


All Articles