2014 05 19 NY Java User Group
2014 05 19 NY Java User Group
This presentation reflects information available to the Technology Division of Goldman Sachs only and not any other part of Goldman Sachs. It should not be relied upon
or considered investment advice. Goldman, Sachs & Co. (“GS”) does not warrant or guarantee to anyone the accuracy, completeness or efficacy of this presentation, and
recipients should not rely on it except at their own risk. This presentation may not be forwarded or disclosed except with this disclaimer intact.
1
We Did It!
“Two years, seven months, and eighteen days after the
release of JDK 7, production-ready builds of JDK 8 are
now available for download!”
(from a blog post by Mark Reinhold, the Chief Architect of the
Java Platform Group at Oracle)
TransactionManager.execute(() -> {
Person person = new Person();
person.setName("Bob");
HELLO
person.setAge(55); my name is
}); Statement
Lambda
this.people.sort(new Comparator<Person>()
{
public int compare(Person p1, Person p2)
{
return p1.getName().compareTo(p2.getName());
}
});
this.people.sort(c);
new Thread(r).start();
}
nameStream.forEach(System.out::println);
// When stream values are gone, they are gone. Let’s try again:
nameStream.forEach(System.out::println);
.collect(Collectors.<String>toList());
names.forEach(System.out::println);
• When you extend an interface that contains a default method, you can do the
following:
– Not mention the default method at all, which lets your extended interface inherit the default
method
– Redeclare the default method, which makes it abstract
– Redefine the default method, which overrides it
• Interfaces are stateless
– Java does not have multiple implementation inheritance
– Java always had multiple interface inheritance
Immutable Collections
BiMaps
17
Design Concepts
RichIterable
Bag, Set, List,
Stack, Map,
etc.
Readable
Interface
Mutable Immutable
Interface Interface
MutableBag,
ImmutableBag,
MutableList,
ImmutableList,
etc.
etc.
© 2014 Goldman Sachs. All rights reserved. 18
Collection Hierarchy
50
JDK HashSet
40
GSC UnifiedSet
Size (Mb)
30
Trove THashSet
20
10
Elements
40
JDK HashMap
35
GSC UnifiedMap
30
Trove THashMap
25 JDK Hashtable
Size (Mb)
20
15
10
Elements
20 JDK ArrayList
GSC IntArrayList
15 Trove TIntArrayList
Size (Mb)
10
Elements
/**
* Get the name of each of the company's customers.
*/
MutableList<Customer> customers = this.company.getCustomers();
MutableList<String> customerNames = customers.collect(nameFunction);
MutableList<String> expectedNames =
FastList.newListWith("Fred", "Mary", "Bill");
Assert.assertEquals(expectedNames, customerNames);
}
© 2014 Goldman Sachs. All rights reserved. 28
GSC Kata Example#1 w/ JDK 8
@Test
public void getCustomerNames()
{
Function<Customer, String> fn = c -> c.getName();
/**
* Get the name of each of the company's customers.
*/
MutableList<Customer> customers = this.company.getCustomers();
MutableList<String> customerNames = customers.collect(fn);
MutableList<String> expectedNames =
FastList.newListWith("Fred", "Mary", "Bill");
Assert.assertEquals(expectedNames, customerNames);
}
MutableList<Customer> selected =
customers.select(new Predicate<Customer>()
{
public boolean accept(Customer customer)
{
return "London".equals(customer.getCity());
}
});
MutableList<Customer> selected =
customers.selectWith(Customer::livesIn, "London");
// On Customer class
public boolean livesIn(String city)
{
return city.equals(this.city);
}
34
GSC Kata Example#4 w/ JDK 5-7 (2)
// In Order Class
public static final Function<Order, Iterable<LineItem>> TO_LINE_ITEMS =
new Function<Order, Iterable<LineItem>>()
{
public Iterable<LineItem> valueOf(Order order)
{
return order.lineItems;
}
};
// In LineItem Class
public static final DoubleFunction<LineItem> TO_VALUE =
new DoubleFunction<LineItem>()
{
public double doubleValueOf(LineItem lineItem)
{
return lineItem.value;
}
};
35
GSC Kata Example#4 w/ JDK 8
// Create a multimap where the values are customers and the key is the price of
// the most expensive item that the customer ordered.
@Test
public void mostExpensiveItem()
{
MutableListMultimap<Double, Customer> multimap = this.company.getCustomers()
.groupBy(customer -> customer.getOrders()
.asLazy()
.flatCollect(Order::getLineItems)
.collectDouble(LineItem::getValue)
.max());
Assert.assertEquals(3, multimap.size());
Assert.assertEquals(2, multimap.keysView().size());
Assert.assertEquals(
FastList.newListWith(
this.company.getCustomerNamed("Fred"),
this.company.getCustomerNamed("Bill")),
multimap.get(50.0));
}
@Test
public void totalOrderValuesByItem()
{
MutableMap<String, Double> map = this.company.getOrders()
.flatCollect(Order::getLineItems)
.aggregateBy(LineItem::getName,
() -> 0.0,
(result, lineItem) -> result + lineItem.getValue());
Verify.assertSize(12, map);
Assert.assertEquals(100.0, map.get("shed"), 0.0);
Assert.assertEquals(10.5, map.get("cup"), 0.0);
}
http://openjdk.java.net/projects/code-tools/jmh/
© 2014 Goldman Sachs. All rights reserved. 40
JDK 8 vs. GS Collections Benchmarks
Mean Throughput
0 50 100 150 200 250
Parallel Select/Filter
Serial Select/Filter
Parallel CollectIf/FilterMap
SerialCollectIf/FilterMap
@GenerateMicroBenchmark
public void serial_lazy_jdk()
{
List<Integer> evens = this.integersJDK.stream().filter(each -> each % 2 == 0).collect(Collectors.toList());
Assert.assertEquals(SIZE / 2, evens.size());
}
@GenerateMicroBenchmark
public void serial_eager_gsc()
{
MutableList<Integer> evens = this.integersGSC.select(each -> each % 2 == 0);
Assert.assertEquals(SIZE / 2, evens.size());
}
...
Output: