简体   繁体   English

Java,如何使它成为通用方法?

[英]Java, how to make this into a generic method?

How would I refactor the code below into 1 generic method? 如何将以下代码重构为1个通用方法?
(background info, it is used to get values from a Json string used with GSON library) (背景信息,它用于从与GSON库一起使用的Json字符串中获取值)

ArrayList<Map<Object, Object>> theme = new ArrayList<Map<Object, Object>>();
for (int i = 0; i < obj.getThemeList().size(); i = i + 1) {
    if(Boolean.parseBoolean(obj.getThemeList().get(i).getChecked())){
        Map<Object,Object> map = new HashMap<Object,Object>();
        map.put("id", obj.getThemeList().get(i).getId());
        map.put("name", obj.getThemeList().get(i).getName());
        theme.add(map);
    }
}

ArrayList<Map<Object, Object>> tag = new ArrayList<Map<Object, Object>>();
for (int i = 0; i < obj.getTagList().size(); i = i + 1) {
    if(Boolean.parseBoolean(obj.getTagList().get(i).getChecked())){
        Map<Object,Object> map = new HashMap<Object,Object>();
        map.put("id", obj.getTagList().get(i).getId());
        map.put("name", obj.getTagList().get(i).getName());
        tag.add(map);
    }
}

Basically, just make it a single method that accepts the getThemeList() or getTagList() , no? 基本上,只需将其getThemeList()接受getThemeList()getTagList()的单个方法getTagList() ,不是吗? It looks like that's the only difference between them... 看来这是它们之间的唯一区别...

I think you mean "how can I refactor this to reuse code", because generics does not apply here as much as refactoring does. 我认为您的意思是“如何重构它以重用代码”,因为泛型在这里的应用不如重构。

Firstly, read up on the foreach syntax - your for loop is really ugly. 首先,阅读foreach语法 -您的for循环确实很丑陋。

You haven't given much to go on, but try this: 您没有什么可以继续的,但是请尝试以下操作:

public interface HasIdNameAndChecked {
    String getChecked();
    String getId();
    String getName();
}

public static List<Map<String, String>> extractList(List<? extends HasIdNameAndChecked> items) {
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    for (HasIdNameAndChecked item : items) {
        if (Boolean.parseBoolean(item.getChecked())){
            Map<String, String> map = new HashMap<String, String>();
            map.put("id", item.getId());
            map.put("name", item.getName());
            list.add(map);
        }
    }
    return list;
}

Then have your Theme and Tag classes implement HasIdNameAndChecked , and call it like this: 然后让您的ThemeTag类实现HasIdNameAndChecked ,并像这样调用它:

List<Map<String, String>> list1 = extractThemeList(obj.getThemeList());
List<Map<String, String>> list2 = extractThemeList(obj.getTagList());

Disclaimer: I typed this in without an IDE, so there could be a couple of typos. 免责声明:我在没有IDE的情况下输入了此信息,因此可能会有一些错别字。

Well, let's do it in following order: 好吧,让我们按以下顺序进行操作:

  1. Identify what varies. 找出差异。 themeList and tagList. themeList和tagList。

  2. Identify common properties and behaviour. 识别共同的特性和行为。 getChecked(), getId(), getName(). getChecked(),getId(),getName()。

  3. Apply generalization for what varies. 对变化进行概括。 Define a common interface for what varies: abstract class, interface, behaviour, etc. 定义各种变化的通用接口:抽象类,接口,行为等。

  4. Update your solution to generalized solution. 将您的解决方案更新为广义解决方案。

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

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