Group and map to enum for
each group total
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public
class Main
{
public
static
void main(String...args)
{
Map> o =
Food.menu.stream().collect(
Collectors.groupingBy(Food::getType, Collectors.mapping(
dish ->
{
if (dish.getCalories() <= 400)
return CaloricLevel.diet;
else
if (dish.getCalories() <= 700)
return CaloricLevel.normal;
else
return CaloricLevel.fat; },
Collectors.toSet() )));
System.out.println(o);
}
}
enum CaloricLevel { Diet, NormaL, Fat }
enum Type { Meat, Fish, Other }
class Food {
private
final String name;
private
final
boolean vegetarian;
private
final
int calories;
private
final Type type;
public Food(String name,
boolean vegetarian,
int calories, Type type)
{
this.name = name;
this.vegetarian = vegetarian;
this.calories = calories;
this.type = type;
}
public String getName()
{
return name;
}
public
boolean isVegetarian()
{
return vegetarian;
}
public
int getCalories()
{
return calories;
}
public Type getType()
{
return type;
}
public String toString()
{
return name;
}
public
static
final List menu =
Arrays.asList(
new Food(
"pork", false, 1800, Type.MEAT),
new Food(
"beef", false, 7100, Type.MEAT),
new Food(
"chicken", false, 1400, Type.MEAT),
new Food(
"french fries", true, 1530, Type.OTHER),
new Food(
"rice", true, 3510, Type.OTHER),
new Food(
"season fruit", true, 1120, Type.OTHER),
new Food(
"pizza", true, 5150, Type.OTHER),
new Food(
"prawns", false, 1400, Type.FISH),
new Food(
"salmon", false, 4150, Type.FISH));
}
Group by Food type
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public
class Main
{
public
static
void main(String...args)
{
Map> o = Food.menu.stream().collect(Collectors.groupingBy(Food::getType));
System.out.println(o);
}
}
enum Type { Meat, Fish, Other }
class Food {
private
final String name;
private
final
boolean vegetarian;
private
final
int calories;
private
final Type type;
public Food(String name,
boolean vegetarian,
int calories, Type type)
{
this.name = name;
this.vegetarian = vegetarian;
this.calories = calories;
this.type = type;
}
public String getName()
{
return name;
public
boolean isVegetarian()
{
return vegetarian;
}
public
int getCalories()
{
return calories;
}
public Type getType()
{
return type;
}
public String toString()
{
return name;
}
public
static
final List menu =
Arrays.asList(
new Food(
"pork", false, 1800, Type.Meat),
new Food(
"beef", false, 7100, Type.Meat),
new Food(
"chicken", false, 1400, Type.Meat),
new Food(
"french fries", true, 1530, Type.Other),
new Food(
"rice", true, 3510, Type.Other),
new Food(
"season fruit", true, 1120, Type.Other),
new Food(
"pizza", true, 5150, Type.Other),
new Food(
"prawns", false, 1400, Type.Fish),
new Food(
"salmon", false, 4150, Type.Fish));
}
Group by one property
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public
class Main
{
public
static
void main(String...args)
{
List transactions = Arrays.asList(
new Transaction(Currency.EUR, 1500.0),
new Transaction(Currency.USD, 2300.0),
new Transaction(Currency.GBP, 9900.0),
new Transaction(Currency.EUR, 1100.0),
new Transaction(Currency.JPY, 7800.0),
new Transaction(Currency.CHF, 6700.0),
new Transaction(Currency.EUR, 5600.0),
new Transaction(Currency.USD, 4500.0),
new Transaction(Currency.CHF, 3400.0),
new Transaction(Currency.GBP, 3200.0),
new Transaction(Currency.USD, 4600.0),
new Transaction(Currency.JPY, 5700.0),
new Transaction(Currency.EUR, 6800.0) );
Map> transactionsByCurrencies =
transactions.stream().collect(Collectors.groupingBy(Transaction::getCurrency));
System.out.println(transactionsByCurrencies);
}
}
class Transaction
{
private
final Currency currency;
private
final
double value;
public Transaction(Currency currency,
double value)
{
this.currency = currency;
this.value = value;
}
public Currency getCurrency()
{
return currency;
}
public
double getValue()
{
return value;
}
public String toString()
{
return currency +
" " + value;
}
}
enum Currency
{
Eur, usd, jpy, gbp, chf
}
No comments:
Post a Comment