�{�A�ڂł́A���܂�Java�̌o���͂����Ă��u�����_���́A�܂��m��Ȃ��v�Ƃ����l��ΏۂɃ����_���ɂ��ĉ�����Ă����܂��B����́AJava 8�̐V�@�\Stream API�̎g�����ɂ��āBStream�̐����A���ԓI�ȏ������s�����\�b�h�A�ŏI�I�ȏ������s�����\�b�h�A���ʂƂ��Ďg����Optional�ɂ��ăR�[�h��������ĉ�����܂��B
�@�O��́u�����_���Ŗ{�̂�������^�C���^�[�t�F�[�X��Stream API�̊�b�m���v�ł́A�����_�����g�����ƂŖ{�̂�����ėp�I�Ȋ��^�C���^�[�t�F�[�X��Stream API�̊T�v�����Ă��܂����B
�@����́A�O��Ɉ�������Stream API�ɂ��Č��Ă����A��ȃ��\�b�h�Ƃ��̎g�������w��ł����܂��傤�B
�@Stream API���g���ɂ́AStream�̃C���X�^���X�����Ȃ�������܂���B�܂��́A����Stream�C���X�^���X��������@�����Ă����܂��傤�B
�@Stream�͔z���Collection�Ȃǂ���쐬�\�ł��B�܂��͔z���Collection����Stream�̃C���X�^���X�������ȃ��\�b�h�����Ă݂܂��傤�B���L�̂��̂���ȃ��\�b�h�ł��B
�Ăяo�����N���X�^�C���^�[�t�F�[�X | ���\�b�h | �T�v |
---|---|---|
Collection<T> | stream() | Collection�̗v�f������Stream�̃C���X�^���X�� |
Arrays | stream(T[] array) | ������array�̗v�f������Stream�̃C���X�^���X�� |
Stream | of(T�c values) | ������values������Stream�̃C���X�^���X���B�܂������ɔz���n�����ꍇ�A���̔z��̗v�f�łł���Stream�C���X�^���X�����邱�Ƃ��\ |
List<String> list = Arrays.asList( "list1", "list2", "list3", "list4", "list5" ); String[] array = {"�z��1", "�z��2", "�z��3", "�z��4", "�z��5"}; // Stream�C���X�^���X�̐��� Stream<String> stream1 = list.stream(); Stream<String> stream2 = Arrays.stream(array); Stream<String> stream3 = Stream.of("�P", "�Q", "�R", "�S", "�T"); Stream<String> stream4 = Stream.of(array); // Stream�̊e�v�f��W���o�͂��܂��B stream1.forEach(e -> System.out.println("stream1:" + e)); stream2.forEach(e -> System.out.println("stream2:" + e)); stream3.forEach(e -> System.out.println("stream3:" + e)); stream4.forEach(e -> System.out.println("stream4:" + e));
stream1:list1 stream1:list2 stream1:list3 stream1:list4 stream1:list5 stream2:�z��1 stream2:�z��2 stream2:�z��3 stream2:�z��4 stream2:�z��5 stream3:�P stream3:�Q stream3:�R stream3:�S stream3:�T stream4:�z��1 stream4:�z��2 stream4:�z��3 stream4:�z��4 stream4:�z��5
�@�����Œ��ӂ��ׂ��_�́A���l������Stream������ۂɁA�w�肷��^�����l�̃v���~�e�B�u�^�Ȃ̂����b�p�[�N���X�Ȃ̂��ɂ���āA�Ԃ��Ă���Stream���ς�邱�Ƃł��B
�@�Ⴆ��Arrays#stream���\�b�h�̏ꍇ�A�����̔z���l�̃v���~�e�B�u�^�iint�Ȃǁj���ƁA����ɑΉ������^��Stream�iIntStream�Ȃǁj��Ԃ��܂��B�������A�������v���~�e�B�u�^�̃��b�p�[�N���X�iInteger�Ȃǁj����Stream<���b�p�[�N���X>��Ԃ��܂��B
int[] intArray = {1, 2, 3, 4, 5}; Integer[] integerArray = {1, 2, 3, 4, 5}; IntStream intArrayStream = Arrays.stream(intArray); Stream<Integer> integerArrayStream = Arrays.stream(integerArray);
�@���ɁA2��Stream�̗v�f�����킹��1��Stream�����郁�\�b�h�����Ă݂܂��傤�B
�Ăяo�����N���X�^�C���^�[�t�F�[�X | ���\�b�h | �T�v |
---|---|---|
Stream<T> | concat(Stream<? extends T> s1, Stream<? extends T> s2) | �v�f�������^�A�������́A�e�������^������s1��s2��Stream��������1��Stream�̃C���X�^���X�� |
Stream<String> stream1 = Stream.of("��", "��", "��", "��", "��"); Stream<String> stream2 = Stream.of("�A", "�C", "�E", "�G", "�I"); Stream<String> concatStream = Stream.concat(stream1, stream2); concatStream.forEach(value -> System.out.println(value));
�� �� �� �� �� �A �C �E �G �I
�@�܂��A����concat���\�b�h�̈�����1�ł�������Stream�̏ꍇ�A�߂�l��Stream���������s�����̂ɂȂ�܂��B
Stream<String> stream1 = Stream.of("��", "��", "��", "��", "��"); Stream<String> stream2 = Stream.of("�A", "�C", "�E", "�G", "�I"); Stream<String> parallelStream = stream1.parallel(); Stream<String> concatStream = Stream.concat(parallelStream, stream2); concatStream.forEach(value -> System.out.println(value)); System.out.println("concatStream.isParallel()=" + concatStream.isParallel());
�C �A �G �� �� �� �� �E �� �I concatStream.isParallel()=true
�@�O��̘A�ڂŐ��������悤��Java 8�ł͕������s��Stream�C���X�^���X���ȒP�ɐ������邽�߂̃��\�b�h���p�ӂ���Ă��܂��B�������s��Stream������ꍇ�͉��L�̃��\�b�h���g���܂��B
�Ăяo�����N���X�^�C���^�[�t�F�[�X | ���\�b�h | �T�v |
---|---|---|
Collection | parallelStream() | Collection�����ɕ������s��Stream�̃C���X�^���X�� |
BaseStream | parallel() | �Ăяo������Stream����������s��Stream��Ԃ� |
�@�܂��AStream�̃C���X�^���X���������s�����̂��ǂ����`�F�b�N����ɂ�BaseStream#isParallel���\�b�h�Ŋm�F�\�ł��B���̃��\�b�h�����s�����ۂ�True���Ԃ�A�������s�����̂ɂȂ�܂��B
�@�t�ɁA�������璼����Stream������ꍇ��BaseStream# sequential���\�b�h�Œ�����Stream���\�ł��B
List<String> list = Arrays.asList( "list1", "list2", "list3", "list4", "list5" ); // �������s��Stream�� Stream<String> parallelStream1 = list.parallelStream(); Stream<String> parallelStream2 = list.stream().parallel(); // �������ꂽStream���������s�����̂��m�F System.out.println("parallelStream1.isParallel()=" + parallelStream1.isParallel()); System.out.println("parallelStream2.isParallel()=" + parallelStream2.isParallel()); // �������璼�ڏ����ɕϊ� Stream<String> sequentialStream = parallelStream1.sequential(); System.out.println("sequentialStream.isParallel()=" + sequentialStream.isParallel());
parallelStream1.isParallel()=true parallelStream2.isParallel()=true sequentialStream.isParallel()=false
Copyright © ITmedia, Inc. All Rights Reserved.