简体   繁体   English

如何提高Java中用于语句的低效率重复项

[英]How to improve inefficient duplicated for statement in java

I am using a duplicate for statement for some reason, but somehow I feel inefficient. 由于某种原因,我使用重复的语句,但是以某种方式我感觉效率低下。 The reasons why I use duplicated for statement is below. 我使用重复的语句的原因如下。

  • I got 'channel' data, which is a List of Channel information 我得到了“频道”数据,这是频道信息列表
  • Channel Information is ' Map<String, Object> ' type which has many elements 频道信息是“ Map<String, Object> ”类型,其中包含许多元素
  • There is another list of Map<String, Object> which represent program information 还有另一个Map<String, Object>列表,它们表示程序信息
  • What I need to do in insert a program in to each channels 在每个频道中插入程序时我需要做什么

Source code is below 源代码如下

public static void injectProgram2Channel (
            List<Map<String, Object>> channelList, List<Map<String, Object>> programList) throws Exception {
        for(Map<String, Object> channel : channelList){
            String serviceId = channel.get("serviceId").toString();
            ArrayList<Map<String, Object>> programsForChannel = new ArrayList<Map<String,Object>>();
            for(Map<String, Object> program : programList){
                if(serviceId.equals(program.get("serviceId").toString())){
                    programsForChannel.add(program);
                }
            }
            channel.put("programs", programsForChannel);
        }
    }

I do not want to iterate all of this List, since each List has many elements. 我不想迭代所有此列表,因为每个列表都有很多元素。 How can I improve its performance? 如何改善其性能?

  • Channel List has around 500 Elements 频道列表约有500个元素
  • Program List has around 2000 Elements 节目表约有2000个元素

(I need solution which does not using duplicated for statement) (我需要不使用重复的语句的解决方案)

You need to ask yourself the question: does it matter how fast it is. 您需要问自己一个问题:多快才重要。 If it doesn't, don't make code more complex. 如果没有,请不要使代码更复杂。 "Premature optimization is the root of all evil". “过早的优化是万恶之源”。

Once you have confirmed that performance is relevant, try to create a benchmark eg using JMH and with that in place, start to optimise. 一旦确认性能是相关的,就尝试创建一个基准,例如使用JMH,并建立适当的基准,开始进行优化。

Channel List has around 500 Elements
Program List has around 2000 Elements

Those are really small in real world applications. 在实际应用中,这些功能确实很小。 You shouldn't worry if you have only two nested for loops for that amount of data. 如果您只有两个嵌套的for循环用于该数量的数据,则不必担心。

As hinted a possible option that may or may not make sense in your case would be to just group the programs by serviceId into a new Map<String, List<Map<String, Object>>> and lookup the programs when needed. 作为暗示一种可能的选择, 可能会可能没有意义,你的情况是只由组服务Id程序到一个新的Map<String, List<Map<String, Object>>>并在需要时查找的程序

public static Map<String, List<Map<String, Object>>> buildProgramList (
        List<Map<String, Object>> programList) throws Exception {

    Map<String, List<Map<String, Object>>> programsByServiceId = new HashMap<>();

    for(Map<String, Object> program : programList){
        String serviceId = program.get("serviceId").toString();
        if(programsByServiceId.containsKey(serviceId)){
            programsByServiceId.get(serviceId).add(program);
        } else {
            programsByServiceId.put(serviceId,
                    new ArrayList<Map<String, Object>>(){{
                add(program);
            }});
        }
    }
    return programsByServiceId;
}

Once this structures is built, just retrieve your programs for a channel by calling 建立此结构后,只需调用以下方法即可检索频道程序

List<Map<String, Object>> programs = programsByServiceId.get(channel.get("serviceId").toString());

And if you want to get extra fancy you could lazy load the results in to the channel . 而且,如果您想获得更多的花哨,可以将结果懒惰地加载到通道中 Something like 就像是

if (channel.get("programs") == null) 
    channel.put("programs", programsByServiceId.get(channel.get("serviceId").toString());
return channel.get("programs");

This would defer the upfront cost and spread it out. 这将推迟前期成本并将其摊开。

When this might make sense. 可能有意义。 If there are certain channels that are looked up more often than others for their program list. 是否有某些频道比其他频道更频繁地查找其节目列表。

When this does not make sense. 当这没有意义时。 All of the channels are requested their list of programs immediately or soon after. 所有通道都要求其立即或不久后的程序列表。 (like if you are just going to serialize all of the channels and their programs). (例如,如果您只是要序列化所有通道及其程序)。

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

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