目录
Annotation
AnnotationDemo
@Auther:kongshan
@Date:2022/5/15-05-15-17:00
@Description:IntelliJ IDEA
@version:
*/
import java.util.Date;
public class AnnotationDemo {
private int num;
@Override
public String toString() {
return "AnnotationDemo{" +
"num=" + num +
'}';
}
/*
* 计算两个整数相加的和
* @param num1第一个数
* @param num2第二个数
* @return
*
* 两个数相加的和
* */
public int add(int num,int num2){
return num+num2;
}
@Deprecated
public void test(){
System.out.println("test");
}
public static void main(String[] args) {
AnnotationDemo ad=new AnnotationDemo();
// ad.test();
//使用反射技术创建对象
try {
AnnotationDemo ad2=(AnnotationDemo)Class.forName("class4.class_StreamAPI").newInstance();
ad2.test();
} catch (Exception e) {
e.printStackTrace();
}
Date date=new Date();
System.out.println(date.getYear());
}
}
FunctionInterfaceDemo
@Auther:kongshan
@Date:2022/5/15-05-15-17:13
@Description:IntelliJ IDEA
@version:
*/
@FunctionalInterface
public interface FunctionInterfaceDemo {
void suv(int a,int b);
}
MetaAnnotation
@Auther:kongshan
@Date:2022/5/15-05-15-17:14
@Description:IntelliJ IDEA
@version:
*/
import java.lang.annotation.*;
@MyAnnotation(age=20,joys = {"a","b","c"})
public class MetaAnnotation {
public void print(){
System.out.println("print.......");
}
}
//自定义注解
//target是说明自定义注解在什么位置可以使用,包、类、方法、属性等等
@Target({ElementType.METHOD,ElementType.TYPE})
//retention说明当前注解可以使用的环境,或者说什么级别保存注释信息,有三个级别,源码级别SOURCE,类级别CLASS,运行时环境RUNTIME,默认一般是RUNTIME级别
@Retention(RetentionPolicy.RUNTIME)
//说明注解是否可以像是在javadoc中
@Documented
//说明当前注释是否能被继承
@Inherited
@interface MyAnnotation{
//定义的方式看起来像是方法,但是实际上在使用注解的时候需要配置的一些参数名称和类型,默认的是value;
//自定义注解的中填写的参数名称对应方法名,参数的类型对应方法的返回类型需要一个一个的添加,写起来很麻烦,因此一般给它一些默认值
int id()default 1;
String name()default "";
int age()default 18;
String []joys() default "";
}
Test
@Auther:kongshan
@Date:2022/5/15-05-15-17:26
@Description:IntelliJ IDEA
@version:
*/
public class Test {
public void test(){
System.out.println("test");
}
}
Lambda
Demo1
@Auther:kongshan
@Date:2022/5/15-05-15-19:21
@Description:IntelliJ IDEA
@version:
*/
import java.util.function.*;
;
public class Demo1 {
public void getNum(Object num){
System.out.println("getNum"+num);
}
public String toUpperCase(String str){
return str.toUpperCase();
}
public Integer getLength(String str1,String str2){
return str1.length()+str2.length();
}
public int get(){
return 1;
}
public static void main(String[] args) {
// new Demo1().getNum(100);
//使用new关键字示例(instance)化一个对象
// Demo1 d=new Demo1();
Consumer<Integer> c1=(num)->new Demo1().getNum(num);
Consumer<Integer> c2=new Demo1()::getNum;
c1.accept(200);
c2.accept(300);
Function<String,String> f1 = (str)->str.toUpperCase();
Function<String,String>f2=(str)->new Demo1().toUpperCase(str);
Function<String,String> f3 =new Demo1()::toUpperCase;
System.out.println(f1.apply("abcdefg"));
System.out.println(f2.apply("abc"));
System.out.println(f3.apply("abc"));
UnaryOperator<String>uo=(str)->new Demo1().toUpperCase(str);
UnaryOperator<String >uo2=new Demo1()::toUpperCase;
System.out.println(uo.apply("abcdefghijk"));
System.out.println(uo2.apply("abcd"));
BiFunction<String,String,Integer>bf1=new BiFunction<String, String, Integer>() {
@Override
public Integer apply(String s, String s2) {
return new Demo1().getLength(s,s2);
}
};
System.out.println(bf1.apply("空衫","加油"));
BiFunction<String,String,Integer>bf2=(str1,str2)->new Demo1().getLength(str1,str2);
BiFunction<String,String,Integer>bf3=new Demo1()::getLength;
BiFunction<String,String,Integer>bf4=(str1,str2)->{
return new Demo1().getLength(str1,str2);
};
System.out.println(bf2.apply("abc","bcd"));
System.out.println(bf3.apply("空衫","加油"));
System.out.println(bf4.apply("空衫","666"));
Supplier<Integer>s1=new Demo1()::get;
System.out.println(s1.get());
}
}
Demo2
@Auther:kongshan
@Date:2022/5/15-05-15-19:49
@Description:IntelliJ IDEA
@version:
*/
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
public class Demo2 {
public static void main(String[] args) {
Consumer <Foo>c1=new Consumer<Foo>(){
@Override
public void accept(Foo foo) {
// new Foo().get();
// new Foo().get();
foo.get();
}
};
c1.accept(new Foo());
Consumer<Foo>c2=(foo)->new Foo().get();
Consumer<Foo>c3=(foo)->new Foo().get();
Consumer<Foo>c4=(foo)->new Foo().get();
Consumer<Foo>c5=(foo)->new Foo().get();
c2.accept(new Foo());
c3.accept(new Foo());
c4.accept(new Foo());
c5.accept(new Foo());
Function<Foo,Integer>f1=(foo)->new Foo().print();
Function<Foo,Integer>f2=Foo::print;
f1.apply(new Foo());
f2.apply(new Foo());
BiConsumer<Foo2,String> bc1=(foo, name)->new Foo2().show(name);
BiConsumer<Foo2,String>bc2=Foo2::show;
// BiConsumer<String,Foo2>bc3=Foo2::show;错误
bc1.accept(new Foo2(),"zhaoyun");
bc2.accept(new Foo2(),"guanyu");
}
static class Foo{
public void get(){
System.out.println("foo--get");
}
public int print(){
System.out.println("foo---print");
return 1;
}
}
static class Foo2{
public void get(){
System.out.println("foo2---get");
}
public int print(){
System.out.println("foo2---print");
return 2;
}
public void show(String name){
System.out.println("foo2---show---"+name);
}
}
}
Demo3
@Auther:kongshan
@Date:2022/5/15-05-15-22:54
@Description:IntelliJ IDEA
@version:
*/
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
public class Demo3 {
public static void main(String[] args) {
Supplier<Person>s1=new Supplier<Person>() {
@Override
public Person get() {
return new Person();
}
};
s1.get();
Supplier<Person> s2 = ()->new Person();
Supplier<Person> s3 = ()->{return new Person();};
Supplier<Person> s4 = Person::new;
s2.get();
s3.get();
s4.get();
Function<String,User>f=new Function<String, User>() {
@Override
public User apply(String s) {
return new User(s);
}
};
Function<String,User>f1=(name)->new User(name);
Function<String,User>f2=User::new;
f1.apply("zhangsan");
f2.apply("guanyu");
Function<Integer,User>f3=(age)->new User(age);
Function<Integer,User>f4=User::new;
f3.apply(25);
f4.apply(30);
List<String>list= Arrays.asList("a","b","c","d","e");
list.forEach(System.out::println);
}
}
class User{
public User(){
System.out.println("User的空构造方法被调用了.....");
}
public User(String naem){
System.out.println("User含有name的构造方法被调用了....");
}
public User(int age){
System.out.println("User含有age的构造方法被调用了.....");
}
}
class Person{
public Person(){
System.out.println("Person的空构造方法被调用了");
}
}
Stream
StreamDemo
@Auther:kongshan
@Date:2022/5/16-05-16-10:01
@Description:IntelliJ IDEA
@version:
*/
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamDemo {
//通过数组生成
public static void generate1(){
String []str={"a","b","c","d","e" };
Stream<String>stringStream=Stream.of(str);
stringStream.forEach(System.out::println);
}
//通过集合生成
public static void generate2(){
List<Integer>list= Arrays.asList(1,2,3,4,5);
Stream<Integer>stream=list.stream();
stream.forEach(System.out::println);
}
//通过generate生成
public static void generate3(){
// 生成10个1
Stream<Integer>stream=Stream.generate(()->1);
stream.limit(10).forEach(System.out::println);
}
public static void generate4(){
// 生成1-10的整数
Stream<Integer>stream=Stream.generate(()->{
int i=1;
for (int j = 0; j <100 ; j++) {
System.out.println(j);
}
return i;
});
Stream<Integer>stream1=Stream.iterate(1,(x)->x+1);
stream1.limit(10).forEach(System.out::println);
}
// 通过其他API创建
public static void generate5(){
String str="abcdefg";
IntStream intStream=str.chars();
intStream.forEach(System.out::println);
}
public static void main(String[] args) {
List<String>list =Arrays.asList("go","java","python","scala","javascript");
list.forEach(System.out::println);
generate1();
generate2();
generate3();
System.out.println("********************");
generate4();
generate5();
// 中间操作:如果调用方法之后返回的结果是Stream对象就意味着是一个中间操作
Arrays.asList(1,2,3,4,5,6).stream().filter(x->x%2==0).forEach(System.out::println);
System.out.println("********************");
// 原来的写法
List<Integer>list2=Arrays.asList(1,2,3,4,5,6);
// 1.for循环遍历
for (int i=0;i<list2.size();i++){
if (list2.get(i)%2==0){
System.out.println(list2.get(i));
}
}
// 2.迭代器iterator遍历
Iterator<Integer>iteratorIterator=list2.iterator();
while (iteratorIterator.hasNext()){
Integer next=iteratorIterator.next();
if (next%2==0){
System.out.println(next);
}
}
// 3.增强for循环
for (Integer integer:list2){
if (integer%2==0){
System.out.println(integer);
}
}
List<Integer>list1=Arrays.asList(1,2,3,4,5,6);
// 求出结果集中所有偶数的和
int sum=list1.stream().filter(x->x%2==0).mapToInt(x->x).sum();
System.out.println(sum);
//求集合中的最大值
Optional<Integer>max=list1.stream().max((a,b)->a-b);
System.out.println(max.get());
// 求集合的最小值
Optional<Integer>min=list1.stream().min((a,b)->a-b);
System.out.println(min.get());
// 原来的写法?
Optional<Integer>any=list1.stream().filter(x->x%2==0).findAny();
System.out.println(any.get());
Optional<Integer>first=list1.stream().filter(x->x%10==6).findFirst();
System.out.println(first.get());
Stream<Integer>integerStream=list1.stream().filter(i->{
System.out.println("代码运行!");
return i%2==0;
});
integerStream.findFirst();
System.out.println(integerStream.findFirst().get());
System.out.println(integerStream.findAny().get());
//获取最大值和最小值但是不适用min和max方法
Optional<Integer>min2=list1.stream().sorted().findFirst();
System.out.println(min2.get());
Optional<Integer>max2=list1.stream().sorted((a,b)->b-a).findFirst();
System.out.println(max2.get());
Arrays.asList("go","python","scala","java","javascript").stream().sorted().forEach(System.out::println);
Arrays.asList("go","python","scala","java","javascript").stream().sorted((a,b)->a.length()-b.length()).forEach(System.out::println);
// 原来的写法?如果不适用比较器如何写代码
// 想将集合中的元素进行过滤的同时返回一个集合对象
List<Integer>list3=list1.stream().filter(x->x%2==0).collect(Collectors.toList());
list3.forEach(System.out::println);
// 去重操作
Stream<Integer>integerStream1=Arrays.asList(1,2,3,4,5,6,7,8,9).stream().distinct();
integerStream1.forEach(System.out::println);
//打印20-30这样的集合数据
Stream.iterate(1,x->x+1).limit(30).skip(20).limit(10).forEach(System.out::println);
String str="10,20,30,40,50,60";
//原来的写法?
String[]strs=str.split(",");
int sum1=0;
for (String s:strs){
sum1+=Integer.valueOf(s);
}
System.out.println(sum1);
System.out.println(Stream.of(str.split(",")).mapToInt(x->Integer.valueOf(x)).sum());
System.out.println(Stream.of(str.split(",")).mapToInt(Integer::valueOf).sum());
System.out.println(Stream.of(str.split(",")).map(x->Integer.valueOf(x)).mapToInt(x->x).sum());
System.out.println(Stream.of(str.split(",")).map(Integer::valueOf).mapToInt(x->x).sum());
// 创建一组自定义对象
String names="张三,李四,王二,麻子,空衫";
//原来的写法
String []nameArray=names.split(",");
Student student=new Student();
for (String s:nameArray){
student.setName(s);
System.out.println(student);
}
Stream.of(names.split(",")).map(name->new Student(name)).forEach(System.out::println);
Stream.of(names.split(",")).map(Student::new).forEach(System.out::println);
System.out.println("*************************");
Stream.of(names.split(",")).map(name->Student.getStudent(name)).forEach(System.out::println);
Stream.of(names.split(",")).map(Student::getStudent).forEach(System.out::println);
// 将str中的每一个数值都打印出来,同时算出最终的求和结果
String str2="1,2,3,4,5,6,7,8,9";
System.out.println(Stream.of(str2.split(",")).peek(System.out::println).mapToInt(Integer::valueOf).sum());
System.out.println(list1.stream().allMatch(x->x>2));
System.out.println(list1.stream().anyMatch(x->x>2));
System.out.println(list1.stream().count());
}
}
Student
@Auther:kongshan
@Date:2022/5/16-05-16-10:34
@Description:IntelliJ IDEA
@version:
*/
public class Student {
private String name;
public Student() {
}
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static Student getStudent(String name){
Student student=new Student();
student.setName(name);
return student;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}