스트림
// tip: each public class is put in its own file
public class main
{
// tip: arguments are passed via the field below this editor
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5,};
// 스트림 생성
IntStream stream = Array.stream(arr);
int sum = Arrays.stream(arr).sum();
System.out.print(sum);
// 한 번 생성한 스트림은 최종 연산까지 완료하면 이미 소모되어 버림
int count = (int)stream.count();
System.out.print(count));
}
}
public class ArrayListTest
{
// tip: arguments are passed via the field below this editor
public static void main(String[] args)
{
List<String> sList = new ArrayList<String>();
sList.add("Tomas");
sList.add("James");
Stream<String> stream = sList.stream();
stream.forEach(s -> System.out.println(s));
// OR
for(String s : sList){
System.out.println(s);
}
}
}
class CompareString implements BinaryOperator<String>{
@Override
public String apply(String s1, String s2){
if(s1.getBytes().length >= s2.getBytes().length)
return s1;
else return s2;}
}
public class reduceTest
{
// tip: arguments are passed via the field below this editor
public static void main(String[] args)
{
String[] greatings = {"안녕하세요", "Hello", "Good Morning", "반갑습니다."};
Arrays.stream(greetings).reduce("",(s1,s2)->{
if(s1.getBytes().length >= s2.getBytes().length)
return s1;
else return s2;}
));
String str = Arrays.stream(greetings).reduce(new CompareString()).get();
System.out.println(str);
}
}