Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Created September 20, 2012 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustingetz/3758511 to your computer and use it in GitHub Desktop.
Save dustingetz/3758511 to your computer and use it in GitHub Desktop.
public static Map<String, Collection<InboxEntry>> partitionByTaskName(Collection<InboxEntry> delegated)
// unfortunately generalizing this requires a higher order function and java makes me not really want to bother
// implementing default-dict correctly also needs a higher order function (default value constructor)
{
Map<String, Collection<InboxEntry>> taskMap = new HashMap<String, Collection<InboxEntry>>(); // this is a default-dict
for (InboxEntry inboxEntry : delegated)
{
// the partition key
String task_name = inboxEntry.getQueueItemAttributes().fetchSimpleValue("task_name");
Collection<InboxEntry> inboxEntries = taskMap.get(task_name);
if (null == inboxEntries)
{
inboxEntries = new ArrayList<InboxEntry>();
taskMap.put(task_name, inboxEntries);
}
inboxEntries.add(inboxEntry);
}
return taskMap;
}
@anakanemison
Copy link

public static <T, K> Multimap<K, T> partitionBy(final Iterable<? extends T> inputList, final Function<? super T, ? extends K> partitionFunc) {
        final Multimap<K, T> multimap = HashMultimap.create();
        for (T t : inputList) {
            multimap.put(partitionFunc.apply(t), t);
        }
        return multimap;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment