将HashMap密钥与扫描仪输入进行比较不起作用

时间:2018-10-07 06:29:44

标签: hashmap compare java.util.scanner rank disjoint-sets

    import java.util.*;
    import java.util.Map.Entry;
    public class DisjointSet2 {
    private Map<Integer,Node> map1 = new HashMap<>();

    class Node{
        int data;
        Node parent;
        int rank; 
    }



    public void dis_ele() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter d no of elements: ");
        int n = sc.nextInt();
        for(int i=0;i<n;i++)
            {
                System.out.println("Enter d elements: ");
                int k= sc.nextInt();
                MakeSet(k);
            }
    }

    public void MakeSet(int data){
        Iterator<Map.Entry<Integer, Node>> itr1 = map1.entrySet().iterator();
        while(itr1.hasNext()){
            Map.Entry<Integer, Node> entry = itr1.next();
            /*String itemId = Integer.toString(data);
            if(itemId.contains((CharSequence) entry.getValue())){*/
            if(entry.getKey()==data){
                 System.out.println("Duplicate Found");
                 }
            else {
                Node node = new Node();
                node.data=data;
                node.parent=node;
                // System.out.println("Node of "+data+" is "+node);
                node.rank=0;     
                map1.put(data,node);
                }
            }
        }

    public void union(int data1,int data2){
        Node node1=map1.get(data1);
        Node node2=map1.get(data2); //for getting hash value

        Node parent1 = FindSet(node1);
        Node parent2 = FindSet(node2);

        if(parent1.data==parent2.data)
            return;

        if(parent1.rank==parent2.rank){
            parent1.rank = parent1.rank+1;
            parent2.parent= parent1;
            //System.out.println(parent1.data+ "'s Rank = "+parent1.rank);
            }
        else if(parent1.rank>parent2.rank){
            parent2.parent = parent1;
            }
        else{
            parent1.parent = parent2;
            }   
        }

    public int FindSet(int data){
        return FindSet(map1.get(data)).data;
    }

    private Node FindSet(Node node){
        Node parent =node.parent;
        if(parent==node){
            return parent;
            }
        node.parent=FindSet(node.parent);
        return node.parent;
        }

    public void disp(){
        for (Entry<Integer, Node> entry :map1.entrySet())  
            System.out.println("Key = " + entry.getKey() + 
                    ", Representative Element = " + FindSet(entry.getKey())); 
        }   

    public static void main(String args[]){
        DisjointSet2 ds1 = new DisjointSet2();
        Scanner sc = new Scanner(System.in);
        int ch;
         while(true){
             System.out.println("");
             System.out.println("");
             System.out.println("1. Enter disjoint set elements for makeset operation");
             System.out.println("2. FindSet");
             System.out.println("3. Union");
             System.out.println("4. Display");
             System.out.print("Enter Your Choice : ");
             ch=sc.nextInt();
             switch(ch)
             {
                case 1: 
                    ds1.dis_ele();
                    break;   
                case 2:
                    System.out.println("Enter the element: ");
                    System.out.println("");
                    int el = sc.nextInt();
                    int rep = ds1.FindSet(el);
                    System.out.println(el+"'s representative "+rep);
                    break;
                case 3:
                    System.out.println("Enter the 1st element: ");
                    int el1 = sc.nextInt();
                    System.out.println("Enter the 2nd element: ");
                    int el2 = sc.nextInt();
                    ds1.union(el1, el2);
                    break;
                case 4 :
                    ds1.disp();
                    //case 5:System.exit(0);

             }


         }

    }
}

如果我将hashmap键与整数进行比较,则在makeset()函数中获取空指针异常。请修改代码以更改错误。 如果我不比较,那么程序将运行而没有任何麻烦。 但是如果添加重复的元素,我必须进行比较,否则排名会重置。

0 个答案:

没有答案