I am new to Java and stuck while reading these particular lines of code:
private static void checkPrice(final List<String> prices){
List<String> inputCountryCodes = IntStream.range(0, prices.size())
.filter(index -> (index % 2 == 0))
.mapToObj(prices::get)
.collect(Collectors.toList());
List<String> distinctInputCountryCodes = inputCountryCodes.stream().distinct()
.collect(Collectors.toList());
}
Can anyone please explain the para1 and para2 of the code above what these particular lines of code do here?
CodePudding user response:
IntStream.range(0, prices.size())
Its generating a integer stream from 0 to size(excluding) of prize List.
So lets say your prices.size()
equals 3 then InputStream will emmit 0, 1, 2
filter(index -> (index % 2 == 0))
Its intermediate operation will then filter those numbers produced by InputStream
basis on that number will be moded with 2. i.e to check if the number is even.
If it even it will be passed further or else thrown out
mapToObj(prices::get)
mapToObj It's taking that filtered number (even number) and use it to get String type object from prices
.
So above code is like
mapToObj(num -> {
String item = prices.get(num);
return item;
})
collect(Collectors.toList());
collect the result from mapToObj
into List<String>
So Your List<String> inputCountryCodes
will contain item from prices
whose index is even number.
inputCountryCodes.stream().distinct()
This will create a Stream out of your inputCountryCodes
which is of type List<String>
then only take out distinct items from it.
Example if your inputCountryCodes
has item1
, item2
, item2
, item1
it will result into item1
,item2
collect(Collectors.toList());
Then collect result from distinct to List<String>
So finally your List<String> distinctInputCountryCodes
will contains items from prices
a) whose index is an even number
b) And items are distinct
CodePudding user response:
Let's go through this one by one :
List<String> inputCountryCodes = IntStream.range(0, prices.size()).filter(index -> (index % 2 == 0))
.mapToObj(prices::get)
.collect(Collectors.toList());
IntStream
is Java interface for stream specifically for int type (for stream, google stream or Java.util.streamrange(0, prices.size)
isstatic IntStream range(int startInclusive, int endExclusive)
- ^ Creates an intstream from 0,
prices.size() - 1
- ^ Creates an intstream from 0,
prices.size()
is length of pricesfilter
is same in english as is in Java. It returns a stream consisting of the elements of this stream that match the given predicate (removes all those that don't match)index -> index % 2 == 0
is lambda fn that checks whether elements are divisable by 2 or not (%2 == 0 is the check)mapToObj
eturns an object-valued Stream consisting of the results of applying the given function.collect
is a mutable reduce functionCollectors.toList()
is class extending Object andtoList
is one of functions.
That's para 1
List<String> distinctInputCountryCodes = inputCountryCodes.stream().distinct()
.collect(Collectors.toList());
inputCountryCodes
the string list obtained abovestream()
turns list into a streamdistinct
is name sake it gives distinct elements in a stream.collect(Collectors.toList())
Already explained
Hope that explains it
CodePudding user response:
The first one is something like the following:
List<String> result = new ArrayList<>();
for(int i = 0; i < prices.size(); i ) {
if (i % 2 == 0)
result.add(prices.get(i));
The second one iterates through the list and removes the duplicates by boxing the int
to Integer
and using a LinkedHashSet
object under the hood.
Interpretation
You can think range(int a, int b)
as a create a for loop beginning from a to b
filter(Predicate predicate)
applies the given predicate on the stream and filtering accordingly. This is similar to if(condition) -> the iteration shall pass
collect(Collectors.toList())
collects the iterations, which made it this far in a list object.
distinct()
removes the duplicates by first boxing the primitives, passing the objects to a set object and returning the distinct result.