如何使用流

时间:2018-08-10 05:40:10

标签: java java-8 java-stream

我有一个对象列表,其中包含交易媒介和交易金额。我想查找特定交易的总金额。

例如- 我在列表中有以下对象-

Credit card 10
Debit card 20
Credit card 30
Credit card 20
Debit card  20
Digital Wallet 30
Debit card 40

我想要这样的输出-

Credit Card 60
Debit card 80
Digital Wallet 30

我希望它使用Java 8流来完成。谁能帮忙!

1 个答案:

答案 0 :(得分:1)

您可以这样做

groupingBy

首先使用function NoOptionsMessage(props) { return ( <Typography color="textSecondary" className={props.selectProps.classes.noOptionsMessage} {...props.innerProps} > {props.children} </Typography> ); } function inputComponent({ inputRef, ...props }) { return <div ref={inputRef} {...props} />; } function Control(props) { ////console.dir(props.selectProps.inputValue); i am able to get the user input here // so i was thinking i can call my reducer somewhere here but no luck // my function is passed from my parent component so i wish to call this.props.theFunction here return ( <TextField fullWidth InputProps={{ inputComponent, inputProps: { className: props.selectProps.classes.input, ref: props.innerRef, children: props.children, ...props.innerProps, }, }} onChange={(e) => IntegrationReactSelect.testing(e)} /> ); } function Option(props) { return ( <MenuItem buttonRef={props.innerRef} selected={props.isFocused} component="div" style={{ fontWeight: props.isSelected ? 500 : 400, }} {...props.innerProps} > {props.children} </MenuItem> ); } function Placeholder(props) { return ( <Typography color="textSecondary" className={props.selectProps.classes.placeholder} {...props.innerProps} > {props.children} </Typography> ); } function SingleValue(props) { return ( <Typography className={props.selectProps.classes.singleValue} {...props.innerProps}> {props.children} </Typography> ); } function ValueContainer(props) { return <div className={props.selectProps.classes.valueContainer}>{props.children}</div>; } function MultiValue(props) { return ( <Chip tabIndex={-1} label={props.children} className={classNames(props.selectProps.classes.chip, { [props.selectProps.classes.chipFocused]: props.isFocused, })} onDelete={event => { props.removeProps.onClick(); props.removeProps.onMouseDown(event); }} /> ); } const components = { Option, Control, NoOptionsMessage, Placeholder, SingleValue, MultiValue, ValueContainer }; class IntegrationReactSelect extends React.Component { state = { single: null, multi: null, }; handleChange = name => value => { this.setState({ [name]: value, }); this.props.getSelectMultipleValue(value); }; render() { const { classes } = this.props; return ( <div className={classes.root}> <Select classes={classes} options={this.props.theUserFromParentComponent} components={components} value={this.state.multi} onChange={this.handleChange('multi')} placeholder={this.props.reactSelectName} isMulti /> </div> ); } } 收集器按其媒介对交易进行分组。然后使用下游收集器对每个组的交易金额求和。