用java8写
时间: 2023-08-04 09:13:48 浏览: 47
很好,以下是使用Java 8编写的示例代码:
```java
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
// 创建一个整数流
IntStream intStream = IntStream.rangeClosed(1, 10);
// 通过map操作将每个元素平方
IntStream squaredStream = intStream.map(x -> x * x);
// 通过reduce操作将所有元素相加
int sum = squaredStream.reduce(0, (x, y) -> x + y);
System.out.println("1到10的平方和为: " + sum);
}
}
```
这个程序将生成从1到10的整数流,通过map操作将每个元素平方,然后通过reduce操作将所有元素相加,最终输出结果。
相关问题
使用java8写一个流排序
当使用Java 8编写流排序时,可以使用Stream API的`sorted()`方法来对流中的元素进行排序。下面是一个示例代码:
```java
import java.util.Arrays;
import java.util.List;
public class StreamSortExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 3, 8, 2, 1, 9, 4, 7, 6);
List<Integer> sortedNumbers = numbers.stream()
.sorted()
.collect(Collectors.toList());
System.out.println("排序前:" + numbers);
System.out.println("排序后:" + sortedNumbers);
}
}
```
在此示例中,我们首先创建了一个包含整数的列表`numbers`。然后,我们使用流的`sorted()`方法对这些数字进行排序,并使用`collect()`方法将排序后的结果收集到一个新的列表`sortedNumbers`中。最后,我们分别输出原始列表和排序后的列表。
运行以上代码将得到以下输出:
```
排序前:[5, 3, 8, 2, 1, 9, 4, 7, 6]
排序后:[1, 2, 3, 4, 5, 6, 7, 8, 9]
```
这样就实现了使用Java 8对流进行排序的功能。
用java8写一个文件流工具类
以下是一个简单的文件流工具类,可以实现读取和写入文件:
```java
import java.io.*;
public class FileStreamUtil {
/**
* 读取文本文件
* @param filePath 文件路径
* @return 文件内容字符串
*/
public static String readTextFile(String filePath) {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append(System.lineSeparator());
}
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
/**
* 写入文本文件
* @param filePath 文件路径
* @param content 文件内容字符串
*/
public static void writeTextFile(String filePath, String content) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取二进制文件
* @param filePath 文件路径
* @return 文件内容字节数组
*/
public static byte[] readBinaryFile(String filePath) {
try (InputStream is = new FileInputStream(filePath)) {
byte[] content = new byte[is.available()];
is.read(content);
return content;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 写入二进制文件
* @param filePath 文件路径
* @param content 文件内容字节数组
*/
public static void writeBinaryFile(String filePath, byte[] content) {
try (OutputStream os = new FileOutputStream(filePath)) {
os.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
使用示例:
```java
// 读取文本文件
String text = FileStreamUtil.readTextFile("test.txt");
System.out.println(text);
// 写入文本文件
FileStreamUtil.writeTextFile("test.txt", "Hello, world!");
// 读取二进制文件
byte[] binaryData = FileStreamUtil.readBinaryFile("test.bin");
System.out.println(Arrays.toString(binaryData));
// 写入二进制文件
FileStreamUtil.writeBinaryFile("test.bin", new byte[]{0x12, 0x34, 0x56});
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)