の合計値Iは、次の列ヘッダーと行を持つファイルがあります:Javaの8つのストリーム:個別のキー
CITY_NAME COUNTY_NAME POPULATION
Atascocita Harris 65844
Austin Travis 931820
Baytown Harris 76335
...
を私はのような出力を生成しようとしたストリームを使用しています:
COUNTY_NAME CITIES_IN_COUNTY POPULATION_OF_COUNTY
Harris 2 142179
Travis 1 931820
...
これまでストリームを使用して郡名のリストを取得できましたが(これは繰り返しです)、今は別の郡の都市数を計算することに問題があります。これらの郡の都市。私はタイプtexasCitiesClassのArrayListのにファイルを読んでいると、私のコードは、これまでのようになります。この時点で
public static void main(String[] args) throws FileNotFoundException, IOException {
PrintStream output = new PrintStream(new File("output.txt"));
ArrayList<texasCitiesClass> txcArray = new ArrayList<texasCitiesClass>();
initTheArray(txcArray); // this method will read the input file and populate an arraylist
System.setOut(output);
List<String> counties;
counties = txcArray.stream()
.filter(distinctByKey(txc -> txc.getCounty())) // grab distinct county names
.distinct() // redundant?
.sorted((txc1, txc2) -> txc1.getCounty().compareTo(txc2.getCounty())); // sort alphabetically
}
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, String> seen = new ConcurrentHashMap<>();
return t -> seen.put(keyExtractor.apply(t), "") == null;
}
、私はユニーク郡の名前を含むストリームを持っています。 sorted()演算子は新しいストリームを返すので、どのようにして郡の母集団の値を得ることができますか?
このコードはコンパイルされますか? 'counties'はリストですか? –
nullpointer
' counties = txcArray.stream().collect(Collectors.groupingBy(txc - > txc.getCounty()、Collectors.counting()));'? –
Holger