Java8 lamada表达式的简单使用
来源:互联网 发布:filter 过滤css和js 编辑:程序博客网 时间:2024/06/08 16:07
首先是配置。。。
在project的build.gradle中添加
classpath 'me.tatarka:gradle-retrolambda:3.2.0'
在module的build.gradle中添加
//设置JDK1.8 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } jackOptions { enabled true }
位置如下:
哈哈哈,接下来就开始我们的lamda之旅啦。。。
MainActivity.class
public class MainActivity extends AppCompatActivity { private static final String TAG = "cwy"; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(view -> onButton1()); findViewById(R.id.button2).setOnClickListener(view -> onButton2()); findViewById(R.id.button3).setOnClickListener(view -> onButton3()); findViewById(R.id.button4).setOnClickListener(view -> onButton4()); findViewById(R.id.button5).setOnClickListener(view -> onButton5()); findViewById(R.id.button6).setOnClickListener(view -> onButton6()); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } public void onButton1() { Runnable runnable = () -> Toast.makeText(this, "My name is cwy", Toast.LENGTH_SHORT).show(); runnable.run(); } /* * 平方数 * */ public void onButton2() { List<Integer> nums = Arrays.asList(1, 2, 3, 4); List<Integer> squareNums = nums.stream() .map(n -> n * n) .collect(Collectors.toList()); squareNums.forEach(p -> System.out.println(p)); } /* * 转换大写 * * */ public void onButton3() { List<String> wordList = Arrays.asList("a", "b", "c"); List<String> output = wordList.stream() .map(String::toUpperCase) .collect(Collectors.toList()); output.forEach(p -> System.out.println(p)); } /* *一对多的关系 * * */ public void onButton4() { Stream<List<Integer>> inputStream = Stream.of( Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6) ); Stream<Integer> outputStream = inputStream.flatMap((childList) -> childList.stream()); outputStream.forEach(p -> System.out.println(p)); } /* * * * */ public void onButton5() { // 筛选偶数 Integer[] sixNums = {1, 2, 3, 4, 5, 6}; Integer[] evens = Stream.of(sixNums) .filter(n -> n%2 == 0) .toArray(Integer[]::new); Stream<Integer> stream = Arrays.stream(evens); stream.forEach(p -> System.out.println(p)); } /* * * * */ public void onButton6() { // 字符串连接,concat = "ABCD" String concat = Stream.of("A", "B", "C", "D") .reduce("", String::concat); System.out.println(concat); // 求最小值,minValue = -3.0 double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0) .reduce(Double.MAX_VALUE, Double::min); System.out.println(minValue); // 求和,sumValue = 10, 有起始值 int sumValue = Stream.of(1, 2, 3, 4) .reduce(0, Integer::sum); System.out.println(sumValue); // 过滤,字符串连接,concat = "ace" concat = Stream.of("a", "B", "c", "D", "e", "F") . filter(x -> x.compareTo("Z") > 0) . reduce("", String::concat); System.out.println(concat); }}
activity_main.xml中只有六个Button按钮
<Button android:text="Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/button1" /> <Button android:text="Button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:id="@+id/button2" /> <Button android:text="Button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_alignRight="@+id/button1" android:layout_alignEnd="@+id/button1" android:layout_marginTop="49dp" android:id="@+id/button3" /> <Button android:text="Button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button3" android:layout_alignLeft="@+id/button2" android:layout_alignStart="@+id/button2" android:id="@+id/button4" /> <Button android:text="Button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignRight="@+id/button3" android:layout_alignEnd="@+id/button3" android:id="@+id/button5" /> <Button android:text="Button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button5" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:id="@+id/button6" />
阅读全文
0 0
- Java8 lamada表达式的简单使用
- [java杂记]java8的lamada 表达式
- Android使用Lamada表达式
- Java8 lamada
- java lamada表达式使用总结
- lamada 表达式
- AS配置启用Lamada表达式的环境
- java8 lambda表达式的简单介绍
- c# lamada表达式
- Python-lamada表达式
- java8中Lambda表达式的使用
- 1.lamada教学-java8新特性
- Lamada 表达式(C# 3.0)
- JDK8 lamada表达式学习笔记
- Java8 Lambda简单使用
- java8的lambda表达式
- lamada
- 紧随时代的步伐--Java8之Lambda表达式的使用
- 仅自己电脑识别不了自己U盘,却能识别其他U盘
- android点击链接打开应用对应界面
- 使用Swiper4插件问题
- 求最小环
- ios 禁止将页面中的一连串数字识别为电话号码
- Java8 lamada表达式的简单使用
- SpringMVC个人零碎总结
- c语言经典笔试题03-最小公倍数
- 分块算法详解
- 575. Distribute Candies
- 2017.11.2第十周训练周总结
- 一个java局域网聊天的程序
- 如何使用RedisTemplate访问Redis数据结构
- Python循环参数的使用