FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/generics/PrimitiveGenericTest.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
generics
/
PrimitiveGenericTest.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (40 loc) · 1.21 KB
Breadcrumbs
OnJava8-Examples
/
generics
/
PrimitiveGenericTest.java
Copy path
File metadata and controls
42 lines (40 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// generics/PrimitiveGenericTest.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import
onjava
.*;
import
java
.
util
.*;
import
java
.
util
.
function
.*;
// Fill an array using a generator:
interface
FillArray
{
static
<
T
>
T
[]
fill
(
T
[]
a
,
Supplier
<
T
>
gen
) {
Arrays
.
setAll
(
a
,
n
->
gen
.
get
());
return
a
;
}
static
int
[]
fill
(
int
[]
a
,
IntSupplier
gen
) {
Arrays
.
setAll
(
a
,
n
->
gen
.
getAsInt
());
return
a
;
}
static
long
[]
fill
(
long
[]
a
,
LongSupplier
gen
) {
Arrays
.
setAll
(
a
,
n
->
gen
.
getAsLong
());
return
a
;
}
static
double
[]
fill
(
double
[]
a
,
DoubleSupplier
gen
) {
Arrays
.
setAll
(
a
,
n
->
gen
.
getAsDouble
());
return
a
;
}
}
public
class
PrimitiveGenericTest
{
public
static
void
main
(
String
[]
args
) {
String
[]
strings
=
FillArray
.
fill
(
new
String
[
5
],
new
Rand
.
String
(
9
));
System
.
out
.
println
(
Arrays
.
toString
(
strings
));
int
[]
integers
=
FillArray
.
fill
(
new
int
[
9
],
new
Rand
.
Pint
());
System
.
out
.
println
(
Arrays
.
toString
(
integers
));
}
}
/* Output:
[btpenpccu, xszgvgmei, nneeloztd, vewcippcy, gpoalkljl]
[635, 8737, 3941, 4720, 6177, 8479, 6656, 3768, 4948]
*/
Back
|
FazBrowse Home
|
New Git URL