2016-11-17 9 views
1

わかりましたので、基本的に、私は同じ「場所」を共有し、それぞれを、私のリストオブジェクト

private ArrayList<Temperatures> recordedTemperature; 

内のオブジェクトを反復処理して表示しようとしています。場所は、温度クラスのコンストラクタで初期化int型変数です:

public Temperatures(int location, int day, double temperature) 
{ 
    this.location = location; 
    this.day = day; 
    this.temperature = temperature; 
} 

は、どのように私は、温度のArrayListの中ですべてのオブジェクトを反復処理し、一致するロケーション属性を持ち、それらを返すものを見つけるでしょうか?

+2

ストリームは複雑すぎるかもしれませんが、forループとifステートメントを使用してください。一度あなたが基礎を知っていれば非常にまっすぐ進む –

+0

投稿に従うことは簡単です:http://stackoverflow.com/questions/34506218/find-specific-object-in-a-list-by-attribute –

+0

はい、私はそれがforループなどの並べ替えが必要です。しかし、私はintでそれをどうやってするのですか?文字列の場合はif.contains(searchString)を使用するので、ちょっと混乱しますが、intでそれを行う方法がわかりません –

答えて

0

リストをループし、リスト内の各項目を条件に照らして検証する必要があります。あなたのケースでは、リストを渡し、すべてのユニークな場所を特定する必要があります(マップに入れてください)。そして、それぞれの場所について、その場所を持つエントリのリストを追加します。

Map<Integer, ArrayList<Temperatures>> map = new HashMap<Integer, ArrayList<Temperatures>>(); //create a map, for all location => array of Temperatures objects with this location 
for(Temperatures t: recordedTemperatures){ 
    if(map.get(t.location)==null){ 
     map.put(t.location, []); // if it is first Temperatures object with that location, add a new array for this location 
    } 
    map.put(t.location, map.get(t.location).push(t)); // get the Temperatures with this location and append the new Temperatures object 
} 

はその後、すべてのグループを取得するには、これらのマップを反復:

Map<Integer, List<Temperatures>> tempsByLocation = new HashMap<>(); 
for (Temperatures t : recordedTemperature) { 
//1 check that there is such location 
//2 if there is already, then append your location to the list at that location 
//3 otherwise create the new key (new location) and add the new list containing only your temperature to it 
} 
0

あなたはそれを試すことができ、私が実装し、これをしようとしなかった

for (Map.Entry<Integer, ArrayList<Temperatures>>> entry : map.entrySet()) 
{ 
    // entry.getKey() is the location 
    // entry.getValue() is the array of Temperatures objects with this location 
} 

注意を、それがかもしれません仕事をしたりアイデアを与えたりしてください。

0

あなたはのJava 8にこのような何かを行うことができます与えられたlocationに基づいて、すべてのtemperaturesを取得しようとしている場合:

public List<Temperatures> getTemperaturesFromLocation(List<Temperatures> temperatures, int location) { 
    return temperatures 
      .stream() 
      .filter(t -> 
       t.getLocation() == location 
      ) 
      .collect(Collectors.toList()); 
} 

または通常のループ付き/ if文:

public List<Temperatures> getTemperaturesFromLocation(List<Temperatures> temperatures, int location) { 
    List<Temperatures> toReturn = new ArrayList<>(); 
    for(Temperatures temperature : temperatures) { 
     if(temperature.getLocation() == location) { 
      toReturn.add(temperature); 
     } 
    } 

    return toReturn; 
} 
1

Java 8とストリームを使用できます。あなたは鍵があなたの場所と値が与えられたとTemperatureのリストであるMapを取得しますcollectgroupingBy

Map<Integer, List<Temperature>> grouped = recordedTemperature.stream().collect(Collectors.groupingBy(Temperature::getLocation)); 

を使う場所によってグループ化するにはfilter

List<Temperature> filtered = recordedTemperature.stream().filter(t -> t.getLocation() == 1).collect(Collectors.toList()); 

List使用をフィルタリングするために

ロケーション。

関連する問題