Java实用代码片段

从1中取2的交集,移除所有存在1中但不存在于2中的记录

list1.retainAll(list2);

调用默认浏览器打开指定网址

try {
    Desktop d = Desktop.getDesktop();
    URI u = new URI("www.baidu.com");
    d.browse(u);
    } catch (Exception e) {
      e.printStackTrace();
  }

读取yaml配置文件,Yaml相关的库来自org.yaml.snakeyaml

Yaml yaml = new Yaml();
try (InputStream inputStream = AnyClass.class
          .getClassLoader()
          .getResourceAsStream("application.yml")) {
    Map obj = yaml.load(inputStream);
    obj.forEach((k, v) -> {
        System.out.println(k + "|type" + v.getClass() + "|" + GsonUtil.getInstance().toJson(v));
    });
}

如何处理InterruptException

public void run() {
        try {
            while (true) {
                // do stuff
            }
        } catch (InterruptedException e) {
            // record a log
            // Restore interrupted state... 
            Thread.currentThread().interrupt();
        }
    }

你可能感兴趣的:(Java实用代码片段)