This question already has answers here:
Syntax error on token(s), misplaced construct(s) for lambda expression
(2 answers)
Closed 2 years ago.
I'm not getting why JAVA 1.8 is not supporting the lambda expression in stream().filter() / allMatch() / anyMatch()
for example :
ERROR snippet from eclipse IDE
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args) {
// Creating a list of Integers
List<Integer> list = Arrays.asList(3, 4, 6, 12, 20);
// Check if all elements of stream
// are divisible by 3 or not using
// Stream allMatch(Predicate predicate)
boolean answer = list.stream().allMatch(n-> n % 3 ==0);
// Displaying the result
System.out.println(answer);
}
}
I'm getting errors in eclipse as "Syntax error on token "-", -- expected" and red lines below 'n'.
Please help me to understand and resolve this issue.
#NOTE: I'm using eclipse 3.8, JAVA 8 (1.8.0_271)
Your code seem to compile and run in https://www.tutorialspoint.com/compile_java_online.php
Which runs Java 1.8.0_141.
Check if the target run time in eclipse is set to java 8.
You can use Target JRE in Eclipse as an example.
Related
Set language Level to 8 error is received when map.forEach is used with Lambda Expression in Java 11
I have upgraded my JDK to 11 from 8, my existing project is showing compilation errors with lambda expressions for Map. Below is code
public void validateHeaders(Map<String, String> headers) {
headers.forEach((key, value) -> Assert.assertEquals(value, response.getHeader(key)));
}
This question already has answers here:
Create array of regex matches
(6 answers)
Closed 4 years ago.
I need a Groovy/Java function to search for groups in a string based on regular expression
Ex:
function("([\w-]+)-([\w.-]+)\.([\w.-]+)" ,"commons-collections-3.2.2.jar" )
should return a list ["commons-collections" , "3.2.2" , "jar"]
Python can do this by
>> import re
>> re.search("([\w-]+)-([\w.-]+)\.([\w.-]+)" ,"commons-collections-3.2.2.jar" )
>> print(result.groups())
output is ("commons-collections" , "3.2.2" , "jar")
It is a simple and basic task in groovy. Any way I hope this answer will help you.
"commons-collections-3.2.2.jar".findAll(/([\w-]+)-([\w.-]+)\.([\w.-]+)/) {
println it
}
This will produce the output :
[commons-collections-3.2.2.jar, commons-collections, 3.2.2, jar]
Update :
As #tim_yates mentioned in comment,
println "commons-collections-3.2.2.jar".findAll(/([\w-]+)-([\w.-]+)\.([\w.-]+)/) { it.tail() }
This provides better output than above and also more specific to the task.
Output:
[[commons-collections, 3.2.2, jar]]
This question already has answers here:
Groovy equivalent of Java 8 :: (double colon) operator
(2 answers)
Closed 4 years ago.
In java I can write
Arrays.asList("test ").stream().map(String::trim);
If I try this in groovy
Arrays.asList("test ").stream().map(String.&trim)
I get
Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.trim() is applicable for argument types: (String) values: [test ]
Possible solutions: trim(), wait(), grep(), wait(long), split(java.lang.String), print(java.io.PrintWriter)
What is the correct syntax or do I have to resort to
Arrays.asList("test ").stream().map({ x -> x.trim() })
?
you can use .map { it.trim() } too, but otherwise, groovy does not have method reference working like java one
I want to convert a stream into a list. To do so I have tried this code :
try(Stream<String> stream = Files.lines(Paths.get(args[0]))) {
List<String> lines = stream.collect(Collectors.toList());
}
But then the compiler tell me that :
Type mismatch: cannot convert from List<Object> to List<String>
Also I've tried this way :
try(Stream<String> stream = Files.lines(Paths.get(args[0]))) {
List<String> lines = Stream.of(stream).collect(Collectors.toList());
}
But then I have this error :
This static method of interface Stream can only be accessed as Stream.of
which happen every time, whenever I use the Stream.of methods.
Can someone help me out, please?
It's seems that you use java compiler language level lower than 8.
Try compiling from command line with
javac -source 8
Also check your language level project setting in IDE.
For example, output from IDEA 2016 hides info about used language level, but provide info about javac version
Information:Using javac 1.8.0_101 to compile java sources
Information:java: Errors occurred while compiling module 'demo_04'
Information:8/1/16 2:13 PM - Compilation completed with 1 error and 0 warnings in 1s 403ms
/tmp/1/src/JavaStreamExample.java
Error:(16, 49) java: incompatible types: java.util.List<java.lang.Object> cannot be converted to java.util.List<java.lang.String>
I'm trying to calculate high-PMI terms for a particular token in pylucene. A colleague gave me some Java code that works, but I am having trouble translating it into Python. In particular, the code relies on a custom collector. Here's the initial query code:
def __init__(self, some_token, searcher, analyzer):
super(PMICalculator, self).__init__()
self.searcher = searcher
self.analyzer = analyzer
self.escaped_token = QueryParser.escape(some_token)
self.query = QueryParser("text",self.analyzer).parse(self.escaped_token)
self.term_count_collector = TermCountCollector(searcher)
self.searcher.search(self.query, self.term_count_collector)
self.terms = self.term_count_collector.getTerms()
Here's the Term Count Collector class: http://snipt.org/vgGi8
This code breaks at self.searcher.search with the error:
File <filename>, line 26, in __init__
self.searcher.search(self.query, self.term_count_collector)
lucene.JavaError: org.apache.jcc.PythonException: collect() takes exactly 2 arguments (3 given)
TypeError: collect() takes exactly 2 arguments (3 given)
Java stacktrace:
org.apache.jcc.PythonException: collect() takes exactly 2 arguments (3 given)
TypeError: collect() takes exactly 2 arguments (3 given)
at org.apache.pylucene.search.PythonHitCollector.collect(Native Method)
at org.apache.lucene.search.HitCollectorWrapper.collect(HitCollectorWrapper.java:46)
at org.apache.lucene.search.TermScorer.score(TermScorer.java:86)
at org.apache.lucene.search.TermScorer.score(TermScorer.java:74)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:252)
at org.apache.lucene.search.Searcher.search(Searcher.java:110)
I did some google searching, but to no avail - I am new at lucene, and can't tell if this is just not a feature that's supported by 2.9.4, or if it's a pylucene issue, or if my code is wrong. Please help!