How to get first String based on whitespace out of a full String using java8? - string

Let's Example I have String s = "Rahul Kumar" I need to have Rahul as a output using java8
Actual Requirement, I do have a list of Trip Object, I want to set driverName property as only first name to each Trip Object and return that list?
System.out.println( someStringValue.subSequence(0, someStringValue.indexOf(' ')));
I'm getting trouble to incorporate this code into the listOfTrip. If I'm doing like this,
List<CharSequence> list = listOfTrips.stream().map(e -> e.getDriverName().subSequence(0, someStringValue.indexOf(' '))).collect(Collectors.toList()); System.out.println(list);
Here, With this, The return type is wrong and it is not fetching only first name out of full name.

Below will give you the proper result:
List<CharSequence> list2 = listOfTrips.stream()
.map(m->m.getDriverName().substring(0,m.getDriverName().indexOf(' ')))
.collect(Collectors.toList());

Please try this also once:
String s = "Rahul Kumar";
Optional<String> beforeWhiteSpace = Pattern.compile(" ").splitAsStream(s).collect(Collectors.toList()).stream().findFirst();

Related

getDocumentByKey with a number vector doesn't find the document

I have a 2 column sorted view and try to get a document the following code:
var searchArr = new java.util.Vector();
searchArr.addElement(10000310);
searchArr.addElement(45);
var customerdoc:NotesDocument = viw.getDocumentByKey(searchArr,true);
but the result is null.
If I use only the first element for the key (10000310), then I get (the first) doc with that key. But with the 2-element-vector the lookup returns null.
the same in LotusScript works fine:
Dim searchkey(1) As Double
searchkey(0) = 10000307
searchkey(1) = 45
Set doc = luview.Getdocumentbykey(searchkey, true)
gives me the document I need.
Confusing, for me ....
Uwe
This is a known bug, hopefully to be fixed in 9.0.2. See this question getDocumentByKey with view category separated by "\\" in XPages
Your LS example uses an array, not a Vector. I am not even sure if it is intended to work with a Vector - never did that. So just use an array here, too, as the key.

What is wrong in this LINQ Query, getting compile error

I have a list AllIDs:
List<IAddress> AllIDs = new List<IAddress>();
I want to do substring operation on a member field AddressId based on a character "_".
I am using below LINQ query but getting compilation error:
AllIDs= AllIDs.Where(s => s.AddressId.Length >= s.AddressId.IndexOf("_"))
.Select(s => s.AddressId.Substring(s.AddressId.IndexOf("_")))
.ToList();
Error:
Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<MyCompany.Common.Users.IAddress>'
AllIDs is a list of IAddress but you are selecting a string. The compiler is complaining it cannot convert a List<string> to a List<IAddress>. Did you mean the following instead?
var substrings = AllIDs.Where(...).Select(...).ToList();
If you want to put them back into Address objects (assuming you have an Address class in addition to your IAddress interface), you can do something like this (assuming the constructor for Address is in place):
AllIDs = AllIDs.Where(...).Select(new Address(s.AddressID.Substring(s.AddressID.IndexOf("_")))).ToList();
You should also look at using query syntax for LINQ instead of method syntax, it can clean up and improve the readability of a lot of queries like this. Your original (unmodified) query is roughly equivalent to this:
var substrings = from a in AllIDs
let id = a.AddressId
let idx = id.IndexOf("_")
where id.Length >= idx
select id.Substring(idx);
Though this is really just a style thing, and this compiles to the same thing as the original. One slight difference is that you only have to call String.IndexOf() one per entry, instead of twice per entry. let is your friend.
Maybe this?
var boundable =
from s id in AllIDs
where s.AddressId.Length >= s.AddressId.IndexOf("_")
select new { AddressId = s.AddressId.Substring(s.AddressId.IndexOf("_")) };
boundable = boundable.ToList();

How to search and sort in J2me (ascending or descending)

I am wondering about how to search in J2ME. I have been searching in the internet, so many result show to me, and I see in Java2s.com I got a result use RecordFilter and matches method for search in record store.
But my problem is, when I need to pass 2 or more parameters into it. How can result matches with these parameter?
And how to sort descending or ascending like bubble sort?
Concatenate your searches into a single String variable. Separate each of them with ; for example. In the code of the matches method explode the String to get each search criteria.
To make the filter in effect create an instance of SearchFilter and call the matches method with the concantenated String as its param.
For the sort implement the RecordComparator interface ; implement the compare method to build your sort criteria. Make a google search about j2me+recordcomparator to see examples about how to make sorts.
EDIT :
In the code of the matches method explode the String param obtained from the byte[] param. Treat each String exploded to make the criteria.
As I understand you want to pass two string as a search criteria when you wrote :
SearchFilter search = new SearchFilter(txtSearch.getString(), strType);
So in the constructor there should be two params !!!
When you want to make the matching then call
if searchFilter.matches((search1+";"+sType).getBytes())
Then explode the candidate param into two String when you code the matches method.
When I save my Data in RMS I save it as a String[] like I want to save Name, Age,Salary,EmpID for each employee I save it create an array and convert it to bytes and save it in RMS. When i retrieve it i do the reverse process. Now if i want to get employee with names starting with A and with salary 10000 i use the following filter
class UtilFilter implements RecordFilter{
public UtilFilter(String str_searchText,String str_searchText1)
{
this.str_searchText = str_searchText.toLowerCase();
this.str_searchText1 = str_searchText1.toLowerCase();
}
public boolean matches(byte[] bt_byteData)
{
String str_str = "";
String str_str1 = "";
//here goes code how u get back ur String[] from RMS say u get it in Data
str_str = Data[0].trim();
str_str1 = gd_cd.Data[2].trim();
if(str_searchText != null && str_searchText1 != null && str_str.equals(str_searchText) && str_str1.equals(str_searchText1 ))
{
return true;
}
else
{
return false;
}
}
}
This way i can filter any no of parameters.Hope tht helps! :)

String matching in HQL

I'm creating an HQL query to filter a grid of data:
string formatString = "Type = {0} AND {1} = '{2}' AND CompletedDate > '{3}'
AND CompletedDate < '{4}' AND UserName LIKE '{5}'";
HqlBindingSource.Where = string.Format(formatString, type, keyId, entityID,
fromDate, toDate, toDate, UserTextBox.Text);
The problem I'm having is the string matching on the UserName field. I'm used to working with SQL and I can't get it to match on a value using = or LIKE. Can anyone point me in the right direction for this?
Thanks
I had "toDate" listed twice, so UserTextBox.Text was never used in the query.

How are AMF3 string tables built?

I am working on AMF3 format decoding. I have used inline objects and variables, however I am having problem with the String reference. Can anyone tell how String reference table is built?
Currently, I have:
var object:Object = new Object();
object.s1 = "abc";
object.s2 = object.s1;
object.s0 = "111";
byteBuffer.writeObject(object);
then I write this to a byteBuffer and finally to a file. I am wondering how String references are built?
Flex builds a table for string references. As soon as Flex encounters a string in an object, it does the following:
If the string is new:
Add the string to the table and increase the reference number.
Otherwise
add a reference to the string.

Resources