Is there an "in" Python keyword equivalent in Java? [duplicate] - java

This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 10 years ago.
What is the most simple/efficient way of implementing the following Python code in Java?
if "foo" in ["foo", "bar"]:
print "found."

If you have your data in array use this
String[] strings = {"foo","bar"};
for (String s : strings) {
if (s.equals("foo")) {
System.out.println("found");
break;
}
}

Related

check if all Objects of a stream meet a rule [duplicate]

This question already has answers here:
how to check if all elements of java collection match some condition?
(3 answers)
Is it possible to check whether all Java 8 stream elements satify one of given predicates?
(3 answers)
Closed 3 years ago.
I want to check if all objects of a stream meet a rule, and returns True only if all of them meets the rule,
but I have a compilation error: Role cannot be applied to lambda parameter
public static Predicate<Hostel> areAllTrue() {
return req -> req.getRole().stream(r -> isTrue(r));
}
private static boolean isTrue(HostelRole hostelRole) {
}
Use the terminal operation allMatch:
public static Predicate<Hostel> areAllTrue() {
return req -> req.getRole().stream().allMatch(r -> isTrue(r));
}

My Java application crashes because of this method [duplicate]

This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(31 answers)
Closed 4 years ago.
My application crashes because of this method. Please help :=)
public void sortedList() {
String goodLetter = "B";
for (String myItem : myArrayList) {
String myFirstChar = myItem.substring(0, 1);
if (myFirstChar != goodLetter) {
myArrayList.remove(myItem);
}
}
}
Your code will crash if:
myArrayList contains a null element (NullPointerException)
myArrayList contains the empty String (IndexOutOfBoundsException)
Any element from myArrayList does not start with "B" (ConcurrentModificationException)
For that last, and most probable, cause see the question linked by #Todd in the comments.
Additionally, in Java you should compare Strings using .equals instead of == or !=.
First, for string comparison, use String.equals() and not ==.
Second, if you want to remove objects from a list :
do a reverse browsing
or create an intermediary array containing objects to remove

Why is it impossible to compare a String and null with equals? [duplicate]

This question already has answers here:
How to check if my string is equal to null?
(28 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
In the getter of my class the attribute may have null value from database :
public String getCible() {
return cible.equals(null) ? " - " : cible;
}
In my code I want to call this getter , but I got NullPointerException.
If I change the comparison to return cible == null ? " - " : cible; then everything is wright :) So why ?
You call a method on a null, so it is like null.equals(null), null has no methods so it throws the exception.
If you dont want to use ==, you can use Objects.equals(yourString, null)

Passing by reference in Java. Help me understand [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Immutability of Strings in Java
(26 answers)
Closed 5 years ago.
I have the following code
public static void main(String[] args) {
String word = "Super";
reverseString(word);
System.out.println(word);
}
public static String reverseString(String word) {
String helper = "";
int i = word.length() - 1;
while (i >= 0) {
helper += word.charAt(i);
i--;
}
return helper;
I do not understand why when I'm printing the "word" variable it still prints "Super" even though I changed it in the reverseString method. I understand that strings are passed by reference and not a copy like primitive values.
If I do word = reverseString(word) it prints the reverse what I expect, "repuS"
Thanks
You're not changing the string in reverseString, you're creating a new one and returning the new one (which you've called helper).
A second thing to note about strings in Java is that they're immutable - all string methods return a new string rather than modifying the one you're calling the method on.

Java: Converting Integer to String. Comparing with == equals operator [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
public static void main(String[] args) {
Integer i = new Integer(4);
System.out.println(i.toString());
if (i.toString() == i.toString()) {
System.out.println("true how");
} else {
System.out.println("false how");
}
}
While executing above code, I am getting output as "false how".
Can you explain how Jvm treats this object?
toString() creates a new string object every time and your code is actually checking if both references are the same, which is never the case so it runs the else case. If you try
i.toString().equals(i.toString())
you'll get the desired output.
You must compare objects with equals() method.
i.toString().equals(i.toString())

Categories