Skip to content

Instantly share code, notes, and snippets.

@Jimexist
Created November 21, 2016 04:07
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 Jimexist/ed68825468271578b75b2bf07567dcd3 to your computer and use it in GitHub Desktop.
Save Jimexist/ed68825468271578b75b2bf07567dcd3 to your computer and use it in GitHub Desktop.
Java 8 streaming API example
package com.madadata.demo.stream;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.io.Resources;
import io.dropwizard.jackson.Jackson;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.partitioningBy;
import static java.util.stream.Collectors.reducing;
/**
* Created by jiayu on 11/21/16.
*/
public class StreamDemo {
private static final ObjectMapper objectMapper = Jackson.newObjectMapper();
private static final List<UserOrder> userOrders;
static {
URL fixtureUrl = Resources.getResource("fixture/user_orders.json");
try {
String fixture = Resources.toString(fixtureUrl, Charsets.UTF_8);
userOrders = objectMapper.readValue(fixture, new TypeReference<List<UserOrder>>() {
});
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
private static Map<Boolean, Long> getGroupedByCashOnDelivery() {
return userOrders.stream().collect(
partitioningBy(UserOrder::isCashOnDelivery,
mapping(UserOrder::getTotal,
counting())));
}
private static Map<String, BigDecimal> getProvincialSum() {
return userOrders.stream().collect(
groupingBy(UserOrder::getProvince,
mapping(UserOrder::getTotal,
reducing(BigDecimal.ZERO, BigDecimal::add))));
}
private static Map<String, Map<String, BigDecimal>> getProvincialAndCitySum() {
return userOrders.stream().collect(
groupingBy(UserOrder::getProvince,
groupingBy(UserOrder::getCity,
mapping(UserOrder::getTotal,
reducing(BigDecimal.ZERO, BigDecimal::add)))));
}
public static void main(String[] args) throws Exception {
// provincial sum {四川=426.41, 河南=102.1}
System.out.printf("provincial sum %s\n", getProvincialSum());
// provincial and city sum {四川={德阳=31.31, 资阳=395.1}, 河南={开封=102.1}}
System.out.printf("provincial and city sum %s\n", getProvincialAndCitySum());
// number of orders and whether they are COD {false=1, true=2}
System.out.printf("number of orders and whether they are COD %s\n", getGroupedByCashOnDelivery());
}
}
[
{
"payTime": "2016-11-10T23:11:04",
"province": "四川",
"city": "资阳",
"total": 395.1,
"isCashOnDelivery": true
},
{
"payTime": "2016-11-12T04:12:44",
"province": "四川",
"city": "德阳",
"total": 31.31,
"isCashOnDelivery": false
},
{
"payTime": "2016-12-20T12:04:33",
"province": "河南",
"city": "开封",
"total": 102.1,
"isCashOnDelivery": true
}
]
package com.madadata.demo.stream;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.math.BigDecimal;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import org.joda.time.LocalDateTime;
/**
* Created by jiayu on 11/21/16.
*/
public class UserOrder {
@JsonProperty
@NotEmpty
private String province;
@JsonProperty
@NotEmpty
private String city;
@JsonProperty
@NotNull
private BigDecimal total;
@JsonProperty
private boolean isCashOnDelivery;
@JsonProperty
@NotNull
private LocalDateTime payTime;
@JsonProperty
public String getProvince() {
return province;
}
@JsonProperty
public String getCity() {
return city;
}
@JsonProperty
public BigDecimal getTotal() {
return total;
}
@JsonProperty
public boolean isCashOnDelivery() {
return isCashOnDelivery;
}
public LocalDateTime getPayTime() {
return payTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment