Java 有没有一种更有效的方法可以使用ArrayList中的计数器生成HashMap?
我有一个方法,它允许我通过获取AirbnbListing对象,然后将邻里名称与HashMap中的任何键进行比较,从ArrayList创建HashMap。如果hashmap中还没有它,我添加一个从1开始的计数器,如果它已经存在,我增加计数器 有没有更有效的方法来实现这一点这是我的代码:Java 有没有一种更有效的方法可以使用ArrayList中的计数器生成HashMap?,java,sorting,arraylist,hashmap,counter,Java,Sorting,Arraylist,Hashmap,Counter,我有一个方法,它允许我通过获取AirbnbListing对象,然后将邻里名称与HashMap中的任何键进行比较,从ArrayList创建HashMap。如果hashmap中还没有它,我添加一个从1开始的计数器,如果它已经存在,我增加计数器 有没有更有效的方法来实现这一点这是我的代码: public HashMap<String, Integer> sortHousesInNbrhood(ArrayList<AirbnbListing> priceRangeList
public HashMap<String, Integer> sortHousesInNbrhood(ArrayList<AirbnbListing> priceRangeListing) {
HashMap<String, Integer> housesInNbrhood = new HashMap<>();
for (AirbnbListing listing : priceRangeListing) {
if (housesInNbrhood.isEmpty()) {
housesInNbrhood.put(listing.getNeighbourhood(), 1);
} else if (housesInNbrhood.containsKey(listing.getNeighbourhood())) {
housesInNbrhood.replace(listing.getNeighbourhood(), housesInNbrhood.get(listing.getNeighbourhood()) + 1);
} else {
housesInNbrhood.put(listing.getNeighbourhood(),1);
}
}
return housesInNbrhood;
}
将具有的收集器用作下游收集器:
priceRangeListing.stream()
.collect(groupingBy(AirbnbListing::getNeighbourhood, counting()));
注意,上述操作将生成一个映射,但如果您确实需要映射,则使用收集器作为下游:
priceRangeListing.stream()
.collect(groupingBy(AirbnbListing::getNeighbourhood, summingInt(e -> 1)));
将具有的收集器用作下游收集器:
priceRangeListing.stream()
.collect(groupingBy(AirbnbListing::getNeighbourhood, counting()));
注意,上述操作将生成一个映射,但如果您确实需要映射,则使用收集器作为下游:
priceRangeListing.stream()
.collect(groupingBy(AirbnbListing::getNeighbourhood, summingInt(e -> 1)));
是专门用于跟踪对象在集合中出现次数的数据结构。它们非常适合你的问题
它们在Java标准库中不可用,但在Guava库中可用
我建议用Multiset替换HashMap作为方法的返回值。在这种情况下,读者会更清楚它包含了什么
使用该解决方案,代码如下所示:
public Multiset<String> sortHousesInNbrhood(List<AirbnbListing> priceRangeListing) {
Multiset<String> housesInNbrhood = TreeMultiset.create();
// Multimap automatically counts the number of times an object have been added
priceRangeListing.forEach(list -> housesInNbrhood.add(list.getNeighbourhood()));
return housesInNbrhood;
}
// Use the multiset like this:
public void useMultiset(List<AirbnbListing> priceRangeListing) {
Multiset<String> s = sortHousesInNbrhood(priceRangeListing);
System.out.println("Houses in Bagdad:" + s.count("Bagdad"));
System.out.println("All counts: ");
for (Entry<String> e : s.entrySet()) {
System.out.println(e.getElement() + ": " + e.getCount());
}
}
public Map<String, Integer> sortHousesInNbrhood_2(List<AirbnbListing> priceRangeListing) {
Multiset<String> housesInNbrhood = TreeMultiset.create();
priceRangeListing.forEach(list -> housesInNbrhood.add(list.getNeighbourhood()));
return housesInNbrhood.entrySet().stream().collect(toMap(Entry::getElement, Entry::getCount));
}
Multiset和multimaps是我最喜欢的数据结构之一。太方便了 是专门用于跟踪对象在集合中出现次数的数据结构。它们非常适合你的问题
它们在Java标准库中不可用,但在Guava库中可用
我建议用Multiset替换HashMap作为方法的返回值。在这种情况下,读者会更清楚它包含了什么
使用该解决方案,代码如下所示:
public Multiset<String> sortHousesInNbrhood(List<AirbnbListing> priceRangeListing) {
Multiset<String> housesInNbrhood = TreeMultiset.create();
// Multimap automatically counts the number of times an object have been added
priceRangeListing.forEach(list -> housesInNbrhood.add(list.getNeighbourhood()));
return housesInNbrhood;
}
// Use the multiset like this:
public void useMultiset(List<AirbnbListing> priceRangeListing) {
Multiset<String> s = sortHousesInNbrhood(priceRangeListing);
System.out.println("Houses in Bagdad:" + s.count("Bagdad"));
System.out.println("All counts: ");
for (Entry<String> e : s.entrySet()) {
System.out.println(e.getElement() + ": " + e.getCount());
}
}
public Map<String, Integer> sortHousesInNbrhood_2(List<AirbnbListing> priceRangeListing) {
Multiset<String> housesInNbrhood = TreeMultiset.create();
priceRangeListing.forEach(list -> housesInNbrhood.add(list.getNeighbourhood()));
return housesInNbrhood.entrySet().stream().collect(toMap(Entry::getElement, Entry::getCount));
}
Multiset和multimaps是我最喜欢的数据结构之一。太方便了 这里的问题是,什么对你更有效率?更快的执行时间?更少的编译字节码?有很多应用于效率的度量。在当今服务器资源充足的情况下,请考虑在可读性更好的情况下,多一点可读性代码。因为几个月后让开发人员(包括您自己)思考代码的作用是低效的,因为您试图将其精简为巧妙的缩短。啊,我将修改我的问题,最好是加快执行时间。这就是我的意思。如果可读性更好,那么就这样做,如果是相同的,代码更少,那么就这样做。如果它的可读性较差,那么就不要这样做,因为与潜在地导致开发人员必须找出一些本来很好的问题相比,您在执行时间上节省的纳秒是微不足道的:有不同的解决方案,它们在可读性上没有太大差异,但在性能上可能会有显著差异。我对一个非常相似的案例的不同选择做了比较,这里是,什么对你更有效?更快的执行时间?更少的编译字节码?有很多应用于效率的度量。在当今服务器资源充足的情况下,请考虑在可读性更好的情况下,多一点可读性代码。因为几个月后让开发人员(包括您自己)思考代码的作用是低效的,因为您试图将其精简为巧妙的缩短。啊,我将修改我的问题,最好是加快执行时间。这就是我的意思。如果可读性更好,那么就这样做,如果是相同的,代码更少,那么就这样做。如果它的可读性较差,那么就不要这样做,因为与潜在地导致开发人员必须找出一些本来很好的问题相比,您在执行时间上节省的纳秒是微不足道的:有不同的解决方案,它们在可读性上没有太大差异,但在性能上可能会有显著差异。我对一个非常相似的案例的不同选项进行了比较,在收集时我如何知道它被收集到地图中?@LogiGunaratnam groupingBy返回一个Map@LogiGunaratnam利诺说的是对的。只是为了扩展groupingBy始终会返回一个映射,但是为了决定将结果收集到哪种类型的映射,您需要使用groupingBy的另一个变体,例如priceRangeListing.stream.CollectionGroupingByAirbBinListing::GetNeighbory,HashMap::new,counting@Lino minor,但我确信您指的是Map而不是Map。值得一提的是,您需要导入静态java.util.stream.Collectors。*;编译此代码。当它收集时,我如何知道它被收集到地图中?@LogiGunaratnam groupingBy返回一个Map@LogiGunaratnam利诺说的是对的。只是为了扩展groupingBy始终会返回一个映射,但是为了决定将结果收集到哪种类型的映射,您需要使用groupingBy的另一个变体,例如priceRangeListing.stream.CollectionGroupingByAirbBinListing::GetNeighbor
urhood,HashMap::new,counting@Lino minor,但我确信您指的是Map而不是Map。值得一提的是,您需要导入静态java.util.stream.Collectors。*;以编译此代码。