当前位置: 首页 > wzjs >正文

网站手机端和电脑端.net开发大型网站开发

网站手机端和电脑端,.net开发大型网站开发,代理加盟做什么好,在线wordpress文章目录 1.接口中默认方法修饰为普通方法2.lambda表达式如何定义函数接口无参有参精简代码实现集合遍历实现集合排序实现线程调用 3.Stream流strean将list转为setstream将list转为mapstream计算求和stream查找最大和最小stream中的matchstream过滤器stream实现limitstream实现…

文章目录

    • 1.接口中默认方法修饰为普通方法
    • 2.lambda表达式
      • 如何定义函数接口
      • 无参
      • 有参
      • 精简代码
      • 实现集合遍历
      • 实现集合排序
      • 实现线程调用
    • 3.Stream流
      • strean将list转为set
      • stream将list转为map
      • stream计算求和
      • stream查找最大和最小
      • stream中的match
      • stream过滤器
      • stream实现limit
      • stream实现数据排序

1.接口中默认方法修饰为普通方法
2.lambda表达式
3.Stream流接口
4.函数式接口
5.方法与构造函数引用

1.接口中默认方法修饰为普通方法

在jdk8之前,interface之中可以定义变量和方法,变量必须是public、static、final的,方法必须是public、abstract的,由于这些修饰符都是默认的。

接口定义方法:public抽象方法 需要子类实现
接口定义变量:public、static、final

在jdk1.8开始 支持使用static和default修饰 可以写方法体,不需要子类重写
普通方法 可以有方法体
抽象方法 没有方法体需要子类实现 重写

package com.xd;public interface JDK8Interface {/*** 默认的时候就是为public、abstract*/void add();/*** jdk8 提供默认的方法*/default void defaultGet(){System.out.println("defaultGet");}static void staticDel() {System.out.println("staticDel");}}
package com.xd;public class JDK8InterfaceImpl implements JDK8Interface{/*** 定义子类实现JDK8Interface 没有强制要求重写default和静态方法*/@Overridepublic void add() {System.out.println("add");}}
package com.xd;public class Test01 {public static void main(String[] args) {JDK8InterfaceImpl jdk8Interface = new JDK8InterfaceImpl();jdk8Interface.defaultGet();jdk8Interface.add();JDK8Interface.staticDel();}
}

2.lambda表达式

