Java - 树形图解决方案

发布于 2024-12-12 17:20:43 字数 656 浏览 0 评论 0 原文

我已经有一段时间没有接触 Java 了,我需要一些关于数据结构的建议和想法。

目前我正在使用 TreeMap 将字符串值映射到整数值。我现在需要进行一些计算,并将地图条目的整数值除以整个地图的大小,并将其存储到每个条目。我正在考虑使用 Map,Integer>但是Java中有3路泛型数据结构吗?

我目前的解决方案是这样的..

            int treeSize = occurrence.size();
            String [][] weight = new String[treeSize][2];
            int counter=0;
            double score =0;
            for(Entry<String, Integer> entry : occurrence.entrySet()) {
                weight[counter][0]=entry.getKey();
                score=entry.getValue()/treeSize;
                weight[counter][1]= Double.toString(score);
                counter++;
              }

I haven't done Java in a while and i need some suggestions and idea's regarding data structures.

Currently i am using a TreeMap to map String values to Integer value. I now need to do some calculations and divide the Integer value of the map entry by the the size of the whole map and store this for each entry. I was thinking about using a Map,Integer> but is there a 3 way generics data structure in Java?

My current solution for this is this ..

            int treeSize = occurrence.size();
            String [][] weight = new String[treeSize][2];
            int counter=0;
            double score =0;
            for(Entry<String, Integer> entry : occurrence.entrySet()) {
                weight[counter][0]=entry.getKey();
                score=entry.getValue()/treeSize;
                weight[counter][1]= Double.toString(score);
                counter++;
              }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

折戟 2024-12-19 17:20:43

我将使用另一个对象来保存此数据:

public Data {
    private int value;
    private double score;

    ...
}

然后将地图键入为 Map。插入所有值后,您可以迭代这些值并更新映射中每个值的 ratio 属性。例如:

double size = myMap.size();
for(Map.Entry<String, Data> entry : myMap.entrySet()) {
    Data data = entry.getValue();
    data.setScore(data.getValue() / size); 
}

编辑

另一个想法刚刚浮现在脑海中。您可能应该在插入时计算它,而不是在插入它之后计算值;这样更有效率。当然,只有在事先知道值的总数的情况下才能执行此操作。

更好的方法是仅当您从地图中检索值时才执行计算。这样做有两个优点:

  • 您不需要单独的对象。只需在另一个函数内抽象对映射的值的访问,该函数返回与键关联的值除以映射的大小。
  • 由于您没有单独的对象来维护计算值,因此不需要每次添加或删除新值时都更新它。

I would use another object to hold this data:

public Data {
    private int value;
    private double score;

    ...
}

And then type the map as Map<String, Data>. After inserting all the values, you can iterate over the values and update the ratio property for each value in the map. For example:

double size = myMap.size();
for(Map.Entry<String, Data> entry : myMap.entrySet()) {
    Data data = entry.getValue();
    data.setScore(data.getValue() / size); 
}

EDIT

Another thought just came to mind. Instead of calculating the values after you have inserted it, you should probably calculate it as you are inserting it; it's more efficient that way. Of course, you can only do this if you know the total number of values beforehand.

An even better way is to perform the calculation only when you retrieve a value from the map. There are two advantages to this:

  • You don't need a separate object. Just abstract the access of the value from the map inside another function which returns the value associated with the key, divided by the size of the map.
  • Since you don't have a separate object to maintain the calculated value, you don't need to update it every time you add or delete a new value.
遇见了你 2024-12-19 17:20:43

您可以使用 Map.Entry 来保存这两个值。 (最终,您将使用 AbstractMap.SimpleEntry 或 AbstractMap.SimpleImmutableEntry)

因此您的 TreeMap 将是 TreeMap>

但是,除非您有充分的理由否则,我强烈建议您即时进行计算。 每次插入或删除任何内容时重新计算每个分数非常耗时,并且会搅动小对象,因此它可能比仅仅执行要慢计算。此外,如果多个线程访问 TreeMap,重新计算将导致线程问题。相反,类似的东西

public synchronized double getFraction(String key) {
   Integer value = theTreeMap.get(key);
   if (value == null)
      return 0.0;  // or throw an exception if you prefer...

   // note, since the Map has at least one entry, no need to check for div by zero
   return value.doubleValue() / theTreeMap.size();
}

You could use a Map.Entry<Integer, Double> to hold the two values. (Ultimately, you'd use either AbstractMap.SimpleEntry or AbstractMap.SimpleImmutableEntry)

So your TreeMap would be TreeMap<String, Map.Entry<Integer, Double>>

However, unless you have a good reason to do otherwise, I'd strongly suggest that you do the calculation on the fly. Recalculating every fraction every time anything is inserted or deleted is time consuming, and churns small little objects, so it's likely to be slower than just doing the calculation. Also, recalculation will cause threading issues if multiple threads access the TreeMap. Instead, something like

public synchronized double getFraction(String key) {
   Integer value = theTreeMap.get(key);
   if (value == null)
      return 0.0;  // or throw an exception if you prefer...

   // note, since the Map has at least one entry, no need to check for div by zero
   return value.doubleValue() / theTreeMap.size();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文