哦。我有Java 8,想收集
class Celebration extends React.Component {
constructor(props){
super(props);
this.printProps = this.printProps.bind(this);
}
printProps(){
this.props.reset();
}
render(){
return (
<div className="alert alert-success alert-dismissible">
<a href="#" className="close" data-dismiss="alert" aria-label="close" onClick={this.printProps.bind()}>×</a>
<strong>Game Over!</strong> Player {this.props.winner} won!
</div>
);
}
}
class Square extends React.Component {
constructor(props) {
super(props);
this.state = {
value: null,
selected: this.props.selected
};
}
toggleState(){
if(this.state.value==null)
{
this.setState({value: this.props.value});
this.props.selected(this.props.number);
this.props.forToggle();
}
}
render() {
return (
<button
className="square"
onClick={() => this.toggleState()}
>
{this.state.value}
</button>
);
}
}
class Board extends React.Component {
renderSquare(i, val) {
return <Square value={i} number={val} forToggle={this.props.forToggle} selected={this.props.selected}/>;
}
render() {
const status = 'Next player: '+this.props.playerTurn;
return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(this.props.playerTurn,1)}
{this.renderSquare(this.props.playerTurn,2)}
{this.renderSquare(this.props.playerTurn,3)}
</div>
<div className="board-row">
{this.renderSquare(this.props.playerTurn,4)}
{this.renderSquare(this.props.playerTurn,5)}
{this.renderSquare(this.props.playerTurn,6)}
</div>
<div className="board-row">
{this.renderSquare(this.props.playerTurn,7)}
{this.renderSquare(this.props.playerTurn,8)}
{this.renderSquare(this.props.playerTurn,9)}
</div>
</div>
);
}
}
class Game extends React.Component {
constructor(props){
super(props);
this.state = this.initialState;
}
get initialState() {
return {
playerTurn: null,
selectedX: new Set(),
selectedO: new Set(),
winnerDecided: false
};
}
masterReset(){
this.setState({
winnerDecided: false
});
}
addInSelected(i){
let currentSet;
if(this.state.playerTurn == 'X')
currentSet=this.state.selectedX;
else
currentSet=this.state.selectedO;
currentSet.add(i);
if(currentSet.has(1) && currentSet.has(2) && currentSet.has(3))
this.setState({winnerDecided: true});
else if(currentSet.has(4) && currentSet.has(5) && currentSet.has(6))
this.setState({winnerDecided: true});
else if(currentSet.has(7) && currentSet.has(8) && currentSet.has(9))
this.setState({winnerDecided: true});
else if(currentSet.has(1) && currentSet.has(4) && currentSet.has(7))
this.setState({winnerDecided: true});
else if(currentSet.has(2) && currentSet.has(5) && currentSet.has(8))
this.setState({winnerDecided: true});
else if(currentSet.has(3) && currentSet.has(6) && currentSet.has(9))
this.setState({winnerDecided: true});
else if(currentSet.has(1) && currentSet.has(5) && currentSet.has(9))
this.setState({winnerDecided: true});
else if(currentSet.has(3) && currentSet.has(5) && currentSet.has(7))
this.setState({winnerDecided: true});
}
togglePlayer(){
if(this.state.playerTurn.localeCompare('X')==0)
this.setState({playerTurn: 'O'});
else
this.setState({playerTurn: 'X'});
}
decideWinner(){
if(this.state.playerTurn == 'X')
return 'O';
else
return 'X';
}
componentDidMount(){
this.setState({playerTurn: 'X'});
//alert("game mounted");
}
render() {
if(!this.state.winnerDecided)
return (
<div className="game">
<div className="game-board">
<Board playerTurn={this.state.playerTurn} selected={this.addInSelected.bind(this)} forToggle={this.togglePlayer.bind(this)}/>
</div>
<div className="game-info">
<div>{/* status */}</div>
<ol>{/* TODO */}</ol>
</div>
</div>
);
else
return (<div>
<Celebration winner={this.decideWinner()} reset={this.masterReset.bind(this)} />
</div>);
}
}
// ========================================
ReactDOM.render(
<Game />,
document.getElementById('root')
);
来自
Map<K, V>
由于冗长的语法,我不想使用Pair。有什么办法
Stream<Tuple2<K, V>>
感谢
答案 0 :(得分:2)
AFAIK ,这在Java中是无法避免的(与在scala中完成的方式相比。)
但是,如果您打算在代码中使用大量的元组到映射转换,并且希望避免冗长的语法,则可以创建自定义TupleCollector
并添加toMap
方法。
这可能是您进入scala的壁橱。
static class TupleCollector {
public static <K, V, T extends Tuple2<K, V>> Collector<T, ?, Map<K, V>> toMap() {
return Collectors.toMap(T::_1, T::_2);
}
}
调用代码
import static TupleCollector.toMap
...
myStream.collect(toMap());
PS: 再说一次,如果不通过代码审查阶段,我也不会感到惊讶。