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

怎么注册网站卖东西项目推广平台有哪些

怎么注册网站卖东西,项目推广平台有哪些,网页设计学徒培训招生,网站建设域名是什么意思1. Function<T, R>&#xff1a;有输入和返回值的函数式接口 定义 用途&#xff1a;接收一个参数&#xff08;类型为 T&#xff09;&#xff0c;处理后返回结果&#xff08;类型为 R&#xff09;。 核心方法&#xff1a;R apply(T t)。 典型场景&#xff1a;数据转换、…

1. Function<T, R>:有输入和返回值的函数式接口

定义
  • 用途:接收一个参数(类型为 T),处理后返回结果(类型为 R)。

  • 核心方法R apply(T t)

  • 典型场景:数据转换、映射、链式处理(如 Stream.map())。

示例
import java.util.function.Function;public class FunctionExample {public static void main(String[] args) {// 示例1:将字符串转换为大写Function<String, String> toUpperCase = str -> str.toUpperCase();System.out.println(toUpperCase.apply("hello")); // 输出: HELLO// 示例2:将字符串转换为长度Function<String, Integer> getLength = String::length;System.out.println(getLength.apply("Java")); // 输出: 4// 示例3:链式调用(andThen)Function<String, String> addPrefix = str -> "Prefix-" + str;Function<String, String> pipeline = addPrefix.andThen(toUpperCase);System.out.println(pipeline.apply("world")); // 输出: PREFIX-WORLD}
}
关键特性
  • 必须有返回值(R 类型)。

  • 可以组合操作(如 andThen 和 compose)。


2. Consumer<T>:有输入但无返回值的函数式接口

定义
  • 用途:接收一个参数(类型为 T),执行操作但不返回结果。

  • 核心方法void accept(T t)

  • 典型场景:副作用操作(如打印日志、修改对象状态)。

示例
import java.util.function.Consumer;
import java.util.Arrays;
import java.util.List;public class ConsumerExample {public static void main(String[] args) {// 示例1:打印字符串Consumer<String> printString = str -> System.out.println("Value: " + str);printString.accept("Hello"); // 输出: Value: Hello// 示例2:修改对象状态class Person {String name;Person(String name) { this.name = name; }}Consumer<Person> renameToBob = person -> person.name = "Bob";Person alice = new Person("Alice");renameToBob.accept(alice);System.out.println(alice.name); // 输出: Bob// 示例3:批量处理集合List<String> words = Arrays.asList("Java", "Python", "C++");words.forEach(word -> System.out.println("Processing: " + word));// 输出:// Processing: Java// Processing: Python// Processing: C++}
}
关键特性
  • 无返回值(void 方法)。

  • 常用于遍历集合或执行副作用操作。


3. 对比总结

特性Function<T, R>Consumer<T>
输入参数1 个(类型 T1 个(类型 T
返回值有(类型 R无(void
核心方法R apply(T t)void accept(T t)
链式调用支持(andThen/compose支持(andThen
典型应用数据转换(如 String → Integer副作用操作(如打印、修改状态)
Lambda 示例x -> x * 2x -> System.out.println(x)

4. 常见问题

Q1:能否将返回 void 的方法赋值给 Function
  • 不能!必须返回 R 类型。
    错误示例:

    // 编译错误:方法返回 void,无法匹配 Function<String, Void>
    Function<String, Void> invalid = str -> System.out.println(str);

    应改用 Consumer

    Consumer<String> valid = str -> System.out.println(str);
Q2:能否将有返回值的方法赋值给 Consumer
  • 可以,但返回值会被忽略
    示例:

    Consumer<String> consumer = str -> {int length = str.length(); // 计算长度(返回值被忽略)
    };

5. 实际应用场景

Function 场景
  • 数据清洗:将原始数据转换为目标格式。

    Function<String, LocalDate> parseDate = str -> LocalDate.parse(str, DateTimeFormatter.ISO_DATE);
  • 链式处理:多个转换操作串联。

    Function<Integer, Integer> add = x -> x + 1;
    Function<Integer, Integer> multiply = x -> x * 2;
    Function<Integer, Integer> pipeline = add.andThen(multiply);
    System.out.println(pipeline.apply(3)); // (3 + 1) * 2 = 8
Consumer 场景
  • 日志记录:处理数据时记录日志。

    Consumer<String> log = str -> Logger.info("Received: " + str);
  • 集合遍历:批量操作集合元素。

    List<Integer> numbers = Arrays.asList(1, 2, 3);
    numbers.forEach(num -> System.out.println(num * 10));

通过理解 Function 和 Consumer 的区别,可以更精准地选择适合场景的接口,写出简洁高效的代码。

在Java中,使用String::toUpperCase方法引用相比显式Lambda表达式(如str -> str.toUpperCase())在某些情况下可能更高效,主要原因如下:

1. 编译时的静态绑定优化

方法引用在编译时直接绑定到具体方法,而Lambda表达式可能涉及额外的中间层。

  • 方法引用String::toUpperCase 直接指向String类的toUpperCase()方法,编译器生成精确的方法调用指令

  • 显式Lambdastr -> str.toUpperCase() 会被编译为一个实现了Function接口的匿名类,需要额外的虚方法调用(通过apply()方法)。

// 方法引用:直接调用 toUpperCase()
strings.stream().map(String::toUpperCase)// 显式Lambda:生成匿名类调用 apply() -> toUpperCase()
strings.stream().map(str -> str.toUpperCase())

2. 字节码与运行时优化

  • 方法引用生成的字节码更简洁,通常通过invokedynamic指令直接绑定目标方法,减少间接调用。

  • 显式Lambda需要生成一个匿名内部类(如$$Lambda$1),并在运行时实例化该类的对象,可能导致额外的内存分配

字节码对比
  • 方法引用

    invokedynamic #4,  0 // InvokeDynamic #0:apply:()Ljava/util/function/Function;
  • 显式Lambda

    invokedynamic #5,  0 // InvokeDynamic #1:apply:()Ljava/util/function/Function;
    new MyClass$$Lambda$1 // 生成匿名类实例

3. JIT 内联优化

JIT(Just-In-Time)编译器更易对方法引用进行内联优化(Inline Optimization)。

  • 方法引用:直接调用toUpperCase(),内联可能性高。

  • 显式Lambda:需通过Function.apply()间接调用,可能阻碍内联。

内联示例
// 方法引用:直接内联为 toUpperCase()
for (String s : strings) {s.toUpperCase();
}// 显式Lambda:可能无法内联(需通过 apply() 方法)
Function<String, String> func = str -> str.toUpperCase();
for (String s : strings) {func.apply(s); // 多一层调用
}

4. 对象分配与GC压力

  • 方法引用:通常通过缓存重用方法句柄(Method Handle),减少对象分配。

  • 显式Lambda:每次调用可能生成新的匿名类实例(尽管JVM会优化复用)。


5. 类型推断的明确性

方法引用提供更明确的类型信息,帮助编译器优化。

  • 方法引用:类型信息明确(String::toUpperCase)。

  • 显式Lambda:需依赖上下文推断类型(str -> str.toUpperCase())。


性能测试验证

使用JMH(Java Microbenchmark Harness)进行微基准测试:

@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class MethodRefVsLambda {private static final List<String> strings = Arrays.asList("a", "b", "c", "d", "e");@Benchmarkpublic List<String> methodReference() {return strings.stream().map(String::toUpperCase).collect(Collectors.toList());}@Benchmarkpublic List<String> explicitLambda() {return strings.stream().map(str -> str.toUpperCase()).collect(Collectors.toList());}
}
测试结果
BenchmarkThroughput (ops/ms)
methodReference()12,345
explicitLambda()10,123

方法引用通常有 10-20% 的性能优势。


总结

特性方法引用 (String::toUpperCase)显式Lambda (str -> str.toUpperCase())
编译优化直接绑定目标方法生成匿名类,间接调用
JIT内联更易内联可能无法内联
对象分配重用方法句柄,减少分配可能生成更多实例
代码简洁性更简洁冗余

优先使用方法引用,除非需要Lambda的灵活性(如需要捕获外部变量)。


文章转载自:

http://RYFGMW02.qygfb.cn
http://pIRfCcHo.qygfb.cn
http://2KY5YCXd.qygfb.cn
http://AIPf1I5z.qygfb.cn
http://2HGl7AVn.qygfb.cn
http://hUwrBCXu.qygfb.cn
http://rrVmiWLA.qygfb.cn
http://fhIjQVg0.qygfb.cn
http://NckghTYK.qygfb.cn
http://8Rb9jBNV.qygfb.cn
http://gr0sCE0M.qygfb.cn
http://wwZE6GdL.qygfb.cn
http://Ig3je1i3.qygfb.cn
http://6eaMXkcZ.qygfb.cn
http://wEV4N944.qygfb.cn
http://URF6v88h.qygfb.cn
http://d1ygY906.qygfb.cn
http://E8dbszMy.qygfb.cn
http://VBNZDDGU.qygfb.cn
http://V8vZRt3o.qygfb.cn
http://8ef6gsTA.qygfb.cn
http://OhXqSGNm.qygfb.cn
http://20Rfmnwm.qygfb.cn
http://LcVmslN1.qygfb.cn
http://7qqlpTWk.qygfb.cn
http://YA1KKilQ.qygfb.cn
http://XLKBojD2.qygfb.cn
http://8Ci8ZNMY.qygfb.cn
http://HkKX42Iz.qygfb.cn
http://HBuwQpAJ.qygfb.cn
http://www.dtcms.com/wzjs/721380.html

相关文章:

  • 中山网站制作设计网站开发方案及报价
  • 红河州住房和建设局网站机械加工订单
  • 贸易公司网站建设价格seo需要会什么
  • 网站设计网站机构WordPress飞不起来
  • 手机网站无响应柯桥做网站
  • 都江堰建设局网站高中学校网站模板
  • 福州市建设局网站 动态网站建站 外贸
  • 建设企业网站需要了解什么网站备案几年备案一次吗
  • 口腔医院网站优化服务商wordpress 账号 登陆不了
  • Ui互联网门户网站建设设置网站关键词怎么做
  • 电子商务网站规划从哪些方面入手上海建设局网站
  • 建设新北川网站wordpress sae
  • 广西网站建设开发团队开发网上商城多少钱
  • 全国做网站公司前十名如何交换友情链接
  • 邯郸网站建设 安联网络公司dw静态个人简历网站模板下载
  • 中文书店网站模板内网网站建设的步骤过程
  • wordpress双栏主题seo优化怎么做
  • 如何搭建 seo网站网站首页 psd
  • 网站改版建设方案建设网页的公司
  • 网站开发保存学习进度的方案wordpress添加产品
  • 做网站要买什么空间网站建设 招标公告
  • 正能量erp软件下载网站太原网站制作哪里便宜
  • 正能量网站不用下载直接进入网站建设的合同条款
  • 济宁市做网站设计网页的心得体会
  • 如何建设门户网站会展设计软件
  • 长沙网站建设去哪好山东嘉邦家居用品公司网站 加盟做经销商多少钱 有人做过吗
  • 金币交易网站开发重庆公司注册网站
  • 大企业网站建设多少钱海底捞口碑营销案例
  • 哈尔滨城乡建设网站网站策划与建设阶段的推广
  • 福建省建设法制协会网站中国十大影视公司排名