Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef919c8

Browse files
committed
first commit
0 parents  commitef919c8

17 files changed

+770
-0
lines changed

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.idea
3+
*.iml
4+
out
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.winterbe.java8;
2+
3+
importjava.lang.annotation.ElementType;
4+
importjava.lang.annotation.Repeatable;
5+
importjava.lang.annotation.Retention;
6+
importjava.lang.annotation.RetentionPolicy;
7+
importjava.lang.annotation.Target;
8+
9+
/**
10+
* @author Benjamin Winterberg
11+
*/
12+
publicclassAnnotations1 {
13+
14+
@Target({ElementType.TYPE_PARAMETER,ElementType.TYPE_USE})
15+
@interfaceMyAnnotation {
16+
17+
}
18+
19+
@Retention(RetentionPolicy.RUNTIME)
20+
@interfaceHints {
21+
Hint[]value();
22+
}
23+
24+
@Repeatable(Hints.class)
25+
@Retention(RetentionPolicy.RUNTIME)
26+
@interfaceHint {
27+
Stringvalue();
28+
}
29+
30+
@Hint("hint1")
31+
@Hint("hint2")
32+
classPerson {
33+
34+
}
35+
36+
publicstaticvoidmain(String[]args) {
37+
Hinthint =Person.class.getAnnotation(Hint.class);
38+
System.out.println(hint);// null
39+
40+
Hintshints1 =Person.class.getAnnotation(Hints.class);
41+
System.out.println(hints1.value().length);// 2
42+
43+
Hint[]hints2 =Person.class.getAnnotationsByType(Hint.class);
44+
System.out.println(hints2.length);// 2
45+
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.winterbe.java8;
2+
3+
importjava.util.UUID;
4+
importjava.util.concurrent.ConcurrentHashMap;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
publicclassConcurrency1 {
10+
11+
publicstaticvoidmain(String[]args) {
12+
ConcurrentHashMap<Integer,UUID>concurrentHashMap =newConcurrentHashMap<>();
13+
14+
for (inti =0;i <100;i++) {
15+
concurrentHashMap.put(i,UUID.randomUUID());
16+
}
17+
18+
intthreshold =1;
19+
20+
concurrentHashMap.forEachValue(threshold,System.out::println);
21+
22+
concurrentHashMap.forEach((id,uuid) -> {
23+
if (id %10 ==0) {
24+
System.out.println(String.format("%s: %s",id,uuid));
25+
}
26+
});
27+
28+
UUIDsearchResult =concurrentHashMap.search(threshold, (id,uuid) -> {
29+
if (String.valueOf(uuid).startsWith(String.valueOf(id))) {
30+
returnuuid;
31+
}
32+
returnnull;
33+
});
34+
35+
System.out.println(searchResult);
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packagecom.winterbe.java8;
2+
3+
/**
4+
* @author Benjamin Winterberg
5+
*/
6+
publicclassInterface1 {
7+
8+
interfaceFormula {
9+
doublecalculate(inta);
10+
11+
defaultdoublesqrt(inta) {
12+
returnMath.sqrt(positive(a));
13+
}
14+
15+
staticintpositive(inta) {
16+
returna >0 ?a :0;
17+
}
18+
}
19+
20+
publicstaticvoidmain(String[]args) {
21+
Formulaformula1 =newFormula() {
22+
@Override
23+
publicdoublecalculate(inta) {
24+
returnsqrt(a *100);
25+
}
26+
};
27+
28+
formula1.calculate(100);// 100.0
29+
formula1.sqrt(-23);// 0.0
30+
Formula.positive(-4);// 0.0
31+
32+
// Formula formula2 = (a) -> sqrt( a * 100);
33+
}
34+
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.winterbe.java8;
2+
3+
importjava.util.Arrays;
4+
importjava.util.Collections;
5+
importjava.util.Comparator;
6+
importjava.util.List;
7+
8+
/**
9+
* @author Benjamin Winterberg
10+
*/
11+
publicclassLambda1 {
12+
13+
publicstaticvoidmain(String[]args) {
14+
List<String>names =Arrays.asList("peter","anna","mike","xenia");
15+
16+
Collections.sort(names,newComparator<String>() {
17+
@Override
18+
publicintcompare(Stringa,Stringb) {
19+
returnb.compareTo(a);
20+
}
21+
});
22+
23+
Collections.sort(names, (Stringa,Stringb) -> {
24+
returnb.compareTo(a);
25+
});
26+
27+
Collections.sort(names, (Stringa,Stringb) ->b.compareTo(a));
28+
29+
Collections.sort(names, (a,b) ->b.compareTo(a));
30+
31+
System.out.println(names);
32+
}
33+
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.winterbe.java8;
2+
3+
/**
4+
* @author Benjamin Winterberg
5+
*/
6+
publicclassLambda2 {
7+
8+
@FunctionalInterface
9+
publicstaticinterfaceConverter<F,T> {
10+
Tconvert(Ffrom);
11+
}
12+
13+
staticclassSomething {
14+
StringstartsWith(Strings) {
15+
returnString.valueOf(s.charAt(0));
16+
}
17+
}
18+
19+
interfacePersonFactory<PextendsPerson> {
20+
Pcreate(StringfirstName,StringlastName);
21+
}
22+
23+
publicstaticvoidmain(String[]args) {
24+
Converter<String,Integer>integerConverter1 = (from) ->Integer.valueOf(from);
25+
Integerconverted1 =integerConverter1.convert("123");
26+
System.out.println(converted1);// result: 123
27+
28+
29+
// method reference
30+
31+
Converter<String,Integer>integerConverter2 =Integer::valueOf;
32+
Integerconverted2 =integerConverter2.convert("123");
33+
System.out.println(converted2);// result: 123
34+
35+
36+
Somethingsomething =newSomething();
37+
38+
Converter<String,String>stringConverter =something::startsWith;
39+
Stringconverted3 =stringConverter.convert("Java");
40+
System.out.println(converted3);// result J
41+
42+
// constructor reference
43+
44+
PersonFactory<Person>personFactory =Person::new;
45+
Personperson =personFactory.create("Peter","Parker");
46+
}
47+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
packagecom.winterbe.java8;
2+
3+
importjava.util.Comparator;
4+
importjava.util.Objects;
5+
importjava.util.UUID;
6+
importjava.util.concurrent.Callable;
7+
importjava.util.function.Consumer;
8+
importjava.util.function.Function;
9+
importjava.util.function.Predicate;
10+
importjava.util.function.Supplier;
11+
12+
/**
13+
* Common standard functions from the Java API.
14+
*
15+
* @author Benjamin Winterberg
16+
*/
17+
publicclassLambda3 {
18+
19+
@FunctionalInterface
20+
interfaceFun {
21+
voidfoo();
22+
}
23+
24+
publicstaticvoidmain(String[]args)throwsException {
25+
26+
// Predicates
27+
28+
Predicate<String>predicate = (s) ->s.length() >0;
29+
30+
predicate.test("foo");// true
31+
predicate.negate().test("foo");// false
32+
33+
Predicate<Boolean>nonNull =Objects::nonNull;
34+
Predicate<Boolean>isNull =Objects::isNull;
35+
36+
Predicate<String>isEmpty =String::isEmpty;
37+
Predicate<String>isNotEmpty =isEmpty.negate();
38+
39+
40+
// Functions
41+
42+
Function<String,Integer>toInteger =Integer::valueOf;
43+
Function<String,String>backToString =toInteger.andThen(String::valueOf);
44+
45+
backToString.apply("123");// "123"
46+
47+
48+
// Suppliers
49+
50+
Supplier<Person>personSupplier =Person::new;
51+
personSupplier.get();// new Person
52+
53+
54+
// Consumers
55+
56+
Consumer<Person>greeter = (p) ->System.out.println("Hello, " +p.firstName);
57+
greeter.accept(newPerson("Luke","Skywalker"));
58+
59+
60+
61+
// Comparators
62+
63+
Comparator<Person>comparator = (p1,p2) ->p1.firstName.compareTo(p2.firstName);
64+
65+
Personp1 =newPerson("John","Doe");
66+
Personp2 =newPerson("Alice","Wonderland");
67+
68+
comparator.compare(p1,p2);// > 0
69+
comparator.reversed().compare(p1,p2);// < 0
70+
71+
72+
// Runnables
73+
74+
Runnablerunnable = () ->System.out.println(UUID.randomUUID());
75+
runnable.run();
76+
77+
78+
// Callables
79+
80+
Callable<UUID>callable =UUID::randomUUID;
81+
callable.call();
82+
}
83+
84+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.winterbe.java8;
2+
3+
/**
4+
* @author Benjamin Winterberg
5+
*/
6+
publicclassLambda4 {
7+
8+
staticintouterStaticNum;
9+
10+
intouterNum;
11+
12+
voidtestScopes() {
13+
intnum =1;
14+
15+
Lambda2.Converter<Integer,String>stringConverter =
16+
(from) ->String.valueOf(from +num);
17+
18+
Stringconvert =stringConverter.convert(2);
19+
System.out.println(convert);// 3
20+
21+
Lambda2.Converter<Integer,String>stringConverter2 = (from) -> {
22+
outerNum =13;
23+
returnString.valueOf(from);
24+
};
25+
26+
String[]array =newString[1];
27+
Lambda2.Converter<Integer,String>stringConverter3 = (from) -> {
28+
array[0] ="Hi there";
29+
returnString.valueOf(from);
30+
};
31+
32+
stringConverter3.convert(23);
33+
34+
System.out.println(array[0]);
35+
}
36+
37+
publicstaticvoidmain(String[]args) {
38+
newLambda4().testScopes();
39+
}
40+
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packagecom.winterbe.java8;
2+
3+
importjava.time.DayOfWeek;
4+
importjava.time.LocalDate;
5+
importjava.time.Month;
6+
importjava.time.format.DateTimeFormatter;
7+
importjava.time.format.FormatStyle;
8+
importjava.time.temporal.ChronoUnit;
9+
importjava.util.Locale;
10+
11+
/**
12+
* @author Benjamin Winterberg
13+
*/
14+
publicclassLocalDate1 {
15+
16+
publicstaticvoidmain(String[]args) {
17+
LocalDatetoday =LocalDate.now();
18+
LocalDatetomorrow =today.plus(1,ChronoUnit.DAYS);
19+
LocalDateyesterday =tomorrow.minusDays(2);
20+
21+
System.out.println(today);
22+
System.out.println(tomorrow);
23+
System.out.println(yesterday);
24+
25+
LocalDateindependenceDay =LocalDate.of(2014,Month.JULY,4);
26+
DayOfWeekdayOfWeek =independenceDay.getDayOfWeek();
27+
System.out.println(dayOfWeek);// FRIDAY
28+
29+
DateTimeFormattergermanFormatter =
30+
DateTimeFormatter
31+
.ofLocalizedDate(FormatStyle.MEDIUM)
32+
.withLocale(Locale.GERMAN);
33+
34+
LocalDatexmas =LocalDate.parse("24.12.2014",germanFormatter);
35+
System.out.println(xmas);// 2014-12-24
36+
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp