【问题标题】:Compare elements of 2d Array to each-other and store this in a map with tuple将二维数组的元素相互比较,并将其存储在带有元组的地图中
【发布时间】:2026-01-04 20:00:01
【问题描述】:

我有这个2d-Array

[[1, path1, path2, path3, path5], [0, path1, path3, path5], [1, path1, path2, path5]]

我想将它的元素存储在Map 中,就像这样Map<String, Pair<Integer, Integer>> information;

第一个整数是1的计数器路径,第二个整数是0的计数器路径

因此预期的输出是这样的。

path1->(2,1) // because the path1 is 2 times appeared with in the 2d-Array with 1 and 1 times with 0. path2->(2,0) path3->(1,1) path5->(2,1)

这是我的尝试,我停止了,因为它不知道如何比较元素并将其元素存储在 Map 中。 导入 java.util.*;

public class HelloWorld {

    public static void main(String[] args) {
        List < List < String >> all = new ArrayList < > ();
        List < String > arr1 = new ArrayList < > ();
        arr1.add("1");
        arr1.add("path1");
        arr1.add("path2");
        arr1.add("path3");
        arr1.add("path5");
        ////////

        List < String > arr2 = new ArrayList < > ();
        arr2.add("0");
        arr2.add("path1");
        arr2.add("path3");
        arr2.add("path5");
        ////////

        List < String > arr3 = new ArrayList < > ();
        arr3.add("1");
        arr3.add("path1");
        arr3.add("path2");
        arr3.add("path5");
        ////////
        all.add(arr1);
        all.add(arr2);
        all.add(arr3);

        Map < String, Pair < Integer, Integer >> information;

        System.out.println(all);

        for (int i = 0; i < all.size(); i++) {
            for (int j = 0; j < all.get(i).size(); j++) {
                System.out.println(all.get(i).get(j));

            }

        }

    }





    public class Pair < L, R > {

        private final L left;
        private final R right;

        public Pair(L left, R right) {
            assert left != null;
            assert right != null;

            this.left = left;
            this.right = right;
        }

        public L getLeft() {
            return left;
        }
        public R getRight() {
            return right;
        }

        @Override
        public int hashCode() {
            return left.hashCode() ^ right.hashCode();
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Pair)) return false;
            Pair pairo = (Pair) o;
            return this.left.equals(pairo.getLeft()) &&
                this.right.equals(pairo.getRight());
        }

    }

}

【问题讨论】:

    标签: java algorithm data-structures collections hashmap


    【解决方案1】:

    由于泛型不允许进行算术运算,我们应该将该泛型值转换为整数(仅当您确定该值是整数时),否则您可以使用原始数据类型代替泛型。由于您使用泛型进行编码,因此我使用泛型进行实现。否则,我建议您使用原始数据类型来满足您的要求。以下代码可以满足您的要求:

    
        public class HelloWorld {
    
            public static void main(String[] args) {
    
    
                String[] inputArray[] = {{"1", "path1", "path2", "path3", "path5"}, {"0", "path1", "path3", "path5"}, {"1", "path1", "path2", "path5"}};
                HashMap<String, Tuple> map = new HashMap<String, Tuple>();
                for (int i = 0; i < inputArray.length; i++) {
                    String[] individualArray = inputArray[i];
    
                    for (int j = 0; j < individualArray.length; j++) {
                        //int[] tuple = new int[2];
                        Tuple tuple;
                        if(map.containsKey(individualArray[j]))
                        {
                            tuple = map.get(individualArray[j]);
    
                            if(individualArray[0].equals("1"))
                            {
                                int counter = (int)tuple.getLeft();
                                counter = counter+1;
                                tuple.setLeft(counter);
                                //tuple[0]++;
                            }
                            else
                            {
                                int counter = (int)tuple.getRight();
                                counter = counter+1;
                                tuple.setRight(counter);
                                //tuple[1]++;
                            }
                            map.put(individualArray[j], tuple);
    
                        }
                        else
                        {
                            if(individualArray[0].equals("1"))
                            {
                                tuple = new Tuple(1,0);
                            /*
                             * tuple[0] = 1; tuple[1] = 0;
                             */
                            }
                            else
                            {
                                tuple = new Tuple(0,1);
                            /*
                             * tuple[0] = 0; tuple[1] = 1;
                             */
                            }
    
    
                            map.put(individualArray[j], tuple);
                        }
    
                    }
    
    
    
    
                    }
    
                    //System.out.println( map.get("path5").getLeft() +" , "+ map.get("path5").getRight());
    
    
    
            }
    
    
    
    public class Tuple<L,R> {
    
        private L left;
        private R right;
    
        public Tuple() {
            this.left = null;
            this.right = null;
        }
    
        public L getLeft() {
            return left;
        }
    
        public void setLeft(L left) {
            this.left = left;
        }
    
        public R getRight() {
            return right;
        }
    
        public void setRight(R right) {
            this.right = right;
        }
    
        public Tuple(L left, R right) {
            this.left = left;
            this.right = right;
        }
    
    
    
    }
    

    【讨论】:

    • 您能否使用Tuple 数据结构而不是int[] 改进您的解决方案,非常感谢
    • 谢谢,有什么办法可以防止使用setter方法吗?并得到相同的结果?
    • 尝试获取变量而不是调用setter方法
    • 问题是类对应该是不可变的。请为此提供任何解决方案
    • 我有一个问题,请问如何将具有编号为1 且数组编号为0 的数组作为附加属性添加到类元组中?请帮帮我
    最近更新 更多