package com.xd;public class Test02 {public static void main(String[] args) {//1.使用匿名内部类的形式调用OrderService中的get方法
//        OrderService orderService = new OrderService() {
//            @Override
//            public void get() {
//                System.out.println("get");
//            }
//        };
//        orderService.get();//        new OrderService() {
//            @Override
//            public void get() {
//                System.out.println("get");
//            }
//        }.get();//        new OrderService() {
//            @Override
//            public void get() {
//                System.out.println("get");
//            }
//        }.get();//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println(Thread.currentThread().getName()+",run");
//            }
//        }).start();}
}

什么是lambda表达式
lambda好处:简化匿名内部类的使用
lambda+方法引入 使代码变得更加精简

Java中的lambda表达式的规范,必须是函数接口
函数接口的定义:在该接口中只能存在一个抽象方法,该接口称为函数接口
在函数接口中可以定义object类中方法
在函数接口中可以使用默认或静态方法
@FunctionalInterface标识该接口为函数接口

如何定义函数接口

package com.xd;@FunctionalInterface
public interface MyFunctionalInterface {void get();default void defaultAdd(){System.out.println("默认方法");}/*** object父类中的方法可以在函数接口中重写* @return*/String toString();}

无参

package com.xd;@FunctionalInterface
public interface NoArgsInterface {void get();
}
package com.xd;public class Test03 {public static void main(String[] args) {//1.使用匿名内部类调用new NoArgsInterface() {@Overridepublic void get() {System.out.println("get");}}.get();NoArgsInterface noArgsInterface = ()->{System.out.println("使用lambda表达式调用方法");};noArgsInterface.get();}
}

有参

package com.xd;@FunctionalInterface
public interface HasArgsInterface {String get(int i, int j);}
package com.xd;public class Test04 {public static void main(String[] args) {//1.使用匿名内部类调用HasArgsInterface hasArgsInterface = new HasArgsInterface() {@Overridepublic String get(int i, int j) {return i + "---" + j;}};System.out.println(hasArgsInterface.get(1,2));//2.使用lambda调用有参函数方法HasArgsInterface hasArgsInterface1 = (i,j) -> {return i + "---" + j;};System.out.println(hasArgsInterface1.get(1,1));}
}

精简代码

package com.xd;public class Test05 {public static void main(String[] args) {NoArgsInterface noArgsInterface = () -> {System.out.println("方法");};noArgsInterface.get();// 精简代码((NoArgsInterface) () -> {System.out.println("方法");}).get();// 使用lambda 方法体中只有一条语句的情况下,可以不需要写{} 也可以不需要写returnNoArgsInterface noArgsInterface1 = () -> System.out.println("方法");HasArgsInterface hasArgsInterface = (i, j) -> {return i + "-" + j;};HasArgsInterface hasArgsInterface1 = (i, j) -> i + "-" + j;String s = ((HasArgsInterface)(i, j) -> i + "-" + j).get(1,2);System.out.println(s);}
}

实现集合遍历

package com.xd;import java.util.ArrayList;
import java.util.function.Consumer;public class Test06 {public static void main(String[] args) {ArrayList<String> strings = new ArrayList<>();strings.add("1");strings.add("2");strings.add("3");strings.forEach(new Consumer<String>() {@Overridepublic void accept(String s) {System.out.println(s);}});strings.forEach(s -> {System.out.println(s);});}
}

实现集合排序

package com.xd.entity;public class UserEntity {private String userName;public UserEntity(String userName, int age) {this.userName = userName;this.age = age;}private int age;public String getUserName() {return userName;}public int getAge() {return age;}@Overridepublic String toString() {return "UserEntity{" +"userName='" + userName + '\'' +", age=" + age +'}';}public boolean equals(Object obj){if (obj instanceof UserEntity)return userName.equals(((UserEntity) obj).userName) && age == (((UserEntity) obj).age);else return false;}public int hashCode() {return userName.hashCode();}}
package com.xd;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.Comparator;public class Test07 {public static void main(String[] args) {ArrayList<UserEntity> userLists = new ArrayList<>();userLists.add(new UserEntity("a",1));userLists.add(new UserEntity("b",4));userLists.add(new UserEntity("c",3));userLists.sort(new Comparator<UserEntity>() {@Overridepublic int compare(UserEntity o1, UserEntity o2) {return o1.getAge()- o2.getAge();}});userLists.sort((o1,o2)->o1.getAge() - o2.getAge());userLists.forEach((t)->{System.out.println(t.toString());});}
}

实现线程调用

package com.xd;public class Test08 {public static void main(String[] args) {new Thread(new Runnable() {@Overridepublic void run() {System.out.println("获取到线程名称:"+Thread.currentThread().getName()+",子线程");}}).start();new Thread(() -> System.out.println("获取到线程名称:"+Thread.currentThread().getName()+",子线程")).start();        }
}

3.Stream流

Stream:非常方便精简的形式遍历集合实现 过滤、排序等。

strean将list转为set

package com.xd.stream;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;public class Test01 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("a",1));userEntities.add(new UserEntity("b",4));userEntities.add(new UserEntity("c",3));userEntities.add(new UserEntity("c",3));userEntities.add(new UserEntity("d",3));userEntities.add(new UserEntity("d",1));/*** 创建stream方式两种* 1.串行流stream() 单线程* 2.并行流parallelStream() 多线程* 并行流parallelStream() 比 串行流stream()效率要高*/Stream<UserEntity> stream = userEntities.stream();// 转换成set集合Set<UserEntity> setUserList = stream.collect(Collectors.toSet());setUserList.forEach(userEntity -> {System.out.println(userEntity.toString());});}
}

set集合底层是依赖于map集合实现防重复key map集合底层基于equals比较防重复,equals结合hashcode

stream将list转为map

package com.xd.stream;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;public class Test02 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("a", 1));userEntities.add(new UserEntity("b", 4));userEntities.add(new UserEntity("c", 3));
//        userEntities.add(new UserEntity("c", 3));userEntities.add(new UserEntity("d", 3));
//        userEntities.add(new UserEntity("d", 1));//list集合只有元素值 key list转换成map集合的情况下 指定key--user对象 username属性 value user对象/*** map<String(userName),UserEntity>*/Stream<UserEntity> stream = userEntities.stream();/*** new Function<UserEntity(List集合中的类型), String(key map)>*/Map<String, UserEntity> collect = stream.collect(Collectors.toMap(new Function<UserEntity, String>() {@Overridepublic String apply(UserEntity userEntity) {return userEntity.getUserName();}}, new Function<UserEntity, UserEntity>() {@Overridepublic UserEntity apply(UserEntity userEntity) {return userEntity;}}));collect.forEach(new BiConsumer<String, UserEntity>() {@Overridepublic void accept(String s, UserEntity userEntity) {System.out.println(s + "," + userEntity);}});}
}

