How to use contains() with substring - java

i have an ArryList which have (name+"\n"+phoneNumber) so I wanna see if the name is containing with the list ? I used this code
HashSet<String> set = new HashSet<String>(ContactsList);
if (set.contains(name))
{}
else
{
ContactsList.add(name+"\n"+phoneNumber+"\n");
}
but how to use substring in contains so I get just the name from it to contain it with the name that I wanna add it to the list
and thanks

Don't add the data in ArrayList as (name+"\n"+phoneNumber),
Create a class which 2 attributes name and phoneNumber, then create object of that class set both attributes and add that object in the ArrayList.
This is the right way of doing it and your problem also will be solved.

Related

Iterating inside a java stream filter

return Arrays.stream(partNumbers.get())
.filter(partNumber -> Objects.nonNull(partNumber.getDescription()))
.filter(partNumber -> partNumber.getDescription().toLowerCase().contains(rateAbbr.toLowerCase()))
.findFirst();
The above code would try to find a partNumber from a list of partNumbers where partNumber's description contains a 'rateAbbr'.
This code worked till 'rateAbbr' was a String but now it is changed to a list of rateAbbrs and I need to find a part number whose description contains any of the rateAbbrs. I tried it with streams and no luck yet. any help is appreciated.
just create a private boolean function that iterates over the list and checks if there is match, then call it inside filter method.

Appium : Verify relative element presence

Can you give a suggestion(please see pic) how can I check if selectIndicator is present on one block then I should choose another one. I know how to check if that element isPresent on whole page, but I need to find if it present on particular element. In my example I have Living Room chosen, and I need to check if DVR not chosen -choose that one. Any idea how can I do it? I was trying to check this way, but no luck:
WebElement element= driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");
[http://i.stack.imgur.com/F98DM.png]
If I am not getting you wrong you want to implement a self defined data structure for an appropriate solution. That could be something similar to this :
public class DVRList {
//declare components required to comprise one item
private String dvrOptionText ;
private boolean dvrOptionCheck ;
// implement setter..getter for these two
}
...in some method set the value using the logic
DVRList dvrlist = new DVRList();
WebElement parentOfBoth = driver.findElement(By.xpath("
//android.widget.RelativeLayout[1]/android.widget.R‌​elativeLayout[1]");
String text = parentOfBoth.findElementByAccessibilityId("First element").getText();
dvrlist.setdvrOptionText(text);
if(isElement(parenOfBoth.findElementByAccessibilityId("Second element"))
dvrlist.setdvrOptionCheck(true);
else dvrlist.setdvrOptionCheck(false);
and thereafter you can use these parameters accordingly.
Note : Parameters and approach are generalised and should be modified for serving the exact purpose.

Howto check if a TreeMap contains a specific object?

In an application I have this TreeMap object:
treePath = new TreeMap<String, DLFolder>();
The first String parameter should be the key and the DLFolder is the value.
Ok the DLFolder object have this method dlFolder.getPath() that return a String
So I want to know if the treePath object contains a DLFolder object having a specific path value
Can I do this thing?
Tnx
for (DLFolder dlf : treePath.values()) {
if ("A SPECIFIC PATH".equals(dlf.getPath()) {
// do someting with the dlf
}
In Java 8 this is rather straightforward.
treePath.values().anyMatch(dlf -> dlf.getPath().equals(specificValue))
You can loop through the values of the TreeMap:
for (DLFoder folder : treePath.values())
if (folder.getPath().equals(somePathValue))
// path found!
If the map's key is also the value stored in dlFolder.getPath(), then yes, you can just call treePath.contains("Value");.
Other options include:
Iterating over treePath's values either using an iterator, an enhanced for loop, or the Java 8 streams.
Creating another map to map the same DLFolder objects, but by path.

How to access the values from strings.xml dynamically?

What I want to do is to get a specific text from strings.xml dynamically. I think it will involve to access an object variable dynamically.
There will be a function like:
public void getDynamicString(int level) {
text.setText(R.string.levelText_+level);
}
And in strings.xml there will be <string name="levelText_5">Text level 5</string>
I would rather not create a list with all the text resources. Can one do this in Java/Android.
Use the method getIdentifier(name, defType, defPackage) of the Resources class to get the id of a resource by name. Then you can do a normal getString(id) from the same class.
EDIT: a bit of Googling revealed this: this. You can find sample usage there.
Try: getResources().getString(R.id.stringId).
You should look at using getIdentifier(String, String, String) of the Resources class.
All you have to do is call
this.getString(R.string.levelText_5)
If your in an area of the program in which you have access to a Context or Application, such as a ListAdapter call:
context.getString(R.string.levelText_5)
or
application.getString(R.string.levelText_5)
if you have no access to the context or application then call:
getResources().getString(R.String.levelText_5);
To do it dynamically call:
String name = "levelText_"+level;
int id = getIdentifier(name, "string", "com.test.mypackage");
getResources().getString(id);
I had the same problem and I fixed it using this
okey , whenever you want to access a string from strings.xml dynamically and what i mean by that is to avoid using getResources().getString(R.id.stringId) ,you create a string in which you can manipulate dynamically however you want in our case uriq ("stupid variable name") and then you create resource object which is in my example level_res and initialize it then you use this method called getIdentifier() which accepts your dynamic string as a parameter ,now u simply pass your ressource to the method getstring(mysttring)
String uriq="level"+level_num;
level_res=getResources();
int mystring=getResources().getIdentifier(uriq,"string",getPackageName());
String level=level_res.getString(mystring);

Look for string in arraylist element and pair it with specific image

Im trying to do a MVC application with Model , JSP and Servlet.
From my Model I get an arrayList:
ArrayList<String> myArrayList = new ArrayList<String>();
Each element look like this example: 96125;www.qwerty.com
Lets say I have a image named: www.qwerty.com.png
I want to take myArrayList pass it from my servlet to the jsp. So lets say I call for myArrayList[0] and the first element is 96125;www.qwerty.com I want it to show the image www.qwerty.com.png in my JSP instead of the actual element.
How can I solve this?
For getting just the image name, you can use split method of String Class:
myArrayList[0].split(";")[1]

Categories

Resources