~~ It looks like a jargon ~~
stream.java
package stream;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
*Let's play with stream
*
* @author Ogawa
*
*/
public class Stream {
/**List of character strings that can be converted to numbers*/
private static final List<String> STRING_LIST = Arrays.asList("0", "2", "2", null, "4", null, "1");
/**List of numbers*/
private static final List<Integer> NUMBER_LIST = Arrays.asList(1, -1, null, 2, null, 2, -4);
/**
* main
*
* @param args
*/
public static void main(String[] args) {
System.out.println(convertToStr(NUMBER_LIST));
System.out.println(convertToInt(STRING_LIST));
System.out.println(sum(NUMBER_LIST));
System.out.println(findPuls(NUMBER_LIST));
System.out.println(findMinus(NUMBER_LIST));
System.out.println(findEven(NUMBER_LIST));
System.out.println(findOdd(NUMBER_LIST));
}
/**
* List{@code <Integer>}List{@code <String>}Conversion to
*
* @param numList Input value
* @return conversion result
*/
private static List<String> convertToStr(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).map(v -> String.valueOf(v))
.collect(Collectors.toList());
}
/**
* List{@code <String>}List{@code <Integer>}Conversion to
*
* @param strList Input value
* @return conversion result
* @throws NumberFormatException When the argument contains an element that cannot be converted to a numerical value.
*/
private static List<Integer> convertToInt(List<String> strList) throws NumberFormatException {
return strList.stream().filter(Objects::nonNull).map(v -> Integer.parseInt(v))
.collect(Collectors.toList());
}
/**
* List{@code <Integer>}Add internal numbers
*
* @param numList Input value
* @return Aggregation result
*/
private static int sum(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).mapToInt(v -> v).sum();
}
/**
* List{@code <Integer>}Is limited to positive integers
*
* @param numList Input value
* @return processing result
*/
private static List<Integer> findPuls(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).filter(v -> v >= 0).collect(Collectors.toList());
}
/**
* List{@code <Integer>}To narrow down to negative integers only
*
* @param numList Input value
* @return processing result
*/
private static List<Integer> findMinus(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).filter(v -> v <= 0).collect(Collectors.toList());
}
/**
* List{@code <Integer>}Squeeze to even numbers only
*
* @param numList Input value
* @return processing result
*/
private static List<Integer> findEven(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).filter(v -> v % 2 == 0).collect(Collectors.toList());
}
/**
* List{@code <Integer>}Squeeze to odd numbers only
*
* @param numList Input value
* @return processing result
*/
private static List<Integer> findOdd(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).filter(v -> v % 2 != 0).collect(Collectors.toList());
}
}
Convert the elements of List <Integer>
to List <String>
while eliminating nulls
convertToStr.java
private static List<String> convertToStr(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).map(v -> String.valueOf(v))
.collect(Collectors.toList());
}
Convert elements of List <String>
to List <Integer>
while eliminating nulls
convertToInt.java
private static List<Integer> convertToInt(List<String> strList) throws NumberFormatException {
return strList.stream().filter(Objects::nonNull).map(v -> Integer.parseInt(v))
.collect(Collectors.toList());
}
List <Integer>
Aggregate the internal numbers while eliminating nulls
sum.java
private static int sum(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).mapToInt(v -> v).sum();
}
Narrow down List <Integer>
to only positive integers
findPuls.java
private static List<Integer> findPuls(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).filter(v -> v >= 0).collect(Collectors.toList());
}
Limit List <Integer>
to only negative integers
findMinus.java
private static List<Integer> findMinus(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).filter(v -> v <= 0).collect(Collectors.toList());
}
Limit List <Integer>
to even numbers
findEven.java
private static List<Integer> findEven(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).filter(v -> v % 2 == 0).collect(Collectors.toList());
}
Narrow down List <Integer>
to odd numbers only
findOdd.java
private static List<Integer> findOdd(List<Integer> numList) {
return numList.stream().filter(Objects::nonNull).filter(v -> v % 2 != 0).collect(Collectors.toList());
}
Don't you want to make it?
Well, it seems unlikely that it will be used if it is just a simple operation ...?
Is it just an original model in the first place?
I want to prepare the basic operation in advance like this
~~ I want you to prepare this much on the java side ...? ~~
Recommended Posts