[JAVA] I tried something called recursive search

I made it

Search.java


package search;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

public class Search {
	
	public static void main(String[] args) {
		
		String path = "C:\\Users\\";
		String word = "\\pool\\plugins\\";

		if (validate(path, word)) {
			//Recursive search when input value is OK
			search(path, word);
		}
		
		System.out.println("end");
	}
	
	/**
	 *Search the directory by string.
	 * <p>
	 *While outputting the absolute path of the file and directory with the name including the search string
	 *Search recursively.
	 * 
	 * @param dir Search target directory
	 * @param word Search string
	 * @throws IOException When something doesn't open
	 */
	private static void search(String dir, String word) {
		
		//Get the list in the directory
		File[] directory = new File(dir).listFiles();
		
		//If the object does not exist,End processing
		if (directory == null) {
			return;
		}
		
		//Outputs absolute path for those containing search words
		Arrays.stream(directory).map(File::getName).filter(file -> isMatch(file, word))
				.forEach(file -> System.out.println(dir + "\\" + file));
		
		//Recursive search of directories
		Arrays.stream(directory).filter(File::isDirectory).map(File::getPath)
				.forEach(nextPath -> search(nextPath, word));
	}
	
	/**
	 *Word search.
	 * <p>
	 *Separate the characters to be searched with a space,OR search.
	 * <p>
	 *If the file name contains one of the words{@code ture}
	 * 
	 * @param file filename
	 * @param word word to search
	 * @If you include return{@code true}
	 */
	private static boolean isMatch(String file, String word) {
		
		List<String> stock = new LinkedList<>();
		
		//Separated by whitespace
		for (String e : word.split(" ")) {
			// \\Separated by
			for (String e2 : e.split("\\\\")) {
				stock.add(e2);
			}
		}
		
		//True if it contains a string
		return stock.stream().map(String::trim).anyMatch(spell -> file.contains(spell));
	}

	/**
	 *Check for null blanks.
	 * <ul>
	 *Check contents
	 * <li>The argument is{@code null}For an array of{@code false}
	 * <li>If the following is satisfied for all the elements of the argument{@code true}
	 * <ul>
	 * <li>{@code null}Not
	 * <li>{@code String}Not empty string when converted
	 * <li>{@code String}Not a whitespace character when converted
	 * </ul>
	 *If even one gets caught{@code false}
	 * </ul>
	 * 
	 * @param obj {@code null}Object array that allows
	 * @return check availability
	 */
	private static boolean validate(Object...obj) {
		return obj == null ? false
				: Arrays.stream(obj)
						.filter(Objects::nonNull)
						.map(Object::toString)
						.filter(nonEmpty)
						.filter(nonBlank)
						//True if all is OK
						.count() == obj.length ? true : false;
	}
	
	/**True if not empty*/
	private static final Predicate<String> nonEmpty = str -> !str.equals("");
	
	/**True if only blank*/
	private static final Predicate<String> nonBlank = v -> v != null && !v.equals("")
			&& Arrays.stream(v.split("")).anyMatch(x -> !x.equals(" ") && !x.equals(" "));
}

It's cheap that the output is a console OR search for multiple words separated by spaces Well, the search process is also a little appropriate, so please don't dig into the ʻisMatchmethod. .. I could have just usedString.contains`, but I wonder if it is necessary to devise various things such as web systems.

The validate part is also adapted to this process, so it may not be said to be versatile.

So the main subject

search.java


	/**
	 *Search the directory by string.
	 * <p>
	 *While outputting the absolute path of the file and directory with the name including the search string
	 *Search recursively.
	 * 
	 * @param dir Search target directory
	 * @param word Search string
	 */
	private static void search(String dir, String word) {
		
		//Get the list in the directory
		File[] directory = new File(dir).listFiles();
		
		//If the object does not exist,End processing
		if (directory == null) {
			return;
		}
		
		//Outputs absolute path for those containing search words
		Arrays.stream(directory).map(File::getName).filter(file -> isMatch(file, word))
				.forEach(file -> System.out.println(dir + "\\" + file));
		
		//Recursive search of directories
		Arrays.stream(directory).filter(File::isDirectory).map(File::getPath)
				.forEach(nextPath -> search(nextPath, word));
	}

This is interesting for recursive search

Find the one in the directory from the list Get the path Do the same with it as an argument

If there is no return statement in the middle, it will be an infinite loop. Is it a while statement that does not use while?

Calling yourself is really interesting!

Recommended Posts

I tried something called recursive search
I tried to solve AOJ's Binary Search
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried FizzBuzz.
I tried JHipster 5.1
[API] I tried using the zip code search API
[I tried] Spring tutorial
I tried running Autoware
I tried using Gson
I tried QUARKUS immediately
I tried using TestNG
I tried Spring Batch
I tried using Galasa
I tried node-jt400 (Programs)
[Java] I tried to implement Yahoo API product search
I tried node-jt400 (execute)
I tried node-jt400 (Transactions)
[Android] [Library] I tried using an animation library called "Before After animation".
I tried node-jt400 (Environment construction)
I tried DI with Ruby
I tried node-jt400 (IFS write)
I tried node-jt400 (SQL Update)
I tried using azure cloud-init
I tried Spring State machine
I tried Drools (Java, InputStream)
I tried Rails beginner [Chapter 1]
I tried the Docker tutorial!
I tried using Apache Wicket
I tried the VueJS tutorial!
I tried node-jt400 (SQL query)
I tried using Java REPL
I tried source code analysis
I tried the FizzBuzz problem
I tried node-jt400 (SQL stream)
I tried node-jt400 (IFS read)
I tried putting XcodeGen + SwiftPM
I tried Rails beginner [Chapter 2]
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried to verify yum-cron
I tried Jets (ruby serverless)
I tried metaprogramming in Java