stream计算求和

package com.xd;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;public class Test10 {public static void main(String[] args) {
//        Stream<Integer> integerStream = Stream.of(10, 9, 6, 33);
//        Optional<Integer> reduce = integerStream.reduce(new BinaryOperator<Integer>() {
//            @Override
//            public Integer apply(Integer a1, Integer a2) {
//                return a1 + a2;
//            }
//        });
//        System.out.println(reduce.get());ArrayList<UserEntity> userLists = new ArrayList<>();userLists.add(new UserEntity("a",1));userLists.add(new UserEntity("b",4));userLists.add(new UserEntity("c",3));Stream<UserEntity> stream = userLists.stream();Optional<UserEntity> sum = stream.reduce(new BinaryOperator<UserEntity>() {@Overridepublic UserEntity apply(UserEntity user1, UserEntity user2) {UserEntity userEntity = new UserEntity("sum", user1.getAge() + user2.getAge());return userEntity;}});System.out.println(sum.get().getAge());}
}

stream查找最大和最小

package com.xd;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;public class Test11 {public static void main(String[] args) {ArrayList<UserEntity> userLists = new ArrayList<>();userLists.add(new UserEntity("a",1));userLists.add(new UserEntity("b",4));userLists.add(new UserEntity("c",3));Stream<UserEntity> stream = userLists.stream();Optional<UserEntity> max = stream.max(new Comparator<UserEntity>() {@Overridepublic int compare(UserEntity o1, UserEntity o2) {return o1.getAge() - o2.getAge();}});System.out.println(max.get());}
}

stream中的match

package com.xd;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.function.Predicate;
import java.util.stream.Stream;public class Test12 {public static void main(String[] args) {ArrayList<UserEntity> userLists = new ArrayList<>();userLists.add(new UserEntity("a",1));userLists.add(new UserEntity("b",4));userLists.add(new UserEntity("c",3));Stream<UserEntity> stream = userLists.stream();boolean b = stream.anyMatch(new Predicate<UserEntity>() {@Overridepublic boolean test(UserEntity userEntity) {return "a".equals(userEntity.getUserName());}});System.out.println(b);}
}

stream过滤器

package com.xd;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.function.Predicate;
import java.util.stream.Stream;public class Test13 {public static void main(String[] args) {ArrayList<UserEntity> userLists = new ArrayList<>();userLists.add(new UserEntity("a",100));userLists.add(new UserEntity("a",200));userLists.add(new UserEntity("a",1));userLists.add(new UserEntity("b",4));userLists.add(new UserEntity("c",3));Stream<UserEntity> stream = userLists.stream();stream.filter(new Predicate<UserEntity>() {@Overridepublic boolean test(UserEntity userEntity) {return "a".equals(userEntity.getUserName())&&userEntity.getAge() > 18;}}).forEach(userEntity -> System.out.println(userEntity));}
}

stream实现limit

package com.xd;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.stream.Stream;public class Test14 {public static void main(String[] args) {ArrayList<UserEntity> userLists = new ArrayList<>();userLists.add(new UserEntity("a",100));userLists.add(new UserEntity("a",200));userLists.add(new UserEntity("a",1));userLists.add(new UserEntity("b",4));userLists.add(new UserEntity("c",3));Stream<UserEntity> stream = userLists.stream();// 相当于mysql limit(0,2)//stream.limit(2).forEach((userEntity -> System.out.println(userEntity)));stream.skip(2).limit(3).forEach((userEntity -> System.out.println(userEntity)));}
}

stream实现数据排序

package com.xd.stream;import com.xd.entity.UserEntity;import java.util.ArrayList;
import java.util.Comparator;
import java.util.stream.Stream;public class Test03 {public static void main(String[] args) {ArrayList<UserEntity> userLists = new ArrayList<>();userLists.add(new UserEntity("a",100));userLists.add(new UserEntity("a",200));userLists.add(new UserEntity("a",1));userLists.add(new UserEntity("b",4));userLists.add(new UserEntity("c",3));Stream<UserEntity> stream = userLists.stream();stream.sorted(new Comparator<UserEntity>() {@Overridepublic int compare(UserEntity o1, UserEntity o2) {return o1.getAge()- o2.getAge();}}).forEach(userEntity -> System.out.println(userEntity.toString()));}
}

文章转载自:

http://915htlIQ.ygwyt.cn
http://qxkmzZ5h.ygwyt.cn
http://seKB1Ka1.ygwyt.cn
http://DpxW3kJk.ygwyt.cn
http://bOv2umag.ygwyt.cn
http://QFTcBIxI.ygwyt.cn
http://DF7j9rFb.ygwyt.cn
http://oWlkappS.ygwyt.cn
http://hlkhiLNj.ygwyt.cn
http://mqyO7kMX.ygwyt.cn
http://WLnv6a0d.ygwyt.cn
http://1tizJp06.ygwyt.cn
http://76Y79B2M.ygwyt.cn
http://fTeaWEHq.ygwyt.cn
http://mgkehLz0.ygwyt.cn
http://RnXtW9s3.ygwyt.cn
http://4yTdDa6U.ygwyt.cn
http://umcpjr4m.ygwyt.cn
http://bG4vkdq0.ygwyt.cn
http://mOhCqOTN.ygwyt.cn
http://d1j8YUyR.ygwyt.cn
http://3JMT9IBM.ygwyt.cn
http://RRPiHzHS.ygwyt.cn
http://eibm5c8U.ygwyt.cn
http://gaOL7o9E.ygwyt.cn
http://5LZP3dnz.ygwyt.cn
http://wgizjjtz.ygwyt.cn
http://ZMxQfNrC.ygwyt.cn
http://EZ6eYHZV.ygwyt.cn
http://m0Valk9i.ygwyt.cn
http://www.dtcms.com/wzjs/618023.html

相关文章:

  • 网站的流量是怎么回事多语种网站营销
  • 怎样做娱乐网站做任务赚话费的网站
  • 泉州茶叶网站建设山东住房与城乡建设部网站
  • 西部数码网站管理系统阿里云支持wordpress
  • 河北工程大学网站开发成本购物网站开发的背景和意义
  • 网站切换语言怎么做的杭州装饰装潢公司10大品牌
  • 怎样申请建立自助网站网站关键词怎样修改
  • 收录快的门户网站wordpress 模板 教程
  • 手机终端网站网站建设与维护兼职
  • 政务服务网站 建设方案python基础教程电子版书籍
  • 网站建设四网合一中铁三局招聘信息2021
  • 仿门户网站多功能js相册画廊源码wordpress建立网站吗
  • 昆明制作企业网站的公司html5手机商城网站模板
  • 做企业网站需要哪些vivo官网网站服务
  • 上海平台网站建设平台做外贸营销网站销售咋样
  • 福州哪家专业网站设计制作最好alt网站标签怎么做
  • 可以在线做c语言的网站宁波企业建站程序
  • 自建网站怎么做二级页跳转电商网站统计怎么做
  • 淄博公司网站建设设计教程网站推荐
  • 学网站建设软件开发品牌vi设计手册案例欣赏
  • 英文版网站制作3分钟宣传片制作费用
  • 网站售价企业营销网站模板
  • 如何设置网站公司动态网站建设关闭窗口代码
  • 企业宣传片文案大全苏州seo网站推广
  • 天猫设计师服务平台成都优化教育百度推广
  • 南沙滩做网站公司做淘宝站外推广网站
  • 美容行业手机网站模版网站建设的公司合肥
  • isp网站接入做哪些业务公司标志logo设计免费
  • 网站排名应该怎么做15秒创意广告短片
  • 网站黑白了中小型网站建设服务