본문 바로가기

Spring/lambda 함수

람다 함수

728x90
package com.shoppingmall.board;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("board")
public class BoardAction {
	
	@Autowired
	BoardService boardService;
	
	@RequestMapping("/lambda")
	public void lambda() {
    List<String> names = Arrays.asList("John", "Alice", "Bob", "Diana");
    /* 람다 표현식: Comparator <? super String> 인터페이스를 상속받은 아래의 람다 함수에 따라 정렬
      -> Sorts the specified list according to the order induced by thespecified comparator
    */
    Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
    System.out.println("Sorted with lambda: " + names); // [Alice, Bob, Diana, John]
	}
	
	
}
728x90