我正在使用Apache Spark 2.1和Java 8开发Kafka Streaming服务。我使用嵌套的for
循环来填充带有主题/分区对的ArrayList
。
是否可以使用其他方法从O(N ^ 2)减少此嵌套for
循环?
以下是代码:
private static List<TopicAndPartition> createTAndPList(List<String> topics, List<String> partitions)
throws ConsumerException {
if (topics.size() != partitions.size()) {
throw new ConsumerException("Cannot create list with unequal number of topics and parititons,");
}
List<TopicAndPartition> topicsAndPartitions = new ArrayList<>();
for (int t = 0; t < topics.size(); t++) {
for (int p = 0; p < Integer.parseInt(partitions.get(t)); p++) {
topicsAndPartitions.add(new TopicAndPartition(topics.get(t), p));
}
}
return topicsAndPartitions;
}
仅供参考:由于我无法控制的权力(管理层),我无法使用Kafka 8以上。
答案 0 :(得分:3)
使用您给定的代码,看起来无法减少订单。
但是,您可以进行两次小的优化。
将 topics.get(t)移出内部for循环,
不要在每个循环中重新计算内部for循环终止条件。
for (int t = 0; t < topics.size(); t++) {
String topic = topics.get(t);
int count = Integer.parseInt(partitions.get(t));
for (int p = 0; p < count; p++) {
topicsAndPartitions.add(new TopicAndPartition(topic, p));
您正在调用 topics.get 和 Integer.parseInt(partitions.get(t)) t * p次而不是t次。 topic.get()的更改可能不会做太多,但是像这样将内容移出内部循环是一种非常常见的优化。
最后,你真的需要列表中的所有内容吗?或者你可以在实际需要的地方动态生成它们吗?