Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
awa-w6d4-junit-testproject
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Eric Arndt
awa-w6d4-junit-testproject
Commits
87363416
Commit
87363416
authored
Apr 09, 2021
by
earndt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[W6D4] (ArndtED) Adds classes
parent
0f6a3ee3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
0 deletions
+111
-0
ImprovedRandomSpliterator.java
...ct/src/main/java/com/nisum/ImprovedRandomSpliterator.java
+61
-0
WhoIsTheBest.java
testproject/src/main/java/com/nisum/WhoIsTheBest.java
+50
-0
No files found.
testproject/src/main/java/com/nisum/ImprovedRandomSpliterator.java
0 → 100644
View file @
87363416
package
com
.
nisum
;
//all credit to https://stackoverflow.com/users/2229438/grzegorz-piwowarek
//https://stackoverflow.com/questions/45981964/how-to-get-a-random-element-from-a-list-with-stream-api/45982130
import
java.util.List
;
import
java.util.Random
;
import
java.util.Spliterator
;
import
java.util.function.Consumer
;
import
java.util.function.Supplier
;
import
java.util.stream.Stream
;
import
java.util.stream.StreamSupport
;
public
class
ImprovedRandomSpliterator
<
T
>
implements
Spliterator
<
T
>
{
private
final
Random
random
;
private
final
T
[]
source
;
private
int
size
;
ImprovedRandomSpliterator
(
List
<
T
>
source
,
Supplier
<?
extends
Random
>
random
)
{
if
(
source
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"RandomSpliterator can't be initialized with an empty collection"
);
}
this
.
source
=
(
T
[])
source
.
toArray
();
this
.
random
=
random
.
get
();
this
.
size
=
this
.
source
.
length
;
}
@Override
public
boolean
tryAdvance
(
Consumer
<?
super
T
>
action
)
{
if
(
size
>
0
)
{
int
nextIdx
=
random
.
nextInt
(
size
);
int
lastIdx
=
size
-
1
;
action
.
accept
(
source
[
nextIdx
]);
source
[
nextIdx
]
=
source
[
lastIdx
];
source
[
lastIdx
]
=
null
;
// let object be GCed
size
--;
return
true
;
}
else
{
return
false
;
}
}
@Override
public
Spliterator
<
T
>
trySplit
()
{
return
null
;
}
@Override
public
long
estimateSize
()
{
return
source
.
length
;
}
@Override
public
int
characteristics
()
{
return
SIZED
;
}
}
testproject/src/main/java/com/nisum/WhoIsTheBest.java
0 → 100644
View file @
87363416
package
com
.
nisum
;
import
java.util.*
;
import
java.util.stream.Collector
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
java.util.stream.StreamSupport
;
import
static
java
.
util
.
stream
.
Collectors
.
toCollection
;
public
class
WhoIsTheBest
{
private
List
<
String
>
theGreats
;
private
List
<
String
>
theNotSoGreats
;
public
WhoIsTheBest
(){
this
.
theGreats
=
Arrays
.
asList
(
"Alex P"
,
"Alex S"
,
"Ben"
,
"Chef"
,
"Chris"
,
"Darrick"
,
"Deep"
,
"Joe"
,
"John"
,
"Julius"
,
"Kevin"
,
"Khai"
,
"Kyle"
,
"Nikitha"
,
"Philippe"
,
"Shanelle"
,
"Sumaiyya"
,
"Vishal"
);
this
.
theNotSoGreats
=
Arrays
.
asList
(
"Eric"
);
}
public
List
<
String
>
getTheGreats
()
{
return
theGreats
;
}
public
List
<
String
>
getTheNotSoGreats
()
{
return
theNotSoGreats
;
}
public
Optional
<
String
>
gimmeAGreat
(){
return
theGreats
.
stream
().
collect
(
toShuffledStream
()).
findAny
();
}
public
Optional
<
String
>
gimmeANotSoGreat
(){
return
theNotSoGreats
.
stream
().
findAny
();
}
public
static
<
T
>
Collector
<
T
,
?,
Stream
<
T
>>
toShuffledStream
()
{
return
Collectors
.
collectingAndThen
(
toCollection
(
ArrayList:
:
new
),
list
->
!
list
.
isEmpty
()
?
StreamSupport
.
stream
(
new
ImprovedRandomSpliterator
<>(
list
,
Random:
:
new
),
false
)
:
Stream
.
empty
());
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment