注解
-
java 注解:又称为java标记,jdk1.5开始引入的注释机制,注解可以通过反射获取标记的内容,编译器生成类字节码文件时,标记也可嵌入到字节码中。
-
java 内置注解:
-
使用在代码上的注解
-
@Override-检查方法是否被重写
public class Test extends AnnotationDemo { @Override public void test() { super.test(); } }
-
@Deprecated-标记过时
@Deprecated public String toLocaleString() { DateFormat formatter = DateFormat.getDateTimeInstance(); return formatter.format(this); }
-
@SuppressWarnings-忽略注解中声明的警告
-
-
元注解(DIV):
- @Documented:标记这个注解是否包含在用户文档中
- @Retention:标记注解保存的时间
- SOURCE:源代码中
- CLASS:字节码中
- RUNTME:运行时
- @Target:标记注解的使用目标
- METHOD
- FIELD
- TYPE
- @Inherited-标记注解是继承哪个注解类(默认是没有继承的)
-
自定义注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default "MyAnnotation"; } @MyAnnotation("eat1") public void eat(){ //s }
-
自定义注解
-
案例:自定义关系型数据库的动态sql (mybatis)
select username,address,phone from user where username=zhangsan and password=123456
java8 特性
-
Lambda表达式:允许一个函数作为方法参数传递到方法中去
(parm)->expression; 或 (parm)->{expression;}
-
实操
Callable<Integer> integerCallable = () -> 5; IntFunction<Integer> integerIntFunction = (int x) -> (3 * x); IntBinaryOperator intBinaryOperator = (int a, int b) -> a + b; Consumer<String> stringConsumer = (String s) -> { System.out.println(s); };
-
-
Stream:数据源的元素队列,支持聚合操作(想象成流)
-
数据源:集合,数组
-
聚合操作:filter,map,reduce,find,match,sorted
-
实操
-
filter(过滤)
List<String> list = new ArrayList(); list.add("A"); list.add(""); list.add("B"); System.out.println(list); List<String> collect = list.stream().filter(x -> !x.isEmpty()).collect(Collectors.toList());
-
forEach(循环)
list.stream().forEach(x-> System.out.println(x));
-
map(做映射)
List<Integer> collect1 = list1.stream().map(x -> x * x).collect(Collectors.toList());
-
limit(截取指定数量)
List<Integer> collect2 = list1.stream().limit(2).collect(Collectors.toList());
-
sorted(排序)
List<Integer> collect3 = list1.stream().sorted((a, b) -> b.compareTo(a)).collect(Collectors.toList());
-
Collectors(工具类)
Collectors.toList() Collectors.toSet() Collectors.toMap()
-
mapToInt(统计操作,max,min,avg,sum)
IntSummaryStatistics intSummaryStatistics = list1.stream().mapToInt(a -> a).summaryStatistics(); System.out.println(intSummaryStatistics.getMin());
-
-
-
接口中提供默认方法和静态方法
-
默认方法
public interface DefaultInterfaceDemo { default void eat(String name){ System.out.println(name); } }
-
静态方法
static void eat(String name,int count){ System.out.println(name); }
-
-
新的 date api
-
旧版存在问题:
- 设计较差(java.util,java.sql 都有date)
- 非线程安全的
- 处理时区比较麻烦
-
新版提供:
- Local(本地):简化了日期处理,没有时区的问题
- Zoned(时区):指定时区处理时间
//获取当前时间 LocalDateTime now = LocalDateTime.now(); System.out.println(now); //获取时间的年月日 System.out.println(now.getYear() +""+ now.getMonthValue() + now.getDayOfMonth()); //解析 DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime parse = LocalDateTime.parse("2025-10-11 11:11:12", pattern); System.out.println(parse); ZoneId zoneId = ZoneId.systemDefault(); System.out.println(zoneId); ZoneId of = ZoneId.of("Asia/Shanghai"); System.out.println(of);
-