Java 8????????????
???????????? ???????[ 2015/8/13 14:30:15 ] ??????????????????? ???????
???????????????????????????????????????????Щ????£???????????????Java??????????????????????
?????????Java 8??????????????????????????
??????????
????Java?г????????????£?
??????????
????Object obj; // may be an integer
????if (obj instanceof Integer) {
????Integer objAsInt = (Integer) obj;
????// do something with 'objAsInt'
????}
????????????? instanceof ??????????????Щ????????????????????????????????????????????????Integer????????????????????????????????????????????????
???????obj????Integer?????????????????????????κη???????????????????? ClassCastException ???????obj??null??intanceof ??????????????????????????????null??????κ????????á?
??????????
??????????????????????????Class?????????Щ??????????????????????????μ??
???????????????????
????Object obj; // may be an integer
????if (Integer.class.isInstance(obj)) {
????Integer objAsInt = Integer.class.cast(obj);
????// do something with 'objAsInt'
??????????????????????????????????????????????б??????????
??????????
????Object obj; // may be an integer
????Class<T> type = // may be Integer.class
????if (type.isInstance(obj)) {
????T objAsType = type.cast(obj);
????// do something with 'objAsType'
????}
?????????????????????????????????????????????????????????
?????????????? null ???????????????????????????μ??
????Stream??Optional?????
????????
?????? Optional ?е???? Stream ?е?????????????????裺??????????????????????????????????????????????????????
????Optional?е????
????Optional<?> obj; // may contain an Integer
????Optional<Integer> objAsInt = obj
????.filter(Integer.class::isInstance)
????.map(Integer.class::cast);
???????????????????????????????????????????????????о?????????????????
????δ?????????
?????????Class??????????????????? Optional ???? Stream???????????????????????????????????????????Optional??Stream?????????Optional??Stream???????κ?????
??????Щ??????????????飺
????Class????·???
????public Optional<T> castIntoOptional(Object obj) {
????if (isInstance(obj))
????return Optional.of((T) obj);
????else
????Optional.empty();
????}
????public Stream<T> castIntoStream(Object obj) {
????if (isInstance(obj))
????return Stream.of((T) obj);
????else
????Stream.empty();
????}
?????????????? flatMap ??????????????????
????FlatMap??????
????Stream<?> stream; // may contain integers
????Stream<Integer> streamOfInts = stream.
????flatMap(Integer.class::castIntoStream);
???????????????????null???????????????????????????????? Optional ?? Stream?????????????????? ClassCastException ????
?????????????
???????????????????Щ??????????????????
?????ж?????????????????????
????????????е????????????????????????????????????
?????????????????д???
????????????????????????
?????????Щ??????????????????????????????????????????0??????????????????????????治??????????0???
?????????????????????????????Щ??????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11