Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何编写HashMap<;字符串,HashMap<;整数,ArrayList<;整数>&燃气轮机&燃气轮机;归档?_Java_Sorting_Arraylist_Hashmap - Fatal编程技术网

Java:如何编写HashMap<;字符串,HashMap<;整数,ArrayList<;整数>&燃气轮机&燃气轮机;归档?

Java:如何编写HashMap<;字符串,HashMap<;整数,ArrayList<;整数>&燃气轮机&燃气轮机;归档?,java,sorting,arraylist,hashmap,Java,Sorting,Arraylist,Hashmap,所以我可以写: HashMap<String, ArrayList<Integer>> ... HashMap。。。 但是当涉及到像这样的结构时 HashMap<String, HashMap<Integer, ArrayList<Integer>>> ... HashMap。。。 我失败了… Set<String> keys = outer.keySet(); List<String> list = s

所以我可以写:

HashMap<String, ArrayList<Integer>> ...
HashMap。。。
但是当涉及到像这样的结构时

HashMap<String, HashMap<Integer, ArrayList<Integer>>> ...
HashMap。。。
我失败了…

Set<String> keys = outer.keySet();
List<String> list = sortList(keys);
Iterator<String> it = list.iterator();
HashMap<Integer,ArrayList<Integer>> inner=new HashMap<Integer,ArrayList<Integer>>();
                while (it.hasNext()) {
                    String key = it.next();
                    Set<Integer> ids= inner.keySet();
                    List<Integer> positions=sortList(ids);
                    Iterator<Integer> itIn=positions.iterator();
                    while(itIn.hasNext()){
                        String id= it.next();
                        output.write(key + "\t" + outer.get(key) + " " +inner.get(id) +"\n");
                    }
                }
Set keys=outer.keySet();
列表=排序列表(键);
Iterator it=list.Iterator();
HashMap内部=新的HashMap();
while(it.hasNext()){
String key=it.next();
Set id=internal.keySet();
列表位置=排序列表(ID);
迭代器itIn=positions.Iterator();
while(itIn.hasNext()){
String id=it.next();
output.write(key++“\t”+外部.get(key)+“+内部.get(id)+”\n”);
}
}
我的代码可以写所有外键和整数1的第一个元素,但看不到列表等等。

如何与内部hashmap和外部hashmap建立连接?

如何通过将整个对象存储为文件?比如:

public static void saveObject(HashMap<String, HashMap<Integer, ArrayList<Integer>>> obj, String filePath)
{
    OutputStream os = null;
    try
    {
        os = new ObjectOutputStream(new FileOutputStream(filePath));
        os.writeObject(obj);
    }
    catch(Exception ex){}
    finally
    {
        os.close();
    }
}
publicstaticvoidsaveobject(HashMap obj,stringfilepath)
{
OutputStream os=null;
尝试
{
os=新对象输出流(新文件输出流(文件路径));
os.writeObject(obj);
}
捕获(例外情况除外){}
最后
{
os.close();
}
}
然后您可以像这样加载它:

public static HashMap<String, HashMap<Integer, ArrayList<Integer>>> loadObject(String filePath)
{
    HashMap<String, HashMap<Integer, ArrayList<Integer>>> obj = null;
    InputStream is = null;
    try
    {
        is = new ObjectInputStream(new FileInputStream(filePath));
        obj = (HashMap<String, HashMap<Integer, ArrayList<Integer>>>) is.readObject();
    }
    catch(Exception ex){}
    finally
    {
        is.close();
    }
    return obj;
}
publicstatichashmap加载对象(stringfilepath)
{
HashMap obj=null;
InputStream=null;
尝试
{
is=新对象输入流(新文件输入流(文件路径));
obj=(HashMap)是.readObject();
}
捕获(例外情况除外){}
最后
{
is.close();
}
返回obj;
}

请注意,您需要实现
可序列化
接口才能使用此对象序列化。

序列化
内部
变量,它将负责序列化其内部保存的所有引用


顺便提一下,为什么需要这样一个嵌套的数据结构?闻起来像是设计问题。

你确定你真的想写这段代码吗?查看
内部变量
,您现在正在实例化它,是否要对其进行迭代?那个集合是空的,所以里面不能没有值。我不是故意的,这是我最好的原始想法。是的,我不知道如何将外部值集成到内部值,然后停止考虑hashmaps、arraylists等等,开始考虑矩阵。你那边有什么?一张非常特别的地图。将Srings映射为整数,然后再映射为整数。在3D矩阵中,我们有矩阵[String][Integer][Integer]。如果你能想出一个更好的解决方案,试试这种方法。但这看起来确实是一个错误的做法。把它想象成一个3D矩阵,你就会得到它。我已经成功地提出了一个序列化3D矩阵的想法,但是当使用你的
HashMap
时,事情变得非常复杂。如果你失败了,请再告诉我一次,这样我就可以向你展示这个问题的解决方案。我正在一个大数据集中存储、定位每个唯一单词的索引和文件id。要做到这一点,我认为这种方式会更好。但你肯定可以以某种方式简化设计,你正在创建的是一个可维护性问题