Perl's grep-ish guy in Java

A process that extracts only those that meet the conditions from an array or list. With Perl, you can easily write as follows (remember)

sample.pl


my @array = ('001', '901', '201', '010', '999', '311');
my @result = grep { /1$/ } @array;

After investigating whether Java can do the same, it seems that ListUtils # select () can do something similar.

Sample.java


List<String> list = Arrays.asList("001", "901", "201", "010", "999", "311");
List<String> result = ListUtils.select(list, new Predicate<String>() {

   @Override
   public boolean evaluate(String arg0) {
      //At the end'1'When is attached
      return StringUtils.endsWith(arg0, "1");
   }
});
		
System.out.println(result.toString()); //=> [001, 901, 201, 311]

I tried to make Predicate an anonymous inner type, but which is better to implement Predicate in my class? The latter feels more like "old writing". This time I wrote a sample source with a list of strings, but I think it will be more effective if you want to extract from a list of beans.

Is it like this when writing without using ListUtils # select ()? It made me feel familiar. The usual pattern that the nesting of if statements gets deeper and deeper as the conditions become more complicated.

Sample2.java


List<String> list = Arrays.asList("001", "901", "201", "010", "999", "311");
List<String> result = new ArrayList<String>();
for (String s : list) {
    if (s.endsWith("1")) {
        result.add(s);
    }
}

System.out.println(result.toString()); //=> [001, 901, 201, 311]

Recommended Posts

Perl's grep-ish guy in Java
NVL-ish guy in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Try using RocksDB in Java
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
Edit ini in Java: ini4j
Java history in this world
Try calling JavaScript in Java
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Topic Analysis (LDA) in Java
NEologd preprocessing in Java neologdn-java
Change java encoding in windows
Java Stream API in 5 minutes
Cannot find javax.annotation.Generated in Java 11
Read standard input in Java
Implement Basic authentication in Java
Find a subset in Java
Null-safe program in Java (Eclipse)
[Java] Get KClass in Java [Kotlin]
Create Azure Functions in Java
Import Excel data in Java