方法引用和Stream和时间

package wang;

import java.awt.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class dome {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("欧阳雪",18,"中国",'F'));
        personList.add(new Person("Tom",24,"美国",'M'));
        personList.add(new Person("Harley",22,"英国",'F'));
        personList.add(new Person("向天笑",20,"中国",'M'));
        personList.add(new Person("李康",22,"中国",'M'));
        personList.add(new Person("小梅",20,"中国",'F'));
        personList.add(new Person("何雪",21,"中国",'F'));
        personList.add(new Person("李康",22,"中国",'M'));
        //findFirst match
       // Optional<Person> first = personList.stream().filter(item -> item.getSex() == 'M').findFirst();
      /*  boolean b = personList.stream().filter(item -> item.getSex() == 'M').allMatch(item -> item.getAge() > 20);//所有都满足

        System.out.println(b);*/

      /*  Supplier<Person> supplier = ()->{
            return new Person();
        };*/
        Supplier<Person> supplier = ()->{
            return new Person();
        };
        System.out.println(supplier.get());

        //对象方法的引用
        //Function<String,Integer> function = item->item.length();
       /* Function<String, Integer> function = String::length;
        System.out.println(function.apply("niihao"));*/
        //比较两个字符串是否相等
       /* BiFunction<String,String,Boolean> biFunction = (t,u)->{
            return t.equals(u);
        };*/
        /* BiFunction<String,String,Boolean> biFunction = String::equals;
        System.out.println(biFunction.apply("haha","heihei"));*/
        //collect搜索终止
        //求年龄大于20且性别为M
        /*List<Person> collect = personList.stream()
                .filter(item -> item.getAge() > 20)
                .filter(item -> item.getSex() == 'M')
                .collect(Collectors.toList());
        System.out.println(collect);*/
        //求集合中年龄的和
        /*Optional<Integer> reduce = personList.stream()
                .map(item -> item.getAge())
                .reduce((a, b) -> a + b);*/
       /* Integer reduce = personList.stream()
                .map(item -> item.getAge())
                .reduce(10, (a, b) -> a + b);
        System.out.println(reduce);*/
        //查找最大值
        /*Optional<Person> max = personList.stream()
                .max((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println(max.get());*/
        //按照年龄排序
        /*personList.stream()
                .sorted(((o1, o2) -> o1.getAge()- o2.getAge()))
                .forEach(System.out::println);*/

       /* boolean b = personList.stream().filter(item -> item.getAge() > 20).allMatch(item -> item.getSex() == 'M');
        System.out.println(b);*/


        /*Optional<Integer> reduce = personList.stream().map(item -> item.getAge()).reduce((a, b) -> a + b);
        System.out.println(reduce.get());*/

        //personList.stream().filter(item->item.getAge()==18).forEach(item-> System.out.println(item));
        /*personList.stream().map(item->{
            Map map = new HashMap();
            map.put("name",item.getName());
            map.put("age",item.getAge());
            return map;
        }).forEach(System.out::println);*/

    /*    ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰","何线程");
        list.stream()
                .filter(item->item.startsWith("张"))
                .filter(item->item.length()==3)
                .forEach(item-> System.out.println(item));
        int[] arr={2,1,23,1,5,4};
        IntStream stream = Arrays.stream(arr);
                stream.forEach(item-> System.out.println(item));*/
    }
}
class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", sex=" + sex +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }
}

package date;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class dome {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();//当前日期
        LocalDate of = LocalDate.of(2020,1,2);//自定义日期
        LocalTime now1 = LocalTime.now();//当前时间
        LocalTime of1 = LocalTime.of(12, 25, 12,16546);//自定义时间
        LocalDateTime now2 = LocalDateTime.now();//当前日期时间
        LocalDateTime of2 = LocalDateTime.of(2020, 5, 4, 12, 12, 12, 12);//自定义日期时间
        Duration between = Duration.between(of2, now2);//时间差Duration
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");//自定义类型
        LocalDate parse = LocalDate.parse("2021-12-11", dateTimeFormatter);//字符串转日期
        String format = parse.format(dateTimeFormatter);//日期转字符串

        System.out.println(between.toDays());
    }
}
### 如何在Spring Boot项目中集成LangChain4J #### 配置依赖项 为了使Spring Boot应用程序能够使用LangChain4J库,首先需要更新`pom.xml`文件来引入必要的依赖关系。考虑到当前基于Spring Boot 2.X和JDK 8的环境设置[^1],可以在项目的构建配置文件中加入如下Maven依赖: ```xml <dependency> <groupId>com.langchain4j</groupId> <artifactId>langchain4j-core</artifactId> <version>${langchain4j.version}</version> </dependency> ``` 这里`${langchain4j.version}`应替换为实际使用的LangChain4J版本号。 #### 初始化组件和服务 一旦添加了所需的依赖包,在应用启动时就可以通过创建相应的Bean实例来进行初始化操作。这通常是在某个配置类里完成的,比如下面的例子展示了如何定义一个简单的服务bean用于处理链上数据交互: ```java import com.langchain4j.client.LangChainClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class LangChainConfig { @Bean public LangChainClient langChainClient() { return new LangChainClient(/* 可选参数 */); } } ``` 上述代码片段假设存在名为`LangChainClient`的客户端接口或实现类,它负责与区块链网络通信并提供相应功能支持。 #### 使用自动装配简化开发流程 如果希望进一步减少样板代码量,则可以考虑利用Spring框架提供的@Autowired特性来自动生成所需对象实例。例如,在控制器或其他业务逻辑层可以直接注入之前声明过的`LangChainClient` bean而无需手动new出来: ```java @RestController @RequestMapping("/api/langchain") public class LangChainController { private final LangChainClient client; @Autowired public LangChainController(LangChainClient client) { this.client = client; } // 定义API端点... } ``` 这样做的好处是可以让开发者专注于编写核心业务逻辑而不是担心底层资源管理问题。 #### 处理多入口点的情况 对于那些可能拥有多个带有`main()`函数的应用程序来说——无论是因为它们各自携带了`@SpringBootApplication`注解还是仅仅作为普通的Java程序运行——需要注意的是,默认情况下只有第一个被发现的此类方法会被视为应用程序的主要入口。为了避免潜在冲突,建议指定特定的目标类作为主类,可以通过调整maven插件配置中的属性来达成此目的[^2]: ```xml <build> ... <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.MyApplicationWithMainMethod</mainClass> </configuration> </plugin> </plugins> ... </build> ``` 这样做能确保即使在同一工程中有其他候选者也不会干扰到预期的行为表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值