小编mar*_*122的帖子

如何摆脱不必要的代码?-适应DRY原理

不久前,我遇到了类似的话题。我正在看我的应用程序,我认为它有很多不必要的代码。我的意思是,我拥有负责从两个书店中的不同类别的书中抓取数据的服务。现在我有5个类别,所以我有5个方法,但是如果我要添加一些新类别呢?我将不得不添加更多方法...,我认为这不是一个好选择。现在看起来像这样:

控制者

@GetMapping("/romances")
    public Map<Bookstore, List<Book>> get15RomanticBooks() {
        return categorizedBookService.get15BooksFromRomanceCategory();
    }

    @GetMapping("/biographies")
    public Map<Bookstore, List<Book>> get15BiographiesBooks() {
        return categorizedBookService.get15BooksFromBiographiesCategory();
    }

    @GetMapping("/guides")
    public Map<Bookstore, List<Book>> get15GuidesBooks() {
        return categorizedBookService.get15BooksFromGuidesCategory();
    }

    @GetMapping("/fantasy")
    public Map<Bookstore, List<Book>> get15FantasyBooks() {
        return categorizedBookService.get15BooksFromFantasyCategory();
    }
Run Code Online (Sandbox Code Playgroud)

在这里我在想

@GetMapping("/{category}")
public Map<......> get 15BooksFromCategory(@PathVariable CategoryType category)
{...}
Run Code Online (Sandbox Code Playgroud)

我认为这是最好的方法,但是使用服务很难。

服务如下:

package bookstore.scraper.book.scrapingtypeservice;

import bookstore.scraper.enums.Bookstore;
import bookstore.scraper.book.Book;
import bookstore.scraper.fetcher.empik.EmpikFetchingBookService;
import bookstore.scraper.fetcher.merlin.MerlinFetchingBookService;
import bookstore.scraper.urlproperties.EmpikUrlProperties;
import bookstore.scraper.urlproperties.MerlinUrlProperties;
import bookstore.scraper.utilities.JSoupConnector;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public …
Run Code Online (Sandbox Code Playgroud)

java dry solid-principles

5
推荐指数
1
解决办法
125
查看次数

如何找到以给定字母开头的单词

以给定的字符串为例:例如,My name is James Bond iee我想要Map将以character为键,将字符串集作为值(该字符串集是从相应字符键开头的字符串)- Map<Character,Set<String>>。例如:

m - my, n - name, i - is, iee, j - james etc.

我已经从标点符号和用于查找字符的书面方法中清除了字符串。在我的示例中:(m,n,u,j,b)

这些方法:

static String getPurifiedSentenceFromPunctuationMarks(String sentence) {
    return sentence.replaceAll("\\p{P}", "");
}

static Set<Character> getEveryCharacterOccuringInSentence(String sentence) {
    return sentence.chars()
                .mapToObj(e -> (char) e).collect(Collectors.toSet());
}
Run Code Online (Sandbox Code Playgroud)

现在,我想遍历每个字符和每个字符串以找到这些单词,但是我在为每个字符创建集合方面感到挣扎:

String purifiedSentenceFromPunctuationMarks = AP.getPurifiedSentenceFromPunctuationMarks(sentence);
        Set<Character> characters = AP.getEveryCharacterOccuringInSentence(purifiedSentenceFromPunctuationMarks);
        String[] words = purifiedSentenceFromPunctuationMarks.split("\\s");
        Map<Character, Set<String>> characterWithAccordingWord = new LinkedHashMap<>();
        Set<String> nonRepeatableWords = new HashSet<>();

        for (char iteratedCharacter : characters) { …
Run Code Online (Sandbox Code Playgroud)

java set java-stream

2
推荐指数
1
解决办法
65
查看次数

标签 统计

java ×2

dry ×1

java-stream ×1

set ×1

solid-principles ×1