首页 > 解决方案 > [TreeMap]:在一个类中创建和加载 TreeMap,并从几个单独/不同的类中访问该 Tmap

问题描述

编译错误由“新”TMap 定义解决,而不是可能的重复引用。我试图说清楚。我是 Java Newbee,这是我的第一篇文章。

问题是,就像当时一样,无法从不同的类访问我的 TMap。

话虽如此,由于随后的“新”获取,我的看跌期权丢失了。如果不一遍又一遍地重新创建它,我似乎无法访问 TMap。事件顺序:Define/init TMap=>Put/Get to TMap=>Get TMap with "new"。

我在 Stackoverflow 问题中发现了一些类似的好主意:“Accessing a HashMap from a different class”。

创建并初始化我的树形图“DataStorage”和 Put/Get“PutDataStorage”类工作正常(成功加载并能够获取这些记录)。

我尝试使用“新”DataStorage 获取(因为我还没有弄清楚如何访问该表)并且失败了,因为我创建了一个新的 Tmap。

我从这些例子中遗漏了一些东西。也许一个完整的例子会帮助我而不是碎片。我喜欢这些作品的概念,但是我太新了,无法在这个时候欣赏单个作品,并且已经在这方面花费了太多时间(断断续续地持续了几个星期)。

是的,我就是那个新人;)。我会在某个时候以某种方式修复它。

概括:

理想情况下,我想要完成的是在一个类中创建和加载我的 TreeMap。从一个或多个单独/附加/其他类访问相同的 TreeMap。

我现在拥有的:丢失了初始加载的数据和地图。无法从其他类访问我的 Treemap。

与此同时,我还在尝试不同的想法,玩包等等。

定义/初始化树形图:DataStorage

import java.util.TreeMap;
public class DataStorage {
 public static TreeMap<String, Integer> people = new TreeMap<String, Integer>();
}

然后放:PutDataStorage

import java.util.TreeMap;
public class PutDataStorage {
/** 
 * Run one time "put"
 */
    public static void main(String[] args) {  
       DataStorage storage = new DataStorage();
       TreeMap<String, Integer> people = storage.people;
            people.put("bob", 2);
            people.put("susan", 5);

        System.out.println("PutData whole Entry  " +people.entrySet());
        System.out.println("PutData First Entry  " +people.firstEntry());
        System.out.println("PutData susan Value  " +people.get("susan"));
    }
    }

获取树形图:GetDataStorage 清除我以前的放置记录,因为那里有“新”记录。我从 GetDataStorage 中得到的唯一记录是独奏 popeye=3 记录。我需要找到一种方法来访问 TMap 而不必一遍又一遍地重新创建它。

import java.util.TreeMap;
public class GetDataStorage {

    public static void main(String[] args) {  //System.out.println("main");
        DataStorage storage = new DataStorage();
        TreeMap<String, Integer> people = storage.people;
       // TreeMap<String, Integer> people = new TreeMap<String, Integer>();
        people.get("bob");
        people.get("susan");
        people.put("popeye", 3);
        System.out.println("GetData 1stEntry  " +people.firstEntry());
        System.out.println("GetData bobValue  " +people.get("bob"));
        System.out.println("GetData popValue  " +people.get("popeye"));
        System.out.println("GetData lowerKey  " +people.lowerEntry("popeye"));
        System.out.println("GetData highKey   " +people.higherEntry("popeye"));
    }
    }

输出:来自 Put

PutData whole Entry  [bob=2, susan=5]
PutData First Entry  bob=2
PutData susan Value  5

输出:从获取

GetData 1stEntry  popeye=3
GetData bobValue  null
GetData popValue  3
GetData lowerKey  popeye=3
GetData hireKey   popeye=3

预期的获取结果应该是:

GetData 1stEntry  bob=2
GetData bobValue  2
GetData popValue  3
GetData lowerKey  bob=2
GetData highKey   susan=5

提前谢谢你。

标签: javagetputtreemapbluej

解决方案


这两个类再次是“静态的”。

无论是否创建公共定义的包原因,这都适用。我创建并测试了两者。如果需要,注释掉两个类中的包名。

请注意,我以非升序排列来证明导航对我的 TreeMap 有效。

不要对答案行的数量感到震惊,因为大多数是打印和评论。

PutDS 类(创建和加载 TreeMap 及其记录)。

//package twowherewego;

import java.util.TreeMap;

public class PutDS {        // Put DS(Data Storage)

public static TreeMap gettMapCashType(){    System.out.println("PutDS - entered " );
    // Create TreeMap=tMapCashType  and "put" two records to tMapCashType
       TreeMap<String, String> tMapCashType = new TreeMap<String, String>();

                tMapCashType.put("C", "Credit Card");  // out of order puts
                tMapCashType.put("A", "All");

System.out.println("PutDS " + tMapCashType);
                return tMapCashType;
            }
    }

“主”类执行 PutDS,然后放置其他 TreeMap 记录,然后使用 TreeMap Navigation 关键字获取许多记录:

//package twowherewego;
import java.util.TreeMap;

public class GetDS{         // Get DS(Data Storage)

    public static void main(String[] args) {  
        System.out.println("Works with or without 'towherewego' package cause it is public");
        System.out.println("** GetDS main: call PutDS and...");
        // Execute PutDS with method=gettMapCashType() 
       TreeMap<String, String> people = PutDS.gettMapCashType();   //class=PutDS "." method
       System.out.println("** back into GetDS main: after call to PutDS ");
                                            // space or no space ='s no difference
       people.put("D","popeye");            // notice this "put" has no space                                            
       people.put("B", "Bank Card");        // notice this "put" has a space after the ","

        System.out.println("{{{ Navigational helpful keywords, etc }}}" );
        System.out.println("GetData keySet      " +people.keySet());         // key 
        System.out.println("GetData entrySet    " +people.entrySet());       // key and value 
        System.out.println("GetData 1stEntry    " +people.firstEntry());     // 1st entry/key 
        System.out.println("GetData B_BankCard  " +people.get("B"));         // get B value 
        System.out.println("GetData B_lowerKey  " +people.lowerEntry("B"));  // get B lower
        System.out.println("GetData B_highrKey  " +people.higherEntry("B")); // get B higher    
        System.out.println("GetData lastEntry   " +people.lastEntry());      // last entry/key

           System.out.println("**  << End of GetDS >>");
    }
    }         

生成/显示带有关键字的跟踪和导航记录。注意:puts 是按降序完成的,但是 TreeMap(默认情况下)按升序重新排列它们(参见“GetData keySet, ...entrySet”的例子)。

Works with or without 'towherewego' package cause it is public
** GetDS main: call PutDS and...
PutDS - entered 
PutDS {A=All, C=Credit Card}
** back into GetDS main: after call to PutDS 
{{{ Navigational helpful keywords, etc }}}
GetData keySet      [A, B, C, D]
GetData entrySet    [A=All, B=Bank Card, C=Credit Card, D=popeye]
GetData 1stEntry    A=All
GetData B_BankCard  Bank Card
GetData B_lowerKey  A=All
GetData B_highrKey  C=Credit Card
GetData lastEntry   D=popeye
**  << End of GetDS >>


推荐阅读