-1

I want to revert these Lambda Expressions:

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

While keeping the same performance discussed in the source. Here's the original method:

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

I'd like to revert them similar to 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"); 
});

And here's the best solution discussed from the source:

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)));
    }
}
Community
  • 1
  • 1
Everlight
  • 431
  • 5
  • 18
  • What do you mean by "revert them"? – Tunaki Feb 03 '16 at 21:50
  • Basically don't use Lambda Expressions so that I can use Javassist 3.18.1-GA instead of updating to version 3.18.2-GA and/or updating hibernate. The error I'm getting: http://stackoverflow.com/questions/30313255/reflections-java-8-invalid-constant-type – Everlight Feb 03 '16 at 21:56
  • If you must avoid lambda expressions, why bother with streams API at all? Do this the regular java 7 way. Make a `List`, sort it using `Collections.sort` and then make a list of filenames. – Misha Feb 03 '16 at 22:03
  • Because I'm working with a bulky system that needs to be faster due to my requirements. If I can't use stream APIs and not Lambda Expressions then I'll cut down on time elsewhere. – Everlight Feb 03 '16 at 22:09
  • Why? Have you actually measured this? – Tunaki Feb 03 '16 at 22:23
  • Why what? Have I measured what? I haven't measured anything, I'm assuming the folks in the "source" link I posted above are providing accurate times. – Everlight Feb 03 '16 at 22:27
  • 2
    So you're asking how to convert a lambda expression to an anonymous inner class? Just ask your IDE to do it. – Holger Feb 03 '16 at 23:31
  • I didn't know my IDE could do that until Tagir posted his answer. I simply Import my group's Eclipse-Luna code formatter configuration xml sheets then am told to never edit those configurations. – Everlight Feb 04 '16 at 16:22

1 Answers1

3

To convert all lambdas to anonymous classes you may use the IDE features. Most modern Java IDEs support this. For example, in Eclipse right-click on the project, navigate to

Source -> Clean Up -> Use custom profile -> Configure... -> Code Style -> Functional interface instances -> [x] Convert functional interface instances -> [x] Use anonymous class:

Eclipse Source Clean Up

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334