Stop word using HashSet - java

In this condition I want to read data from a file, but not all the words. Is this condition correct? set in the following code is a HashSet.
if (!set.contains(word.toString().equals(set)))
{
word.set(str);
context.write(word, one);
}
else
continue;

This code would not work, because the contains method on Set checks by means of equals, whether the Set contains the item. You do not need to use the equals check again.
if (!set.contains(word))
{
word.add(word);
}
else
continue;
I am not sure I get what you asked, but hope this helps. Leave a comment and I would be glad to edit/remove my answer.

Related

How do I stop certain code from running?

I have a class that "bans" a player, but I don't want that player to be banned if his name is within a string array. I could loop through the length of the array and use booleans, but there has to be an easier way? I saw something that said just put if a condition is met, put return; and it'll stop all code running below that if statement.
Edit: Thanks for all the help! You were all helpful, even if you're one of the people that downvoted this, which is 4 people at least.
You could make a method that checks if the player is in the array of Strings and yes if you use return in a void method the method will just end.
For example
public void returnUsage(int n)
{
if(n==1)
{
return;
}
System.out.println("n doesn't equal 1.");
}
But it would probably be best to use an if and else to skip the code you don't want to run if the condition is not met. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
An array is the wrong data structure for this task. Add the players to a Set<> allowedPlayers = new HashSet<String>() then use if(allowedPlayers.contains(name)) return; to conditionally exit the method based on whether the name of the player is in the set. The set will remain fast when the number of names gets large. With an array scanning the array will be slow when the array is big.

Need logical solution for uncountable loop dillema

Supposed I have this kind of loop in java (not an exact code, neither it's a truly valid code, but just to give you a general perspective of situation):
print "<ul>";
while (res = fetch(database)) {
print ("<li>" + res.col['data'] + "</li>");
}
print "</ul>";
and I have this CSS, which makes the last item on the list has red color.
ul li:last-child { color: red; }
this works fine on most browsers. the problems are:
I need to make this work on IE8 too.
IE8 doesn't support last-child.
I cannot test whether current iteration of "while" is entering last iteration or not. or let's say, there's no way I could check when the loop would end. this is not an ordinary loop, but let's just say like that. so I can't give the last <li> a class, say class="lastchild".
I also tried with javascript and jquery, and both can't select "last-child" either, as they depends on the css for the selection, I think.
how is the best approach in this situation? thanks.
print "<ul>";
String liText=null;
while (res = fetch(database)) {
if (liText!=null) {
print ("<li>" + liText + "</li>");
}
liText=res.col['data'];
}
if (liText!=null) {
print ("<li style='the last element style'>" + liText + "</li>");
}
print "</ul>";
Move the adding of the last line out of the while and apply desired style there
Assuming that the problem is in Java, I am wondering why there is no way to detect last record. Probably you need to reconsider you data structure first, populate the data in a Map or a List and then iterate over.
And, if that is not possible, you can possibly opt for a StringBuilder instnace to build the entire String html tags first by looping over and then replace the last li element with the desired style and after that put it in the print statement for rendering.
The above points are valid even if you are trying to code in javascript, with little bit of change.

Hashset duplicate values but I dont have custom objects

I am using jsoup parser to extract my anchor tags and then I am just adding the links to a hash set.
The code is as follows
Posting my entire code. I understand the issue is because I am using toString and the value would change My goal is when I get a bunch of links I want to eliminate links such as http://cse.syr.edu and http://cse.syr.edu/ so that my hashSet contains unique elements. How could I do this
for ( Element link : links)
{
String test=link.attr("abs:href");
if(!(link.attr("abs:href").contains("http://cse.syr.edu")))
continue ;
else if(h.isEmpty()){
h.add(test);
}
else if(h.contains(test) || h.contains(test+"/")) // I now removed (test+"/")
continue;
else {
h.add(test);
}
I have updated my question now thanks RJ
There's probably whitespace in your Strings. HashSet works just fine.
If we are talking about java.util.HashSet, the most likely explanation is that your diagnosis of the problem is incorrect. Make sure that the strings in the set are indeed identical (and not subtly different), and that you are not accidentally re-creating or clearing the HashSet between adding identical strings.

Selenium check that text is not present

I want to check that text is now not present on a webpage without my test failing
I have achieved this by catching the exception but is there a better way of doing this?
try {
selenium.isTextPresent(selenium.getText("27"));
} catch (Exception e) {
System.out.println("Element does not exhist");
}
There's nothing wrong with try-catch.
Anyway, what you have should be more of a isElementPresent("27") since you discard the return value of isTextPresent(). Moreover, if the element exists, then isTextPresent() will always return true, because ... well, you took the text out of existing element, it has to be there. In this case, it is enough just to assure whether the element exists or not.
But if you do actually need it in real code somehow, then what about if (selenium.isElementPresent("27") && selenium.isTextPresent(selenium.getText("27"))) ?
Also, getXpathCount(//*[#id='27' and text()]) == 0 expression does the trick, too. It count the number of returned elements that have id=27 and contain some text. If that's a zero, there are none.
you can use assertTextNotPresent OR verifyTextNotPresent
# Slanec Yes, its my mistake
You can use
verifyFalse(selenium.isTextPresent("text"));
assertFalse(selenium.isTextPresent("text"));
Based on the docs you should be able to say the following:
selenium.isTextPresent("27"));
This returns a boolean so if you want to make sure it is not there... check it is false.

what is the best way to check whether the list is null or not before looping?

I know it is a primitive question but I want to learn the smartest way.
I want to loop over the ArrayList<Integer> intList and it can be null. I have 2 ways of doing it
if(intList != null) {
for(int i = 0; i < intList.size(); i++){
System.out.println(intList.get(i));
}
}
and
for (int i = 0; intList != null && i < intList.size(); i++){
System.out.println(intList.get(i));
}
First way seems more pretty to me. What do you think? What are your implementations in that situation?
Excuse me, if it is duplicate question but I can't find one
Thanks
In this case I would choose the first implementation as well because its intent is clearer.
Generally, I would try to avoid a List (or any other Collection object, really) being null. When evaluating a List (which is suddenly and unexpectedly null) you most probably want to abort before any processing takes place so either case of looping over the collection would not occur.
We have one development rule in our company:
If a function is written that returns a list or an array, never return null! Return an empty list or an empty Array in the case where there are no elements to return.
This can be done with minimal overhead, like here:
public List<String> getNames() {
if( !loaded ) {
return Collections.emptyList();
}
...
}
If applied properly, you don't have to check for null-lists. We don't have to.
EDIT: Oh, and to come back to your question at hand: Use the forst variant, it is much clearer, and faster, because the null check only have to be done once (it might be that the compiler factors it out anyway, but in variant 1 you can be sure).
I prefer the first one, for the complete code segment will most likely to be:
if(intList != null) {
for(int i = 0; i < intList.size(); i++){
System.out.println(intList.get(i));
}
}
else {
//Do something here.
}
i preffer the first one as mostly i use the first process
To me the first option is clearer and easier to read and understand.
According to me first option should be preferred for its readability is better than second one, while the second one saves one extra line of code for you.
End of the day both are going to do same thing so its up to you, which code you want to use. I would suggest to stick to first as it is more developer friendly because of readability.
The first form is more readable - the intent is much clearer.
The first form may also be faster, since the second form says to test that intList is not null each time you go around the loop. (The JIT compiler may optimize away the unnecessary tests ... but why rely on this.)

Categories