简体   繁体   English

将TreeSet转换为填充HashMap的最佳方法是什么?

[英]What is the best way to convert a TreeSet to populate a HashMap?

What is the best way to convert a TreeSet to populate a HashMap using Java ? 使用Java将TreeSet转换为填充HashMap的最佳方法是什么? For example: 例如:

SortedSet<Map.Entry<String, Double>> sortedset =
  new TreeSet<Map.Entry<String, Double>();

to

Map<String, Double> map = new HashMap<String, Double>();

I could be wrong, but i don't think Java has anything built in to do this. 我可能是错的,但我认为Java没有内置任何功能来做到这一点。 Your best bet is to do it manually (which is pretty straightforward): 最好的选择是手动进行(这非常简单):

for (Map.Entry<String, Double> entry : sortedSet)
    map.put(entry.getKey(), entry.getValue());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM