簡體   English   中英

Java:ArrayList <HashMap<Integer,Integer> &gt;

[英]Java : ArrayList<HashMap<Integer,Integer>>

有沒有更好的方法在Java中執行以下操作,而不使用外部庫。

我需要模擬int(primitive)的group / child(tree like)結構。 在Json

[{1,1}, {1,2}, {2,1},{3,1}]

我需要支持添加/刪除元素(元素是一對{group,child})而不重復。

我在考慮,保持數據結構。

ArrayList<HashMap<Integer,Integer>>

加上。

通過ArrayList迭代,檢查HashMap鍵和值以插入值,如果不存在則插入。

刪除:

通過ArrayList迭代,檢查HashMap鍵和值以刪除值,如果存在則刪除。

是否有更好的數據結構/方法與標准庫。


根據下面的答案之一,我做了一個這樣的課程。 請讓我知道任何要注意的事項。 我期待(並試圖)arraylist將通過使用KeyValue類中的equal方法正確處理添加/刪除。 謝謝。

 static class KeyValue {
        int groupPos;
        int childPos;

        KeyValue(int groupPos, int childPos) {
            this.groupPos = groupPos;
            this.childPos = childPos;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            KeyValue keyValue = (KeyValue) o;

            if (childPos != keyValue.childPos) return false;
            if (groupPos != keyValue.groupPos) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = groupPos;
            result = 31 * result + childPos;
            return result;
        }
    }

如果我理解你要做什么,這可能會更簡單:

TreeMap<Integer,TreeSet<Integer>>
  or
HashMap<Integer,HashSet<Integer>>

所以,而不是

[{1,1}, {1,2}, {2,1}, {3,1}]

你有

[{1, {1, 2}},
 {2, {1}},
 {3, {1}}]

請注意,上述所有4個類都自動處理消除重復項。

加上:

TreeMap<Integer, TreeSet<Integer>> map;
TreeSet<Integer> set = map.get(group);
if (set == null) // create it if it doesn't exist
{
  set = new TreeSet<Integer>();
  map.put(group, set);
}
set.add(child);

去除:

TreeMap<Integer, TreeSet<Integer>> map;
TreeSet<Integer> set = map.get(group);
set.remove(child);
if (set.isEmpty()) // remove it if it is now empty
  map.remove(group);

您可以編寫一個名為KeyValue的類,其中包含兩個屬性來保存組和子項。 KeyValue對象添加到ArrayList 對於CRUD操作,您可以在KeyValue對類中實現equalscompare

而不是HashMap ,使用一個名為Pair的類,其中兩個字段{group,child}將實現Comparable接口。 然后實現/覆蓋其equals()hashCode()compareTo()方法。 然后根據您的需要使用List<Pair>Set<Pair>來保存它們。 實現compareTo() ,您可以輕松地對Pairs進行排序。

我是數據結構世界的新手,但我認為我們可以在沒有兩個Set Objects相似的假設下使用它

設置validSet = new HashSet(); //在這里使用泛型

HashSet將為添加/刪除/包含提供一個恆定的時間

SomeObject{
     Integer parent ;
     Integer child;
     //define equals method based on your requirement
}

繼續你的問題我認為你想要顯示這一行

[{1,1}, {1,2}, {2,1},{3,1}]


組1-> 1,2(前兩對)
組2-> 1(來自第三對)
組3-> 1(來自第四對)

最適合存儲此層次結構的數據結構是:

Map<Integer,Set<Integer>> map = new HashMap<Integer,Set<Integer>>();

地圖的key部分存儲組號。 而map的value部分是存儲TreeSet ,它存儲該組的子TreeSet
作為代碼示例:

import java.util.HashMap;
import java.util.ListIterator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
class  TreeLike
{
    public static void main(String[] args) 
    {
        Map<Integer,Set<Integer>> map = new HashMap<Integer,Set<Integer>>();
        int groups[] = {1,2,3,4,5,6,7};
        //To add new group in map
        for (int i = 0 ; i < groups.length; i++)
        {
            Set<Integer> child = new TreeSet<Integer>();
            child.add(1);child.add(2);child.add(3);child.add(4);child.add(5);
            map.put(groups[i],child);
        }
        //To add new child(8) to a group (say group 1)
        Set<Integer> child = map.get(1);
        if (child != null)
        {
            child.add(8);
            map.put(1,child);
        }

        //To remove a child (say child 4) from group 3
        child = map.get(3);
        if (child != null)
        {
            child.remove(4);
            map.put(1,child);
        }
        //To Iterate through all trees
        Set<Map.Entry<Integer,Set<Integer>>> entrySet = map.entrySet();
        Iterator<Map.Entry<Integer,Set<Integer>>> iterator = entrySet.iterator();
        while (iterator.hasNext())
        {
            Map.Entry<Integer,Set<Integer>> entry = iterator.next();
            int group = entry.getKey();
            Set<Integer> children = entry.getValue();
            System.out.println("Group "+group+" children-->"+children);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM