1

私には3つの方法があります。

  1. 最初のメソッドはリスト(アプリケーションに固有のデータオブジェクトを含む)からデータを取得し、次に2番目のメソッドを呼び出して、取得したデータをパラメーターとして渡します。

  2. 次に、2番目のメソッドはこの情報を参照として使用して、別のリストから特定の情報を取得し、3番目のメソッドを呼び出して、この情報をパラメーターとして渡します。

  3. 3番目の方法は、2番目の方法と同じように実行され、リスト内の特定のデータを検索するための参照として渡されたデータを使用します。

問題は、この情報をまったく別の方法で使用したいということです。

データを取得するためにメソッドを呼び出すにはどうすればよいですか?

private void getRecords() {
    List<LabelSet> allSets = labelSetStructureService.getLabelSets();
    for (LabelSet labelSet : allSets) {
        List<LabelSet> groups = labelSetStructureService.getLabelGroupsForSet(labelSet.getId());
        for (LabelGroup group : groups) {
             getValuesForLabel(group.getCustomerMeasure(), labelSetStructureService.getLabelsForLabelGroup(group.getCustomer().getId(), group.getId()), labelSet);
        }
    }
}

private void getValuesForLabel(CustomerMeasure measure, List<Label> labels, LabelSet labelSet) {
    for (Label label : labels) {
        List<LabelSet> usageRecs = labelDataService.getLabelData(label, LabelSetStorage.DAY_BY_MONTH_STORAGE, new DateTime(), new DateTime().minusWeeks(1));
        preProcessUsageRecs( usageRecs, labelSet);
    }
}

private List<LabelUsage> preProcessUsageRecs(List<LabelUsage> usageRecs, LabelSet labelSet) {
    //Format records, use list instead strings where possible

    List<LabelSet> usageRecords = usageRecs; {
      for( LabelUsage labelUsage : usageRecs)
          usageRecords.addAll(usageRecs);
    }
     return usageRecords;
    }
4

2 に答える 2

0

疑わしいコードを書いているようです。あなたの説明に基づくと、問題を理解すれば、APIは次のようになります。

public void go(Map<MapKey,Collection<MyObject>> values, Keyword keyword)  
{  
      for(MapKey key: values.keyset())  
     {  
                 search(values.get(key),keyword);
     }  
}  

public void search(Collection<MyObject> searchSpace, Keyword key)  
{  
       for(MyObject currentSearch : searchSpace)  
       {  
            if(currentSearch == key)  
            {  
                //do something
            }  
       }  
}  

重要なのは、検索を1つの関数に一般化できるはずです。何か特別なことが必要な場合は、このプロセスにいくつかのプライベート(ヘルパー)関数を適用し、switchステートメントを介してそれらをバインドできます。

于 2012-12-14T17:02:38.590 に答える
0

Javaはオブジェクトベースの言語であるため、次のように、メンバー変数を使用してその1回の呼び出しの結果を格納します。

public List<LabelUsage> labels;
...
private List<LabelUsage> preProcessUsageRecs(List<LabelUsage> usageRecs, LabelSet labelSet) {
    //Format records, use list instead strings where possible

    List<LabelSet> usageRecords = usageRecs; {
      for( LabelUsage labelUsage : usageRecs)
          usageRecords.addAll(usageRecs);
    }
    this.labels = usageRecords;
     return usageRecords;
}

this.labels次に、このメソッドの結果を使用する必要がある場所にアクセスします。

于 2012-12-14T17:05:01.037 に答える