java-8

Why java 8 introduces iterable.forEach() loop even though it has for each?

℡╲_俬逩灬. 提交于 2020-08-27 06:51:26
问题 I'm not able to understand why java 8 has forEach loop and taking the Consumer functional interface as parameter. Even though we can do the same task using traditional for each loop without creating any extra overhead to create a class implements that from Consumer and implements a method and then pass this as reference to the forEach(). Although there is lambda expression to make it short. Q1- why iterable.forEach()? Q2. Where to use it? Q3. which one is faster traditional for each of Java 8

Java 8 adding days with no time segments

雨燕双飞 提交于 2020-08-26 08:21:25
问题 Java 8 here. I'm trying to take the current Date (now), add one day to it, and get a new Date instance representing tomorrow that has no time component to it (only year, month & day). My best attempt: Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, 1); Date tomorrow = calendar.getTime(); But when I print tomorrow , it contains a time component (example: Sat Jun 02 14:04:59 EDT 2018 ) whereas I just want it to contain zeroed-out values for hour/month/minute/etc

Is there a Java function similar to C++'s PBDS order_of_key?

跟風遠走 提交于 2020-08-26 05:52:52
问题 In C++ we can use __gnu_pbds i.e. Policy based data structure. See the following code. #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> PBDS; int main() { PBDS st; st.insert(1); st.insert(3); st.insert(4); st.insert(10); st.insert(15); for(int i=0;i<st.size();i++) { cout<<i<<" "<< *st.find_by_order(i) <<endl

Is there a Java function similar to C++'s PBDS order_of_key?

ぐ巨炮叔叔 提交于 2020-08-26 05:52:31
问题 In C++ we can use __gnu_pbds i.e. Policy based data structure. See the following code. #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> PBDS; int main() { PBDS st; st.insert(1); st.insert(3); st.insert(4); st.insert(10); st.insert(15); for(int i=0;i<st.size();i++) { cout<<i<<" "<< *st.find_by_order(i) <<endl

How to configure RetryTemplate only for Http status code 500?

元气小坏坏 提交于 2020-08-25 04:04:28
问题 I'm using spring-retry (with java 8 lambda) to retry the failed REST calls. I want to retry only for those call which returned 500 error. But I'm not able to configure retrytemplate bean for that. Currently the bean is simple as follows: @Bean("restRetryTemplate") public RetryTemplate retryTemplate() { Map<Class<? extends Throwable>, Boolean> retryableExceptions= Collections.singletonMap(HttpServerErrorException.class, Boolean.TRUE); SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(3,

java.time.ZonedDateTime.parse and iso8601?

心已入冬 提交于 2020-08-24 06:30:14
问题 Why does the JDK8 DateTime library seem to not parse valid iso8601 date time strings? It chokes on time zone offsets expressed like "+01" instead of "+01:00" This works: java.time.ZonedDateTime.parse("2015-08-18T00:00+01:00") This throws a parse exception: java.time.ZonedDateTime.parse("2015-08-18T00:00+01") From the iso8601 wikipedia page: The offset from UTC is appended to the time in the same way that 'Z' was above, in the form ±[hh]:[mm], ±[hh][mm], or ±[hh]. So if the time being

Generic BiFunction that accepts generic wildcard

徘徊边缘 提交于 2020-08-24 04:22:06
问题 I have Enum and want to have a generic function that could accept any class that implements a maker interface. public interface IComponentDataExporter { // Marker Interface } public class ComponentsDataExporter implements IComponentDataExporter { public ComponentTeaser populateFeatured(ComponentTeaserConfiguration componentConfig) { return null; } } public class BussinessComponentsDataExporter implements IComponentDataExporter { // methods } public enum ComponentTypeEnum { FEATURED