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