我正在编写一个程序,它将针对文件中的所有数字列在任何任意文件(没有数据先验知识)的每个特征列上运行 groupby。我希望这个过程非常快,但我希望它先工作。我有两个问题:
1).就这个复杂的 HashMaps 数据结构列表如何以视觉方式表示(在评论中描述)而言,以下理解是否正确?
List<HashMap<String, ArrayList<HashMap<String, Number>>>> finalResult =
new ArrayList<HashMap<String, ArrayList<HashMap<String, Number>>>>();
/**
* Result should contain something like this for population and other metrics:
* [{population={state={Virginia=20000000, Texas=200000, NY=30000000},
* {Country={Africa=30000000, India=400000000}},
* {Temperature={state={Virginia=83, Texas=92, NY=72},
* {Country={Africa=90, India=88, England=65, Canada=69}}}},
* {LifeExpectancy={state={Virginia=77, Texas=83, NY=67},
* {Country={Africa=90, India=88, England=65, Canada=69}}}}]
*/
2).有没有更智能的方法来存储所有这些信息?改进此数据结构设计的任何想法?它基本上将存储聚合类型列表和每个特征列的数字指标。
这是一个示例文件(可以是任何类型的文件):
id;state;city;total_pop;avg_temp
1;Florida;;120000;76
2;Michigan;Detroit;330000;54
3;New Jersey;Newark;;34
4;Florida;Miami;200000;80
5;New Jersey;Jersey City;1200000;55
提前谢谢你。
最佳答案
拥有包含这些属性的 Country
或 State
对象会容易得多。然后您可以使用自定义 Comparator
进行排序秒。然后你会得到这样的东西:
Map<String, List<Country>> countryStatistics = new Map<>();
countryStatistics.put(
"population",
new ArrayList<Country>(
Collections.sort(
countries,
new Comparator<Country>() {
int compare(Country c1, Country c2) {
return c1.getPopulation() - c2.getPopulation();
}
}
)
)
);
依此类推,对于每个类别。然后,您将拥有一张 map ,将每个统计数据映射到按该统计数据排序的国家/地区排序列表。
根据您的编辑,对于任意数据,您也许可以这样做:
//there's probably a better name for this, but let's go with this for now
public class Data {
private Map<String, Integer> attributes = new HashMap<>();
public Integer getValue(String attribute) {
return attributes.get(attribute); //This doesn't handle cases where
//the attribute doesn't exist. Maybe
//you want to return 0 for that.
}
public Integer setValue(String attribute, Integer value) {
attributes.put(attribute, value);
}
}
然后你会做类似的事情:
Map<String, List<Data>> dataStatistics = new Map<>();
dataStatistics.put(
"population",
new ArrayList<Country>(
Collections.sort(
countries,
new Comparator<Country>() {
@Override
public int compare(Country c1, Country c2) {
return c1.getValue("population") - c2.getValue("population");
}
}
)
)
);
如果你不想重复代码,你可以创建一个工厂方法返回一个Comparator
的实例,它根据指定的属性进行排序:
public Comparator<Data> createComparatorForAttribute(final String attribute) {
return new Comparator<Data>() {
@Override
public int compare(Data d1, Data d2) {
return d1.getValue(attribute) - d2.getValue(attribute);
}
};
}
关于java - java 中动态多选项 groupby 的复杂 hashmap 数据结构 - 改进在任意数据上运行 groupbys 的实现的想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19824945/