package lesson04;
import java.util.Arrays;
import java.util.List;
/**
* Created by Administrator on 2018/1/4.
*/
public class Test4 {
public static void main(String[] args) {
String[] l={"Hive","Hbase"};
List<String> list = Arrays.asList(l);
final List<String> newList = new MyList(list).map(new Operator() {
@Override
public String operator(String word) {
//可以自定义输出样式
return "hello"+ word;
}
});
for(String newWord:newList){
System.out.println(newWord);
}
}
}
package lesson04;
/**
* Created by Administrator on 2018/1/4.
*/
public interface Operator {
String operator(String word);
}
package lesson04;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2018/1/4.
*/
public class MyList {
private List<String> list;
public MyList(List<String> list){
this.list=list;
}
public List<String> map( Operator operator){
List<String> newList = new ArrayList<String>();
for(String word:list){
//用到一个接口,可以保证这里不被写死,使其具有调用者自定义的功能。
String newWord = operator.operator(word);
newList.add(newWord);
}
return newList;
}
}