如何还原此Lambda表达式?

时间:2016-02-03 21:47:24

标签: lambda java-8

我想还原这些Lambda表达式:

PathInfo::new
p ->   p.getFileName()

同时保持source中讨论的相同性能。这是原始方法:

public List<String> getSortedFile(Path dir) 
  throws IOException 
{
    return Files.list(dir).map(PathInfo::new).sorted()
        .map(p -> p.getFileName()).collect(Collectors.toList());
}

我想将它们归还how Javin Paul did it in this article

// Before Java 8: 
JButton show = new JButton("Show"); 
show.addActionListener(new ActionListener() 
{ 
  @Override 
  public void actionPerformed(ActionEvent e) 
  { 
    System.out.println("Event handling without lambda expression is boring"); 
  } 
}); 

// Java 8 way: 
show.addActionListener((e) -> { 
  System.out.println("Light, Camera, Action !! Lambda expressions Rocks"); 
});

这是从源头讨论的最佳解决方案:

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FileSorter {

    public List<String> getSortedFile(Path dir) throws IOException {
        return Files.list(dir).map(PathInfo::new).sorted().map(p -> p.getFileName()).collect(Collectors.toList());
    }

    static class PathInfo implements Comparable<PathInfo> {
        private final String fileName;
        private final long modified;
        private final long size;

        public PathInfo(Path path) {
          try {
            // read the whole attributes once
            BasicFileAttributes bfa = Files.readAttributes(path, BasicFileAttributes.class);
            fileName = path.getFileName().toString();
            modified = bfa.lastModifiedTime().toMillis();
            size = bfa.size();
          } catch (IOException ex) {
            throw new UncheckedIOException(ex);
          }
        }

        @Override
        public int compareTo(PathInfo o) {
            int cmp = Long.compare(modified, o.modified);
            if (cmp == 0)
                cmp = Long.compare(size, o.size);
            return cmp;
        }

        public String getFileName() {
            return fileName;
        }
    }

    public static void main(String[] args) throws IOException {
        // File (io package)
        File f = new File("C:\\Windows\\system32");
        // Path (nio package)
        Path dir = Paths.get("C:\\Windows\\system32");

        FileSorter fs = new FileSorter();

        long before4 = System.currentTimeMillis();
        List<String> names4 = fs.getSortedFile(dir);
        long after4 = System.currentTimeMillis();
        System.out.println("Run time of Stream.sorted as List with caching: " + ((after4 - before4)));
    }
}

1 个答案:

答案 0 :(得分:2)

要将所有lambdas转换为匿名类,您可以使用IDE功能。大多数现代Java IDE都支持这一点。例如,在Eclipse中右键单击项目,导航到

来源 - &gt;清理 - &gt;使用自定义配置文件 - &gt;配置... - &gt;代码风格 - &gt;功能接口实例 - &gt; [x]转换功能接口实例 - &gt; [x]使用匿名类:

Eclipse Source Clean Up