Skip to content

Commit 3296585

Browse files
committed
未知更新
1 parent c7cc7be commit 3296585

File tree

6 files changed

+528
-0
lines changed

6 files changed

+528
-0
lines changed

java8-demo/pom.xml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.coderdream</groupId>
8+
<artifactId>java8-demo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>java8-demo</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.11</version>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.apache.commons</groupId>
30+
<artifactId>commons-collections4</artifactId>
31+
<version>4.4</version>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework</groupId>
36+
<artifactId>spring-beans</artifactId>
37+
<version>5.3.9</version>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
44+
<plugins>
45+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
46+
<plugin>
47+
<artifactId>maven-clean-plugin</artifactId>
48+
<version>3.1.0</version>
49+
</plugin>
50+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
51+
<plugin>
52+
<artifactId>maven-resources-plugin</artifactId>
53+
<version>3.0.2</version>
54+
</plugin>
55+
<plugin>
56+
<artifactId>maven-compiler-plugin</artifactId>
57+
<version>3.8.0</version>
58+
</plugin>
59+
<plugin>
60+
<artifactId>maven-surefire-plugin</artifactId>
61+
<version>2.22.1</version>
62+
</plugin>
63+
<plugin>
64+
<artifactId>maven-jar-plugin</artifactId>
65+
<version>3.0.2</version>
66+
</plugin>
67+
<plugin>
68+
<artifactId>maven-install-plugin</artifactId>
69+
<version>2.5.2</version>
70+
</plugin>
71+
<plugin>
72+
<artifactId>maven-deploy-plugin</artifactId>
73+
<version>2.8.2</version>
74+
</plugin>
75+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
76+
<plugin>
77+
<artifactId>maven-site-plugin</artifactId>
78+
<version>3.7.1</version>
79+
</plugin>
80+
<plugin>
81+
<artifactId>maven-project-info-reports-plugin</artifactId>
82+
<version>3.0.0</version>
83+
</plugin>
84+
</plugins>
85+
</pluginManagement>
86+
<plugins>
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-compiler-plugin</artifactId>
90+
<configuration>
91+
<source>8</source>
92+
<target>8</target>
93+
</configuration>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderdream;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.coderdream;
2+
3+
/**
4+
* @author :CoderDream
5+
* @date :Created in 2021/10/31 14:39
6+
* @description:
7+
* @modified By:CoderDream
8+
* @version: $
9+
*/
10+
public class FindNumber {
11+
12+
public static void main(String[] args) {
13+
int input = 1000;
14+
15+
// System.out.println("守形数有:");
16+
// // 守形数
17+
// int n = 0;
18+
// int c = 0;
19+
// int i = 0;
20+
// for (i = 2; i < 1000; i++) {
21+
// n = i * i;
22+
// // 如果循环的数小于10,则守形数小于100,那守形数取余10后等于循环数
23+
// if( i < 10 && n % 10 == i) {
24+
// System.out.println(i);
25+
// c++;
26+
// }
27+
// else if (i < 100 && n % 100 == i) {
28+
// System.out.println(i);
29+
// c++;
30+
// }
31+
// else if (i < 1000 && n % 1000 == i) {
32+
// System.out.println(i);
33+
// c++;
34+
// }
35+
// }
36+
// System.out.println("共计: " +c);
37+
38+
39+
for (int i = 10; i < 1000; i++) {
40+
double d = Double.valueOf(i + "");
41+
int c = 1;
42+
do {
43+
c += 1;
44+
d = d / 10;
45+
System.out.println(d);
46+
} while (d < 1);
47+
System.out.println("值:" + i + " ;位数:" + c);
48+
}
49+
}
50+
51+
52+
// #include<stdio.h>
53+
// int main() {
54+
// int i,n,c;
55+
// c=0;
56+
// printf("守形数有:");
57+
// for(i=2; i<1000; i++) {
58+
// n=i*i;
59+
//
60+
// if(i<10&&n%10==i) {
61+
// printf("%d ",i);
62+
// c++;
63+
// }
64+
//
65+
// else if(i<100&&n%100==i) {
66+
// printf("%d ",i);
67+
// c++;
68+
// } else if(i<1000&&n%1000==i) {
69+
// printf("%d ",i);
70+
// c++;
71+
// }
72+
// }
73+
// printf("\n");
74+
// printf("共计%d个\n",c);
75+
// return 0;
76+
// }
77+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package com.coderdream;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.IntSummaryStatistics;
6+
import java.util.List;
7+
import java.util.Random;
8+
import java.util.stream.Collectors;
9+
import java.util.Map;
10+
11+
/**
12+
* @author :CoderDream
13+
* @date :Created in 2021/10/24 13:23
14+
* @description:
15+
* @modified By:CoderDream
16+
* @version: $
17+
*/
18+
public class StreamDemo {
19+
public static void main(String args[]) {
20+
System.out.println("使用 Java 7: ");
21+
22+
// 计算空字符串
23+
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
24+
System.out.println("列表: " + strings); // 列表: [abc, , bc, efg, abcd, , jkl]
25+
long count = getCountEmptyStringUsingJava7(strings);
26+
System.out.println("空字符数量为: " + count); // 空字符数量为: 2
27+
28+
count = getCountLength3UsingJava7(strings);
29+
System.out.println("字符串长度为 3 的数量为: " + count); // 字符串长度为 3 的数量为: 3
30+
31+
// 删除空字符串
32+
List<String> filtered = deleteEmptyStringsUsingJava7(strings);
33+
System.out.println("筛选后的列表: " + filtered); // 筛选后的列表: [abc, bc, efg, abcd, jkl]
34+
35+
// 删除空字符串,并使用逗号把它们合并起来
36+
String mergedString = getMergedStringUsingJava7(strings, ", ");
37+
System.out.println("合并字符串: " + mergedString); // 合并字符串: abc, bc, efg, abcd, jkl
38+
39+
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
40+
41+
// 获取列表元素平方数
42+
List<Integer> squaresList = getSquares(numbers);
43+
System.out.println("平方数列表: " + squaresList); // 平方数列表: [9, 4, 49, 25]
44+
45+
List<Integer> integers = Arrays.asList(1, 2, 13, 4, 15, 6, 17, 8, 19);
46+
47+
System.out.println("列表: " + integers); // 列表: [1, 2, 13, 4, 15, 6, 17, 8, 19]
48+
System.out.println("列表中最大的数 : " + getMax(integers)); // 列表中最大的数 : 19
49+
System.out.println("列表中最小的数 : " + getMin(integers)); // 列表中最小的数 : 1
50+
System.out.println("所有数之和 : " + getSum(integers)); // 所有数之和 : 85
51+
System.out.println("平均数 : " + getAverage(integers)); // 平均数 : 9
52+
System.out.println("随机数: "); // 随机数:
53+
54+
// 输出10个随机数
55+
Random random = new Random();
56+
57+
for (int i = 0; i < 10; i++) {
58+
System.out.println(random.nextInt());
59+
}
60+
61+
System.out.println("使用 Java 8: ");
62+
System.out.println("列表: " + strings); // 列表: [abc, , bc, efg, abcd, , jkl]
63+
64+
count = strings.stream().filter(string -> string.isEmpty()).count();
65+
System.out.println("空字符串数量为: " + count); // 空字符串数量为: 2
66+
67+
count = strings.stream().filter(string -> string.length() == 3).count();
68+
System.out.println("字符串长度为 3 的数量为: " + count); // 字符串长度为 3 的数量为: 3
69+
70+
filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
71+
System.out.println("筛选后的列表: " + filtered); // 筛选后的列表: [abc, bc, efg, abcd, jkl]
72+
73+
mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
74+
System.out.println("合并字符串: " + mergedString); // 合并字符串: abc, bc, efg, abcd, jkl
75+
76+
squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
77+
System.out.println("Squares List: " + squaresList); // Squares List: [9, 4, 49, 25]
78+
System.out.println("列表: " + integers); // 列表: [1, 2, 13, 4, 15, 6, 17, 8, 19]
79+
80+
IntSummaryStatistics stats = integers.stream().mapToInt((x) -> x).summaryStatistics();
81+
82+
System.out.println("列表中最大的数 : " + stats.getMax()); // 列表中最大的数 : 19
83+
System.out.println("列表中最小的数 : " + stats.getMin()); // 列表中最小的数 : 1
84+
System.out.println("所有数之和 : " + stats.getSum()); // 所有数之和 : 85
85+
System.out.println("平均数 : " + stats.getAverage()); // 平均数 : 9.444444444444445
86+
System.out.println("随机数: "); // 随机数:
87+
88+
random.ints().limit(10).sorted().forEach(System.out::println);
89+
90+
// 并行处理
91+
count = strings.parallelStream().filter(string -> string.isEmpty()).count();
92+
System.out.println("空字符串的数量为: " + count); // 空字符串的数量为: 2
93+
}
94+
95+
private static int getCountEmptyStringUsingJava7(List<String> strings) {
96+
int count = 0;
97+
98+
for (String string : strings) {
99+
100+
if (string.isEmpty()) {
101+
count++;
102+
}
103+
}
104+
return count;
105+
}
106+
107+
private static int getCountLength3UsingJava7(List<String> strings) {
108+
int count = 0;
109+
110+
for (String string : strings) {
111+
112+
if (string.length() == 3) {
113+
count++;
114+
}
115+
}
116+
return count;
117+
}
118+
119+
private static List<String> deleteEmptyStringsUsingJava7(List<String> strings) {
120+
List<String> filteredList = new ArrayList<String>();
121+
122+
for (String string : strings) {
123+
124+
if (!string.isEmpty()) {
125+
filteredList.add(string);
126+
}
127+
}
128+
return filteredList;
129+
}
130+
131+
private static String getMergedStringUsingJava7(List<String> strings, String separator) {
132+
StringBuilder stringBuilder = new StringBuilder();
133+
134+
for (String string : strings) {
135+
136+
if (!string.isEmpty()) {
137+
stringBuilder.append(string);
138+
stringBuilder.append(separator);
139+
}
140+
}
141+
String mergedString = stringBuilder.toString();
142+
return mergedString.substring(0, mergedString.length() - 2);
143+
}
144+
145+
private static List<Integer> getSquares(List<Integer> numbers) {
146+
List<Integer> squaresList = new ArrayList<Integer>();
147+
148+
for (Integer number : numbers) {
149+
Integer square = new Integer(number.intValue() * number.intValue());
150+
151+
if (!squaresList.contains(square)) {
152+
squaresList.add(square);
153+
}
154+
}
155+
return squaresList;
156+
}
157+
158+
private static int getMax(List<Integer> numbers) {
159+
int max = numbers.get(0);
160+
161+
for (int i = 1; i < numbers.size(); i++) {
162+
163+
Integer number = numbers.get(i);
164+
165+
if (number.intValue() > max) {
166+
max = number.intValue();
167+
}
168+
}
169+
return max;
170+
}
171+
172+
private static int getMin(List<Integer> numbers) {
173+
int min = numbers.get(0);
174+
175+
for (int i = 1; i < numbers.size(); i++) {
176+
Integer number = numbers.get(i);
177+
178+
if (number.intValue() < min) {
179+
min = number.intValue();
180+
}
181+
}
182+
return min;
183+
}
184+
185+
private static int getSum(List numbers) {
186+
int sum = (int) (numbers.get(0));
187+
188+
for (int i = 1; i < numbers.size(); i++) {
189+
sum += (int) numbers.get(i);
190+
}
191+
return sum;
192+
}
193+
194+
private static int getAverage(List<Integer> numbers) {
195+
return getSum(numbers) / numbers.size();
196+
}
197+
}

0 commit comments

Comments
 (0)