java 高级用法笔记

@PostContruct是spring框架的注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。
@Component
public class SettingManager { @Inject private SettingService settingService; @Inject private SettingItemService settingItemService; private static SettingManager settingManager; private Long userId; private List<SettingItem> settingItems; @PostConstruct public void init() { settingManager = this; } }

 

 

 

map的高级用法

    public StatictisDataWrapper(Map<String, List<Statistics>> map) {
        this.list = new ArrayList<>();
        map.forEach((string, statistics) -> list.add(new StatictisData(string, statistics)));
    }
map获取不到就设定默认值
String metastore = map.getOrDefault("hive.metastore.uris", "");
 

default的使用

@Target({TYPE,METHOD,FIELD,CONSTRUCTOR})  
@Retention(RetentionPolicy.RUNTIME)  
public @interface TestA {  
    String name();  
    int id() default 0;  
    Class<Long> gid();  
}  

 stream 聚合:

    public Map<String, Long> aggCount() {
        return this.getNotNullValues().stream().collect(Collectors.groupingBy(s -> s, Collectors.counting()));
    }

 FileUtil的用法

 /**
     * 文件操作
     **/

    private static void writeFile(File file, String content) {
        try {
            FileUtils.writeStringToFile(file, content, "UTF-8");
        } catch (IOException e) {
            logger.error("Exception:write title file error!", e);
        }
    }

    private static void writeLines(File file, List<String> lines) {
        try {
            FileUtils.writeLines(file, "UTF-8", lines);
        } catch (IOException e) {
            logger.error("Exception:write title file error!", e);
        }
    }

    private static String readFile(File file) {
        try {
            return FileUtils.readFileToString(file, "UTF-8");
        } catch (IOException e) {
            logger.error("Exception:read type file error", e);
            return "";
        }
    }

    private static List<String> readLines(File file) {
        try {
            return FileUtils.readLines(file, "UTF-8");
        } catch (IOException e) {
            logger.error("Exception:read type file error", e);
            return new ArrayList<>();
        }
    }
重写借口的方法
public
interface ProcessStreamHandler { void handle(String var1); } ProcessStreamHandler handler = (line) -> { FileUtil.writeFile(logFile, line + "\r\n", true); }
ProcessUtil processUtil = new ProcessUtil(handler);
 

 for用法

for(var5 = sparkAction.getAppArgs().iterator(); var5.hasNext(); launcher = launcher.addAppArgs(new String[]{arg})) {
                    arg = (String)var5.next();
                }

 

posted @ 2018-01-22 10:47  lucky_afu  阅读(179)  评论(0编辑  收藏  举报