In last couple of tutorials you have learned how to get all selected checkbox and radio buttons, and in this tutorial, you will learn how to enable/disable form elements using jQuery. A form element e.g. a textfield, radio buttons and checkbox, all represented using input tag can be enabled or disabled by adding an attribute called "disabled". It's similar to "checked" attribute we have seen for checkbox and radio buttons. If this attribute is not present then element is enable, but if this attribute is present then you can use disabled=true to disable the form element and disabled=false to enable the form elements as shown in our example.
Thursday, November 21, 2024
Thursday, November 14, 2024
How to Compare Two Strings Without Case Sensitivity in Java? Example
How to Compare Two Strings Without Case Sensitivity in Java
Java provides several ways to compare strings without taking case sensitivity into account. In this article, we'll explore different methods to perform case-insensitive string comparisons in Java.
Method | Use Case | Efficiency |
---|---|---|
equalsIgnoreCase() | Checking equality | High |
compareToIgnoreCase() | Ordering comparison | High |
String.CASE_INSENSITIVE_ORDER | Sorting collections | High |
equals() + toUpperCase() | Not recommended | Low |
1. Using equalsIgnoreCase()
The equalsIgnoreCase() method
This method is ideal for checking if two strings are equal without considering their case. It returns true if both strings contain the same characters, regardless of their case.
String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2); // Returns true
2. Using compareToIgnoreCase()
The compareToIgnoreCase() method
Use this method when you want to check which string is greater or smaller, or to find out their relative ordering in lexicographical or alphabetical order, without considering case.
String str1 = "Apple";
String str2 = "banana";
int result = str1.compareToIgnoreCase(str2); // Returns a negative value
3. Using String.CASE_INSENSITIVE_ORDER
The String.CASE_INSENSITIVE_ORDER comparator
This comparator is useful for sorting a list of strings in case-insensitive order.
List<String> fruits = Arrays.asList("Apple", "banana", "Cherry");
Collections.sort(fruits, String.CASE_INSENSITIVE_ORDER);
// Result: [Apple, banana, Cherry]
4. Using equals() + toUpperCase()
Combining equals() and toUpperCase()
This is a tricky way to compare two strings in Java in a case-insensitive manner. First, convert both strings to uppercase, then compare them using equals().
String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.toUpperCase().equals(str2.toUpperCase()); // Returns true
Note: This method is not recommended as it creates new String objects, which can be inefficient for large-scale comparisons.
Conclusion
When comparing strings without case sensitivity in Java, it's generally best to use the built-in methods like equalsIgnoreCase()
for equality checks and compareToIgnoreCase()
for ordering comparisons.
These methods are optimized and provide clear, readable code. For sorting collections of strings, the String.CASE_INSENSITIVE_ORDER
comparator is an excellent choice.
Remember, all these methods compare strings in lexicographical order. Choose the method that best fits your specific use case to ensure efficient and accurate string comparisons in your Java applications.
Thursday, November 7, 2024
How to convert XML to JSON in Java? Example
Since XML and JSON are two popular data interchange formats, you will often need to convert one into other. For example, many SOAP web services returns XML response and if you are using Java and wants to convert that XML response to JSON, then you can use JSON library from https://json.org. It provides a class called XML.java, which provides static methods to convert an XML text into a JSONObject. It also support JSON to XML conversion, which will learn in second part of the article. In order to use this class, you can dowload JAR files from Maven Central repository, or you can add required dependency in your pom.xml file.
Thursday, October 31, 2024
How to convert JSON String to Java HashMap? Example
Suppose you have a JSON String, may be a web-service response and you want to create a Map out of it, where key should be the fields and value should be values of those fields e.g. if given JSON String is following :
{
"zip": "90210",
"city": "Beverly Hills"
"state": "CA"
"timeZone": "P"
}
then your HashMap should contain 4 mappings where keys are zip, city, state and timeZone, all of String type and corresponding values.
Thursday, October 24, 2024
What is Timer and TimerTask in Java – Tutorial Example
4 Reasons and Benefits of Using Multithreading in Java? Why Threads?
4 examples to convert Date to LocalDate in Java 8?
One of the challenge you will face while using new Java 8 Date and time API, JSR-310 in existing Java application is the conversion between java.util.Date and java.time.LocalDate and other classes. Even though library has everything you would expect, it doesn't have a direct conversion method between old Date and new LocalDate. There is a reason for it, eventhough java.util.Date says its a date its not, becuase its just a millisecond value since midnight at the start of 1970 GMT. It's equivalent to Instant class in new Java Date API and that's why you have a direct conversion method between Date and Instant in Java 8. Anyway, its not hard to convert a java.util.Date to java.time.LocalDate in Java 8, in fact there are multiple ways which we will explore in this tutorial.
Wednesday, October 23, 2024
Difference between ExecutorService.submit() and Executor.execute() methods in Java?
Difference between Process and Thread in Java - Example
How to use Exchanger for Inter thread communication in Java? Example Tutorial
What happens when you call Thread.run() instead of Thread.start() in Java? Trick Interview Question
How to Implement Thread in Java with Example
In my opinion, Thread is one of the most important features of the Java programming language which helped it to become the most popular programming language. I remember, when I first started learning Java in one of the programming classes in India how important Thread was a portrait and how much emphasis is given on a clear understanding of multi-threading. It’s still popular and one of most sought after skills in Java programmer because writing concurrent and multi-threaded applications in Java is challenging, despite Java providing excellent support at language level using synchronized and volatile keyword.