Java中列表元素存在性检查及表单数据列表首元素非空判断方法
Hey there! Let's break down your two questions one by one.
Checking Validity of the First Element in Form Data Lists
First up, handling those form data list strings like values[,"sample"] or values["test","sample"] to check if the first element has a valid value. Here's how I'd approach this in Java:
- Strip the wrapper: First, we need to get rid of the
values[and]around the actual elements. A simple regex replacement works here. - Split elements correctly: Since elements are wrapped in quotes, we can't just split on commas—we need to split only on commas that aren't inside quoted strings. A regex like
,(?=(?:[^"]*"[^"]*")*[^"]*$)does the trick here. - Clean the first element: Remove any surrounding quotes and trim whitespace from the first element.
- Check validity: If the cleaned first element isn't empty, it's valid.
Here's a code snippet that puts this all together:
public static boolean isFirstElementValid(String formList) { // Extract the inner content by removing the values[ ... ] wrapper String innerContent = formList.replaceAll("^values\\[(.*)\\]$", "$1"); if (innerContent.isEmpty()) { return false; } // Split elements, avoiding splits inside quoted values String[] elements = innerContent.split(",(?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)"); if (elements.length == 0) { return false; } // Clean the first element: strip quotes and trim whitespace String firstElement = elements[0].replaceAll("^\\\"|\\\"$", "").trim(); // A valid element isn't empty after cleaning return !firstElement.isEmpty(); }
Testing this with your examples:
values[,"sample"]→ first element becomes empty after cleaning → returnsfalsevalues["test","sample"]→ first element is "test" → returnstrue
Checking if a Java List Contains a Specified Element
For checking if a list has a specific element in Java, the List interface has a built-in contains() method that's your go-to tool. Let's cover the basics and some edge cases:
Simple String List Example
List<String> fruits = Arrays.asList("apple", "banana", "cherry"); boolean hasBanana = fruits.contains("banana"); // true boolean hasDate = fruits.contains("date"); // false
Important Details:
- Equality Matters:
contains()uses theequals()method of the elements. For strings, this is case-sensitive—so"Banana"won't match"banana"unless you handle it explicitly. - Case-Insensitive Check: If you need to ignore case, use a stream:
boolean hasBananaIgnoreCase = fruits.stream() .anyMatch(fruit -> fruit.equalsIgnoreCase("Banana")); - Custom Objects: If you're using your own classes (like a
UserorProduct), make sure to overrideequals()andhashCode()correctly. Otherwise,contains()will only match the exact object instance, not objects with the same data.
Custom Object Example
class User { private String username; public User(String username) { this.username = username; } // Override equals and hashCode to compare usernames @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return Objects.equals(username, user.username); } @Override public int hashCode() { return Objects.hash(username); } } // Usage List<User> users = Arrays.asList(new User("alice"), new User("bob")); boolean hasAlice = users.contains(new User("alice")); // true, thanks to our equals override
内容的提问来源于stack exchange,提问作者Vamsi
相关产品推荐
相关产品推荐

