java-8

Getting first element and returning after apply a function

末鹿安然 提交于 2021-02-04 16:18:25
问题 I am new in Java 8, I want to make a method that gets the first element that matched and returning after apply a function public void test() { List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API"); String str = features .stream() .filter(s -> "Lambdas".equals(s)) .findFirst() .ifPresent(this::toLowerCase); } private String toLowerCase (String str) { return str.toLowerCase(); } but I got an Incompatible types error. 回答1: Optional.ifPresent

From for loop to Java 8 Stream example

若如初见. 提交于 2021-02-04 16:14:12
问题 I would like a simple example for Java 8 Streams to understand it. I have this code that returns a free taxi. I would like to replace this for loop with equivalent code that uses Java 8 streams : private List<Taxi> taxis = new ArrayList<Taxi>(); Taxi scheduleTaxi(){ for (Taxi taxi : taxis) { if (taxi.isFree()) { return taxi; } } return null; } I iterate over a list of taxis , and evaluate if taxi respects the condition. If the condition applies, I stop the loop and return taxi . Any

Are annotations on a type parameter accessible in runtime?

时光毁灭记忆、已成空白 提交于 2021-02-04 14:10:42
问题 Java 8 allows things like: public List<@NonNull String> names; But is there a way to access this annotation in runtime or is it available only to compiler plugins? There's new Method#getAnnotatedReturnType that provides access to annotations on the return type, so I was hoping ParameterizedType would now have something like getActualAnnotatedTypeArguments that would do the same for generic type arguments, but it doesn't exist... 回答1: The new API continues the tradition of requiring lots of

Most appropriate SQL and Java data types for storing date and time [duplicate]

我与影子孤独终老i 提交于 2021-02-04 13:24:52
问题 This question already has answers here : Handling MySQL datetimes and timestamps in Java (3 answers) Closed 4 years ago . Apologies in advance for the somewhat broad question. What are the most appropriate MySQL and Java data types for handling date and times with the following format: yyyy.MM.dd hh:mm:ss I need to be able to convert a String representation of the date & time into the given Date Format as well as then store that date&time in the database. I then need to do the reverse and

Merge map of arrays with duplicate keys

夙愿已清 提交于 2021-02-04 13:12:27
问题 I have two maps of arrays. Map<String, List<String>> map1 = new HashMap<>(); Map<String, List<String>> map2 = new HashMap<>(); I want to merge them in one new map. If a key exists in both maps, in that case, I should merge arrays. For example: map1.put("k1", Arrays.asList("a0", "a1")); map1.put("k2", Arrays.asList("b0", "b1")); map2.put("k2", Arrays.asList("z1", "z2")); // Expected output is Map 3: {k1=[a0, a1], k2=[b0, b1, z1, z2]} I tried to do that with streams Map<String, List<String>>

How to collect two fields of an object into the same list?

柔情痞子 提交于 2021-02-04 11:21:08
问题 I have an Object of goods, which has two properties: firstCategoryId and secondCategoryId . I have a list of goods, and I want to get all category Ids (including both firstCategoryId and secondCategoryId). My current solution is: List<Integer> categoryIdList = goodsList.stream().map(g->g.getFirstCategoryId()).collect(toList()); categoryIdList.addAll(goodsList.stream().map(g->g.getSecondCategoryId()).collect(toList())); Is there a more convenient manner I could get all the categoryIds in a

How to create a nested Map using Collectors.groupingBy?

我是研究僧i 提交于 2021-02-04 10:28:11
问题 I have a list of class say ProductDto public class ProductDto { private String Id; private String status; private Booker booker; private String category; private String type; } I want to have a Map as below:- Map<String,Map<String,Map<String,Booker>> The properties are to be mapped as below: Map<status,Map<category,Map<type,Booker> I know one level of grouping could be done easily without any hassles using Collectors.groupingBy . I tried to use this for nested level but it failed for me when

RuleBaseLoader loadFromInputStream drools 2.5 compilation error on Java8 and weblogic12c

时光毁灭记忆、已成空白 提交于 2021-02-04 08:36:06
问题 We are facing compilation error while compiling Rule Base from DB on server using drools-io-2.5.jar. RuleBaseLoader.loadFromInputStream(stream); stream is XML input stream picked from database. This code is working with JAVA6 and weblogic 11g but we start getting error when we deployed on weblogic 12c on JAVA8. Can someone please give a direction, how to resolve this. Error: <09-09-2020 18:12:47.235> <DEBUG> <ejbframework> <system> <RulesHelper> <subscriber-type-rules Checking rule base:

filtering a stream against items in another list

不打扰是莪最后的温柔 提交于 2021-02-04 07:49:48
问题 trying to filter a stream against data within a different list: It works, but I use a for loop in the middle of the stream. I cannot find any information of how to convert the for loop to a stream. I could just .stream() the selction.getItems() than .forEach() and have a new .stream() of DATA.accounts, but that is poor code as it would have to restream on every .forEach. y=1; DATA.accounts.stream() .flatMap(estimate -> estimate.getElements().stream()) .filter( ele-> { // different list; for

Difference between anyMatch and findAny in java 8

ε祈祈猫儿з 提交于 2021-02-04 03:20:34
问题 I have an Array and want to perform some matching on it's element. I came to know that it could be done in two ways in java 8 : String[] alphabet = new String[]{"A", "B", "C"}; anyMatch : Arrays.stream(alphabet).anyMatch("A"::equalsIgnoreCase); findAny : Arrays.stream(alphabet).filter("a"::equalsIgnoreCase) .findAny().orElse("No match found")); As I can understand both are doing the same work. However, I could not found which one to prefer? Could someone please make it clear what is the