通执行时间,把他们在多头排列
我在Java和 初学者我想要做的Java 8如下:通执行时间,把他们在多头排列
- 计算的执行时间我方法 - 具有
System.nanoTime
, - 打发时间作为流参数我的比较方法,
- 将这些值代入从长阵列[2]〜[5],并返回该数组
我不知道如何实现这一点。我尝试了不同的方式,但我遇到了错误,并没有任何答案。以下是我迄今所做的:
import java.util.stream.Stream; import java.io.*;
import java.nio.file.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.lang.Object;
import java.util.stream.IntStream;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
*/
public class WinneropsDB implements Winner{
private int getYear;
private int getWinnerAge;
private String getWinnerName;
private String getFilmTitle;
public WinneropsDB(String s) {
String[] Data = s.split(",",5);
getYear = Integer.parseInt(Data[1]);
getWinnerAge = Integer.parseInt(Data[2]);
getWinnerName = Data[3].substring(1,Data[3].length()-1);
getFilmTitle = Data[4].substring(1,Data[4].length()-1);
}
@Override
public String toString(){
return getYear+", "+getWinnerAge+", "+getWinnerName+", "+getFilmTitle;
}
public static Stream<Winner> loadData(String[] fileName) {
return Arrays.stream(fileName).flatMap(f -> {
try {
return Files.lines(Paths.get(f))
.filter(first -> !first.startsWith("#"))
.map(WinneropsDB:: new);
} catch (IOException e) {
System.out.println("file not found");
return null;
}
});
}
/**
*
* @param young
* @return
*/
public static Stream<Winner> youngWinners(Stream<Winner> young) {
return young.filter(w -> w.getWinnerAge() < 35)
.sorted(Comparator.comparing(Winner :: getWinnerName));
}
public static Stream<Winner> extreamWinners (Stream<Winner> mix){
long startTime = System.nanoTime();
Comparator<Winner> comparator = Comparator.comparing(Winner -> Winner.getWinnerName());
Stream<Winner> m = mix.sorted(comparator);
//Winner[] g = (Winner[]) m.toArray();
//System.out.println(g[0]);
Comparator<Winner> comparator1 = Comparator.comparing(Winner -> Winner.getWinnerAge());
Stream<Winner> m1 = m.sorted(comparator1);
Winner[] lis = m1.toArray(s -> new Winner[s]);
Winner youngest = lis[0];
Winner oldest = lis[lis.length - 1];
Arrays.stream(lis).forEach(System.out::println);
System.out.println("youngest: " + youngest + " oldest: " + oldest);
long finishTime = System.nanoTime();
long timeWinner = finishTime - startTime;
System.out.println("timeWinner: " + timeWinner);
comparison(Stream.of(timeWinner));
return m1;
}
public static Stream<String> multiAwardedPerson(Stream<Winner> m){
long startTime = System.nanoTime();
Comparator<Winner> comprator = Comparator.comparing(Winner -> Winner.getWinnerName());
Map<String,List<Winner>> c1 = m.collect(Collectors.groupingBy(Winner::getWinnerName));
//, Collectors.counting()));
long finishTime = System.nanoTime();
long timePerson = finishTime - startTime;
System.out.println("timePerson: " + timePerson);
comparison(Stream.of(timePerson));
return c1.values().stream().filter(s -> s.size()>= 2).map(e -> e.get(0)).sorted(comprator).map(f -> f.getWinnerName());
}
public static Stream<String> multiAwardedFilm(Stream<Winner> d){
long startTime = System.nanoTime();
Comparator<Winner> comprator = Comparator.comparing(Winner -> Winner.getYear());
Map<Integer,List<Winner>> c1 = d.collect(Collectors.groupingBy(Winner::getYear));
long finishTime = System.nanoTime();
long timeFilm = finishTime - startTime;
System.out.println(" timeFilm: " + timeFilm);
comparison(Stream.of(timeFilm));
return c1.values().stream().filter(f -> f.size() == 2).map(t -> t.get(0)).sorted(comprator).map(y -> y.getFilmTitle());
}
public static <T, U> long measure(Function<Stream<T>,Stream<U>> f , Stream<T> s1){
long startTime = System.nanoTime();
Stream<U> s2 = f.apply(s1);
List<U> collect = s2.collect(Collectors.toList());
long endTime = System.nanoTime();
long time = endTime - startTime;
return time;
}
public static long[] comparison(Stream<Winner> e){
long [] arrayTime = LongStream.of(e).toArray();
return null;
}
/**
* @param args the command line arguments
* @throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
String[] fileName = {"G:\\path\\oscar_age_female.csv" , "G:\\path\\oscar_age_male.csv"};
// TODO code application logic here
Stream<Winner> loadData = loadData(fileName);
//loadData.forEach(x -> System.out.println(x));
// Stream<Winner> young = youngWinners(loadData);
// young.forEach(a -> System.out.println(a));
Stream<Winner> mix1 = extreamWinners(loadData);
Stream<String> winner = multiAwardedPerson(loadData);
//winner.forEach(q -> System.out.println(q));
Stream<String> date = multiAwardedFilm(loadData);
//date.forEach(l -> System.out.println(l));
}
@Override
public int getYear() {
return getYear;
}
@Override
public int getWinnerAge() {
return getWinnerAge;
}
@Override
public String getWinnerName() {
return getWinnerName;
}
@Override
public String getFilmTitle() {
return getFilmTitle;
}
}
回答:
,我发现我的问题的答案,但我不知道为什么我没有执行时间。如果任何结果,你有什么想法,请让我知道
import java.util.stream.Stream; import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
*/
public class WinneropsDB implements Winner{
private int getYear;
private int getWinnerAge;
private String getWinnerName;
private String getFilmTitle;
public WinneropsDB(String s) {
String[] Data = s.split(",",5);
getYear = Integer.parseInt(Data[1]);
getWinnerAge = Integer.parseInt(Data[2]);
getWinnerName = Data[3].substring(1,Data[3].length()-1);
getFilmTitle = Data[4].substring(1,Data[4].length()-1);
}
@Override
public String toString(){
return getYear+", "+getWinnerAge+", "+getWinnerName+", "+getFilmTitle;
}
public static Stream<Winner> loadData(String[] fileName) throws IOException{
return Arrays.stream(fileName).flatMap(f -> {
try {
return Files.lines(Paths.get(f))
.filter(first -> !first.startsWith("#"))
.map(WinneropsDB:: new);
} catch (IOException ex) {
System.out.println("file not found");
return null;
}
});
}
/**
*
* @param young
* @return
*/
public static Stream<Winner> youngWinners(Stream<Winner> young) {
return young.filter(w -> w.getWinnerAge() < 35)
.sorted(Comparator.comparing(Winner :: getWinnerName));
}
public static Stream<Winner> extreamWinners (Stream<Winner> mix){
Comparator<Winner> comparator = Comparator.comparing(Winner -> Winner.getWinnerName());
Stream<Winner> m = mix.sorted(comparator);
// Winner[] g = (Winner[]) m.toArray();
//System.out.println(g[0]);
Comparator<Winner> comparator1 = Comparator.comparing(Winner -> Winner.getWinnerAge());
Stream<Winner> m1 = m.sorted(comparator1);
Winner[] lis = m1.toArray(s -> new Winner[s]);
Winner youngest = lis[0];
Winner oldest = lis[lis.length - 1];
Arrays.stream(lis).forEach(System.out::println);
System.out.println("youngest: " + youngest + " oldest: " + oldest);
return m1;
}
public static Stream<String> multiAwardedPerson(Stream<Winner> m){
Comparator<Winner> comprator = Comparator.comparing(Winner -> Winner.getWinnerName());
Map<String,List<Winner>> c1 = m.collect(Collectors.groupingBy(Winner::getWinnerName)); //, Collectors.counting()));
return c1.values().stream().filter(s -> s.size()>= 2).map(e -> e.get(0)).sorted(comprator).map(f -> f.getWinnerName());
}
public static Stream<String> multiAwardedFilm(Stream<Winner> d){
Comparator<Winner> comprator = Comparator.comparing(Winner -> Winner.getYear());
Map<Integer,List<Winner>> c1 = d.collect(Collectors.groupingBy(Winner::getYear));
return
c1.values().stream().filter(f -> f.size() == 2).map(t -> t.get(0)).sorted(comprator).map(y -> y.getFilmTitle());
}
public static <T, U> long measure(Function<Stream<T>,Stream<U>> f , Stream<T> s1){
Stream<U> s2 = f.apply(s1);
long startTime = System.nanoTime();
s2.collect(Collectors.toList());
long endTime = System.nanoTime();
return endTime - startTime;
}
public static <T, U> LongStream runjobs(Stream<Function<Stream<T>,Stream<U>>> Jobs,Stream<T> s) {
List<T> Data = s.collect(Collectors.toList());
return Jobs.mapToLong(i -> measure(i, Data.stream()));
}
public static long[] comparison(Stream<Winner> e){
long [] result = new long[5];
List<Winner> winnerList = e.collect(Collectors.toList());
result[2] = measure(WinneropsDB:: youngWinners, winnerList.stream());
result[3] = measure(WinneropsDB:: multiAwardedPerson, winnerList.stream());
result[4] = measure(WinneropsDB:: multiAwardedFilm, winnerList.stream());
result[5] = measure(WinneropsDB:: extreamWinners, winnerList.stream());
// for(long l: result){
//
// System.out.println(l);
// }
return result;
}
/**
* @param args the command line arguments
* @throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws IOException {
String[] fileName = {"G:\\path\\oscar_age_female.csv" , "G:\\path\\oscar_age_male.csv"};
// TODO code application logic here
Stream<Winner> loadData = loadData(fileName);
// loadData.forEach(x -> System.out.println(x));
// Stream<Winner> young = youngWinners(loadData);
// young.forEach(a -> System.out.println(a));
// Stream<Winner> mix1 = extreamWinners(loadData);
// Stream<String> winner = multiAwardedPerson(loadData);
// winner.forEach(q -> System.out.println(q));
// Stream<String> date = multiAwardedFilm(loadData);
// date.forEach(l -> System.out.println(l));
List<Function<Stream<Winner>,Stream<Winner>>> Jobs = new ArrayList<>();
// Jobs.add(WinneropsDB::youngWinners);
// Jobs.add(WinneropsDB::youngWinners);
// runjob(Job.stream(), loadData).forEach(System.out::println);
long [] c = comparison(loadData);
for(int i = 0; i< c.length; i++){
System.out.println(c[i]);
}
}
@Override
public int getYear() {
return getYear;
}
@Override
public int getWinnerAge() {
return getWinnerAge;
}
@Override
public String getWinnerName() {
return getWinnerName;
}
@Override
public String getFilmTitle() {
return getFilmTitle;
}
}
以上是 通执行时间,把他们在多头排列 的全部内容, 来源链接: utcz.com/qa/266186.html