Skip to content

Instantly share code, notes, and snippets.

@sshark
Last active February 28, 2021 13:35
Show Gist options
  • Select an option

  • Save sshark/93f3b0f02e26ec5a4a5c998b38bed4bd to your computer and use it in GitHub Desktop.

Select an option

Save sshark/93f3b0f02e26ec5a4a5c998b38bed4bd to your computer and use it in GitHub Desktop.
The amount of noise reduced for a simple group and map task
case class Foo(title: String, ids: Set[String])
val xs =
List((123, "aa", "bb"), (233, "aa", "cc"), (233, "aa", ""), (233, "aa", "cc"), (334, "bb", "ee"))
val m = xs.groupBy(_._2).map { case (k, v) => Foo(k, v.map(_._3).filter(_.nonEmpty).toSet) }
println(m)
/*
public class Foo {
String title;
Set<String> ids;
}
List<Object[]> xs = Arrays
.asList(new Object[]{123, "aa", "bbc"}, new Object[]{233, "aa", "acc"},
new Object[]{334, "bb", "ee"});
List<ResourceResponse> list = xs.stream()
.collect(groupingBy(o -> String.valueOf(o[1]))).entrySet().stream()
.map(entry -> new AbstractMap.SimpleEntry<>(entry.getKey(),
entry.getValue().stream().flatMap(o -> {
String value = String.valueOf(o[2]);
return StringUtils.isEmpty(value) ? Stream.empty() : Stream.of(value);
}).collect(Collectors.toSet())))
.map(entry -> new Foo(entry.getKey(), new TreeSet<>(entry.getValue())))
.collect(
Collectors.toList());
System.out.println(list);
*/
package org.teckhooi
data class Foo(val title: String, val ids: Set<String>)
fun main(args: Array<String>) {
val xs = listOf(
Triple(123, "aa", "bb"),
Triple(233, "aa", "cc"),
Triple(233, "aa", ""),
Triple(233, "aa", "cc"),
Triple(334, "bb", "ee"))
println(xs.groupBy { it.second }.map { (k, v) -> Foo(k, v.map { it.third }.filter { it.isNotEmpty() }.toSet()) })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment