有没有办法提升以下代码的性能?

huangapple go评论77阅读模式
英文:

Is there any way to improve the performance of the below code?

问题

代码正在比较两个`list`的代码第一个`list`来自api调用第二个来自数据库我使用两个循环遍历这两个list并进行比较将共同的部分添加到一个新的list中第一个list大约包含800个数据而第二个list来自数据库包含150个数据有没有办法提高这段代码的性能我不能对`AllowedCodes Class`进行任何更改在给定数据量的情况下嵌套循环会影响性能吗

public class AllowedCodes {

    private String codeValue = "";

    public String getCodeValue() {
        return codeValue;
    }

    public void setCodeValue(String codeValue) {
        this.codeValue = codeValue;
    }
}

public class CheckCodes {

    public static void main(String[] args) {

        List<AllowedCodes> old_codes_list = getListOfOldCodes();

        List<AllowedCodes> new_codes_list = new ArrayList<>();

        String sql = "这个查询从数据库获取代码";

        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet result = statement.executeQuery();

        while (result.next()) {

            for (AllowedCodes a : old_codes_list) {
                if (a.getCodeValue().equalsIgnoreCase(result.getCodeValue())) {
                    new_codes_list.add(a);
                }
            }

        }
    }
}
英文:

The codes is comparing 2 list of codes.First list is got from api call and second from database.I am using 2 loops to iterate over the list and compare them ,and add the common to a new list.The first list contains around 800 data and second list(from db) contains 150 data.Is there any way to improve the performance of this code.I am not allowed to make any changes in AllowedCodes Class.Does using nested loops affect performance with the given amount of data?

public class AllowedCodes {

    private String codeValue=&quot;&quot;;

    public String getCodeValue() {
        return codeValue;
    }

    public void setCodeValue(String codeValue) {
        this.codeValue = codeValue;
    }
}

public class CheckCodes {

    public static void main(String[] args) {

        List&lt;AllowedCodes&gt; old_codes_list=getListOfOldCodes();

        List&lt;AllowedCodes&gt; new_codes_list=new ArrayList&lt;&gt;();

        String sql = &quot;This query gets the codes from database&quot;;

        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet result = statement.executeQuery();

        while(result.next()) {

            for(AllowedCodes a:old_codes){
               if(a.getCodeValue().equalsIgnoreCase(result.getCodeValue())){
                   new_codes_list.add(a);
               }
            }

        }



    }

}

答案1

得分: 1

将列表复制到 HashMap 中,将具有相同代码值(转换为小写形式)的 AllowedCodes 进行分组:

Map<String, List<AllowedCodes>> map =
    old_codes.stream().collect(groupingBy(a -> a.getCodeValue().toLowerCase()));

然后,在您的 while 循环中:

while(result.next()) {
  String resultCodeValue = result.getCodeValue().toLowerCase();
  for (AllowedCodes a : map.getOrDefault(resultCodeValue, Collections.emptyList())) {
    new_codes_list.add(a);
  }
}
英文:

Copy the list into a HashMap, grouping AllowedCodes that have the same code value when lowercased:

Map&lt;String, List&lt;AllowedCodes&gt;&gt; map =
    old_codes.stream().collect(groupingBy(a -&gt; a.getCodeValue().toLowerCase()));

Then, in your while loop:

while(result.next()) {
  String resultCodeValue = result.getCodeValue().toLowerCase();
  for (AllowedCodes a : map.getOrDefault(resultCodeValue, Collections.emptyList())) {
    new_codes_list.add(a);
  }
}

huangapple
  • 本文由 发表于 2020年9月9日 04:53:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63801417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定