Examples of anonymous classes in Java

Source: Internet
Author: User

This article uses regular expressions to filter some special files in the folder and print them out. This involves the file filter, which is applied to the anonymous class.

Package date0815.demo1;/** * Java anonymous inner class * @author ZZW * */import java.io.file;import java.io.filenamefilter;import java.ut Il. Arrays;import Java.util.regex.pattern;public class Test1 {public static filenamefilter filter (final String regex) {//anonymous class , directly using an anonymous class to return an object of the FilenameFilter class return new FilenameFilter () {//compile the regular expression with the private pattern pattern = pattern.compile ( regex);//Overwrite the only accept method in the FilenameFilter interface @overridepublic Boolean accept (File dir, String name) {return Pattern.matcher (name). matches ();}};} public static void Main (string[] args) {//Create a new File = "."); /returns the filtered list of files, using regular expressions to filter all files ending with. html string[] fileList = file.list (Filter (". *\\.html"));//Case-insensitive ordering of strings Arrays.sort (FileList, String.case_insensitive_order);//print file name for (String str:filelist) {System.out.println (str);}}}





Examples of anonymous classes in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.