繁体   English   中英

在我的实现中,我希望该方法具有不同的返回类型

[英]In my implementation I want to have different return type of the method

我有以下问题。 我有接口:

public interface Parser {
    public Map<String, List<String>> parse() throws IOException;
}

我有两个实现:

public class RacerInfoParser implements Parser{

    private final Path path;

public RacerInfoParser(Path path) {
    this.path = path;   
}

@Override
public Map <String, List<String>> parse() throws IOException {
    try (Stream<String>lines = Files.lines(path)){
        
        Map <Object, Object> map = lines.collect(Collectors.toMap(
                string -> string.substring(0,3),
                string -> Arrays.asList(string.substring(4).split("_"))));
        
        Map<String, List<String>> result = new HashMap<>((Map) map);
        return result;
        }
    }
}

public class TimeParser implements Parser {

    private final Path path;

public TimeParser(Path path) {
    this.path = path;   
}


@Override
public Map <String, List<String>> parse() throws IOException {
    try (Stream<String>lines = Files.lines(path)){
        
        Map <Object, Object> map = lines.collect(Collectors.toMap(
                string -> string.substring(0,3),
                string -> Arrays.asList(string.substring(3).split("_"))));
        Map<String, List<String>> result = new HashMap<>((Map) map);
    return result;
        }
    }
}

我想要做的是更改 TimeParser 的代码和返回类型,使其返回Map<String, List <LocalTime>类型的结果。 我已经读过,为了有一个不同的类型,我需要有一个父类型的子类,但我不明白在我的情况下如何做。

PS我知道Map<String, List<String>> result = new HashMap<>((Map) map); 是一个糟糕的代码,但我还不知道如何正确地将Map<Object, Object转换为Map<String, List<String>> 如果您有任何建议,我将很乐意听取他们的意见:)。

PSS 我使用这两个实现,因为我相信它们做同样的事情:从日志和 txt 文件解析文本:

    public class RacerBuilder {
        public List<Racer> buildRacers () throws URISyntaxException, IOException {
    
            Parser racerInfoParser = new RacerInfoParser(Paths.get(getClass().getClassLoader()
                      .getResource("abbreviations.txt").toURI()));
            Parser startTimeParser = new TimeParser(Paths.get(getClass().getClassLoader()
                      .getResource("start.log").toURI()));
            Parser endTimeParser = new TimeParser(Paths.get(getClass().getClassLoader()
                      .getResource("end.log").toURI()));
            
            Map<String, List<String>> racerInfoMap = racerInfoParser.parse();
            Map<String, List<String>> startTimeMap = startTimeParser.parse();
            Map<String, List<String>> endTimeMap = endTimeParser.parse();
    
            return racerInfoMap.keySet().stream()
                    .map(i -> new Racer (i,
                            racerInfoMap.get(i).get(0),
                            racerInfoMap.get(i).get(1),
                            startTimeMap.get(i).get(1),
                            endTimeMap.get(i).get(1),
                            endTimeMap.get(i).get(0)))
                    .collect(Collectors.toList());  
           }
}

Racer 类现在有几个字段,它们都是字符串。 我希望它有 2 个 LocalTime 类型的字段。

接受:

Map<String, List<String>>

或者

Map<String, List<LocalTime>>

在这种情况下,您可以使用泛型,您只需要使用:

<T> Map<String, List<T>> parse() throws IOException;
                     ^

您的代码也可以是:

return lines.collect(Collectors.toMap(
        string -> string.substring(0, 3),
        string -> Arrays.asList(string.substring(4).split("_"))));

或者,如果您想要一个 LocalTime 列表,您可以解析您的字符串并按以下方式收集:

return lines.collect(Collectors.toMap(
        string -> string.substring(0, 3),
        string -> Arrays.stream(string.substring(4).split("_"))
        .map(LocalTime::parse) // or you can use a Date time formatter
        .collect(Collectors.toList())

您不需要使用此解决方案进行转换。

我将Map<String, List<String>>包装在一个带有 getter 的新类中,我们称之为MapAsString 使其成为类层次结构的一部分,以便您拥有class MapAsString extends DataMap 接下来有一个新类,它是DataMap的子类,可能称为MapAsLocalTime ,其中MapAsLocalTime extends DataMap

加分项:使您的父class DataMap抽象并提供您必须实现的单个抽象方法。 使用泛型返回List<String, T> 你可以有一个构造函数,它接受一个T (泛型类型),它定义了在构造时T类型。 如果这看起来太难了,也许只是使用通配符返回任何内容? ...所以 getter 返回List<String, ?> - 这里? 可以是任何类型的对象

你可以使用泛型 T。像这样

interface Parser<T> {
    public Map<String, List<T>> parse() throws IOException;
}

class RacerInfoParser implements Parser<String>{

    private final Path path;

    public RacerInfoParser(Path path) {
        this.path = path;   
    }
    
    @Override
    public Map <String, List<String>> parse() throws IOException {
        try (Stream<String>lines = Files.lines(path)){
            
            Map <String,  List<String>> map = lines.collect(Collectors.toMap(
                    string -> string.substring(0,3),
                    string -> Arrays.asList(string.substring(4).split("_"))));
            return map;
            }
        }
    }

class TimeParser implements Parser<LocalTime> {

    private final Path path;

public TimeParser(Path path) {
    this.path = path;   
}


@Override
public Map <String, List<LocalTime>> parse() throws IOException {
    try (Stream<String>lines = Files.lines(path)){
        
        Map<String, List<LocalTime>> result = lines.collect(Collectors.toMap(
                string -> string.substring(0,3),
                string -> Arrays.stream(string.substring(4).split("_"))
                .map(LocalTime::parse)
                .collect(Collectors.toList())
                ));
            return result;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM