Java 8速查表

Lambda表达式 —— 用简单的方法实现只有一个函数的接口

Lambda syntax

1
2
3
(parameters) -> expression
(parameters) -> statement
(parameters) -> { statements }

Lambda表达式实例

1
2
3
( int x, int y) -> x + y
() -> System.out.println( "hi " + s);
(String s) -> { int n = s.length(); return n; }

运行Runnable

1
2
Runnable r = () -> System.out.println( "Hello!" );
r.run();

PI函数

1
2
Callable<Double> pi = () -> 3.14 ;
Double p = pi.call();

根据字符串长度来排序字符串

1
2
3
4
5
String[] words = { "aaa" , "b" , "cc" };
Arrays.sort(words, (s1, s2) -> s1.length() - s2.length());
 
// 等价于:
Arrays.sort(words, (String s1, String s2) -> s1.length() - s2.length());

高效的final变量可以在lambdas中被引用

1
2
3
4
5
// s是高效的final变量(不会更改)
String s = "foo" ;
 
// s可以在lambdas中被引用
Runnable r = () -> System.out.println(s);

方法引用 —— 用函数的方法使用现有方法

静态方法引用

1
2
3
4
5
// Class::staticMethod syntax
Arrays.sort(items, Util::compareItems);
 
// 等价于:
Arrays.sort(items, (a, b) -> Util.compareItems(a, b));

实例方法引用

1
2
3
4
5
// instance::instanceMethod syntax
items.forEach(System.out::print);
 
// 等价于:
items.forEach((x) -> System.out.print(x));

对任意实例的方法的引用

1
2
3
4
5
// Class::instanceMethod syntax
items.forEach(Item::publish);
 
// 等价于:
items.forEach((x) -> { x.publish(); });

构造器引用

1
2
ConstructorReference cref = Item:: new ;
Item item = cref.constructor();

Default方法 —— 接口的方法进行默认的实现

在接口中定义默认的default方法

1
2
3
4
5
6
7
interface Descriptive {
 
   default String desc() {
     return "fantastic" ;
   }
 
}

实现包含default方法的接口

1
2
3
4
5
6
class Item implements Descriptive { }
 
Item x = new Item();
 
// prints "fantastic"
System.out.println(x.desc());

Stream – 值的序列

对非空字符进行计数

1
2
List<String> strings = ...;
long n = strings.stream().filter(x -> !x.isEmpty()).count();

连接元素的title

1
2
List<Item> items = ...;
String names = items.stream().map((x) -> x.getTitle()).collect(Collectors.joining( ", " ));

由‘城市’列表获得唯一的‘国家’列表

1
2
List<City> cities = ...;
List<Country> countries = cities.stream().map((c) -> c.getCountry()).distinct().collect(Collectors.toList());

获得元素rating的计数、最小值、最大值、总数、平均值等统计数据

1
2
List<Item> items = ...;
IntSummaryStatistics stats = items.stream().mapToInt((x) -> x.getRating()).summaryStatistics();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java函数速查,方便使用的Java类库查询手册 chm版,可查询以下四个包的所有类库内容:   Package com.ms.wfc.app   Application类   CharacterSet类   Clipboard类   DataFormats类   DataFormats.Formats类   DataObject类   IDataObject界面   IMessageFilter界面   Locale类   Locale.CalendarType类   Locale.DateFormatOrder类   Locale.Languages类   Locale.LeadingZeros类   Locale.MeasurementSystem类   Locale.NegativeNumberMode类   Locale.OptionalCalendarType类 Locale.PositiveCurrencyMode类   Locale.Sort类   Locale.SubLanguages类   Message类   MethodInvoker代理   Registry类   RegistryKey类   SendKeys类   SpecialFolder类   SystemInformation类   SystemInformation.ArrangeDirection类   SystemInformation.ArrangeStartingPosition类   ThreadExceptionDialog类   ThreadExceptionEvent类   ThreadExceptionEventHandler代理   Time类   Timer类   Version类   Window类   Package com.ms.wfc.data.ui   Column类   ColumnEditingEvent类   ColumnEditingEventHandler代理   ColumnEvent类   ColumnEventHandler代理   ColumnResizeEvent类   ColumnResizeEventHandler代理   ColumnUpdatingEvent类   ColumnUpdatingEventHandler代理   DataBinder类   DataBinding类   DataGrid类   DataNavigator类   DataSource类   EnterAction类   ErrorEvent类   ErrorEventHandler代理   GridLineStyle类   PositionChangeEvent类   PositionChangeEventHandler代理   Scrollbars类   TabAction类      Package com.ms.wfc.html   DhAlignment类   DhBaseContainer类   DhBorderInfo类   DhBorders类   DhBorderStyle类   DhBreak类   DhBrowser类   DhBulletedList类   DhButton类   DhCantAddElementException类   DhCantModifyElementException类   DhCell类   DhCheckBox类   DhComboBox类   DhComponentWrapper类   DhCursor类   DhDialogInfo类   DhDocument类   DhEdit类   DhElement类   DhElementExistsInDocumentException类   DhElementNotFoundException类   DhEnumeration类   DhEventInfo类   DhForm类   DhHorizontalRule类   DhHotSpot类   DhHotSpotShapes类   DhHTMLGenerator类   DhHyperlink类   DhImage类   DhInlineFrame类   DhInsertOptions类   DhJumpPoint类   DhLinkTarget类   DhListBox类   DhListType类   DhMargins类   DhMarginWidthI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值