How to reference a method on the object itself [duplicate] - java

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

Related

lambda expression not supporting in stream().allMatch() / .anyMatch() method [duplicate]

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.

Implement re.search() functionality in Groovy or Java [duplicate]

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]]

Need java equivalent of this c# code [duplicate]

This question already has answers here:
Reading a plain text file in Java
(31 answers)
How do I split a string in Java?
(39 answers)
Closed 5 years ago.
Learning file I/O in java, but cant seem to get java to recognize this format in a text document :
A=1
B=2
.
.
.
.
Z=26
What i want is for the letters A through Z to be equal to the int counterpart, I've been able to do this in C# using this code:
var dic = File.ReadAllLines(AplhabetFile)
.Select(l => l.Split(new[] { '=' }))
.ToDictionary(s => s[0].Trim(), s => s[1].Trim());
but i can't seem to find its exact java equivalent anywhere.
Any Ideas ?
You can do the same with Streams:
Map<String, String> dic = Files.lines(Paths.get(AlphabetFile))
.map(l -> l.split("="))
.collect(Collectors.toMap(s -> s[0].trim(), s -> s[1].trim()));

senkeys() in silenium throughing error while having added all build path and having jdk 8 [duplicate]

This question already has answers here:
The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)
(7 answers)
Closed 6 years ago.
Im beginner in selenium , installed properly with JDK 8 and silenum 2.52 ,getting this error repeatedly . Cnt go forward . Please help me out .
The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)
Java Problem
try
driver.findElement(By.name("q")).sendKeys("Cheese!");
or
driver.findElement(By.id("lst-ib")).sendKeys("Cheese!");

Integrating java and .net dll using JNI

I am doing a small project it is a interoperability of java and .net dll.
Focus:
I just have a java file which calls the .net dll which is created by using C# and CPP and MCPP..
The program is just a hello world Program..
I just refer the below mentioned sites.
http://www.codeproject.com/Articles/378826/How-to-wrap-a-Csharp-library-for-use-in-Java
http://www.codeproject.com/Articles/13093/C-method-calls-within-a-Java-program
Finally I just get some ideas and finally did this with some errors.
Coding:
#using "mscorlib.dll"
#using "CSharpHelloWorld.netmodule"
using namespace System;
public __gc class HelloWorldC
{
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld __gc *t;
HelloWorldC() {
t = new CSharpHelloWorld();
// Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
t->displayHelloWorld();
}
};
Errors:
Error 1 error C4980: '__gc' : use of this keyword requires /clr:oldSyntax command line option
Error 2 error C3699: 'interior_ptr' : cannot use this indirection on type 'CSharpHelloWorld'
Error 3 error C2750: 'CSharpHelloWorld' : cannot use 'new' on the reference type; use 'gcnew' instead
Error 4 error C2440: '=' : cannot convert from 'CSharpHelloWorld *' to 'CSharpHelloWorld ^' 11 WindowsComponentProject
Error 5 error C2011: 'HelloWorldC' : 'class' type redefinition 6 WindowsComponentProject
Error 6 error C3699: '*' : cannot use this indirection on type 'HelloWorldC' 18 WindowsComponentProject
Error 7 error C2750: 'HelloWorldC' : cannot use 'new' on the reference type; use 'gcnew' instead 18 WindowsComponentProject
Error 8 error C2440: 'initializing' : cannot convert from 'HelloWorldC *' to 'HelloWorldC ^' 18 WindowsComponentProject
Error 9 error C2027: use of undefined type 'HelloWorldC' 21 WindowsComponentProject
Error 10 error C2227: left of '->callCSharpHelloWorld' must point to class/struct/union/generic type 21 WindowsComponentProject
I just seek some sites for the solution to change the properties of CLR but it doesn't works kindly help me over it.. thanks in advance !!!
You are using old Managed C++ syntax.
New one is called C++/CLI.
You create a reference type by prefixing the class declaration with ref keyword. Use ^ to declare a reference variable as in CSharpHelloWorld^ t.
gcnew is what you use to create an object in managed heap.
You should modify your class as shown below.
using namespace System;
public ref class HelloWorldC {
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld^ t;
HelloWorldC() {
t = gcnew CSharpHelloWorld();
// Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
t->displayHelloWorld();
}
};

Categories

Resources