List<String>list=[“item1”,”item2”];//存 Stringitem=list[0];//直接取 Set<String>set={“item1”,”item2”,”item3”};//存 Map<String,Integer> map={“key1”:1,”key2”:2};//存 Intvalue=map[“key1”];//取
boolean Character.equalsIgnoreCase(char ch1, char ch2)
Math.safeToInt(longv); Math.safeNegate(int v); Math.safeSubtract(long v1, int v2); Math.safeMultiply(int v1, int v2)……
Map map = {name:”xxx”,age:18};
Collections.sort(names,(String a, String b) -> { returnb.compareTo(a);}); //对于函数体只有一行代码的,你能够去掉大括号{}以及return关键字。如: Collections.sort(names,(String a, String b) -> b.compareTo(a)); Collections.sort(names, (a, b) -> b.compareTo(a));
Set<Integer> ints = Set.of(1,2,3); List<String> strings = List.of("first","second");
除了更短和更好阅读以外,这些方法也能够避免您选择特定的集合实现。 事实上,从工厂方法返回已放入数个元素的集合实现是高度优化的。这是可能的,由于它们是不可变的:在建立后,继续添加元素到这些集合会致使 “UnsupportedOperationException” 。javascript
public interface MyInterface { void normalInterfaceMethod(); default void interfaceMethodWithDefault() { init(); } default void anotherDefaultMethod() { init(); } //This method is not part of the public API exposed by MyInterface private void init() { System.out.println("Initializing"); } }
若是您使用默认方法开发 API ,那么私有接口方法可能有助于构建其实现。java
HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder(URI.create("http://www.google.com")).header("User-Agent","Java").GET().build(); HttpResponse<String> resp = client.send(req, tpResponse.BodyHandler.asString()); HttpResponse<String> resp = client.send(req,HttpResponse.BodyHandler.asString());
除了这个简单的请求/响应模型以外,HttpClient 还提供了新的 API 来处理 HTTP/2 的特性,好比流和服务端推送。git