比较二维数组中的元素,并将其存储在一个包含元组的映射中。

huangapple go评论67阅读模式
英文:

Compare elements of 2d Array to each-other and store this in a map with tuple

问题

import 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 = new HashMap<>();

        for (List<String> arr : all) {
            String key = arr.get(1); // Assuming the path is always at index 1
            int firstValue = Integer.parseInt(arr.get(0));
            int secondValue = (firstValue == 1) ? 0 : 1;

            if (information.containsKey(key)) {
                Pair<Integer, Integer> pair = information.get(key);
                information.put(key, new Pair<>(pair.getLeft() + firstValue, pair.getRight() + secondValue));
            } else {
                information.put(key, new Pair<>(firstValue, secondValue));
            }
        }

        for (Map.Entry<String, Pair<Integer, Integer>> entry : information.entrySet()) {
            String path = entry.getKey();
            Pair<Integer, Integer> counts = entry.getValue();
            System.out.println(path + "->(" + counts.getLeft() + "," + counts.getRight() + ")");
        }
    }

    public static 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());
        }
    }
}
英文:

I have this 2d-Array

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

I want to store its element in a Map like this Map&lt;String, Pair&lt;Integer, Integer&gt;&gt; information;

The first Integer is the counter path with 1 and the sec integer is the counter path with 0

the expected output is therefore like this.

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

This is my try, i stoped because it did not know how to comparing the elements and store its element in the Map.
import java.util.*;

public class HelloWorld {
public static void main(String[] args) {
List &lt; List &lt; String &gt;&gt; all = new ArrayList &lt; &gt; ();
List &lt; String &gt; arr1 = new ArrayList &lt; &gt; ();
arr1.add(&quot;1&quot;);
arr1.add(&quot;path1&quot;);
arr1.add(&quot;path2&quot;);
arr1.add(&quot;path3&quot;);
arr1.add(&quot;path5&quot;);
////////
List &lt; String &gt; arr2 = new ArrayList &lt; &gt; ();
arr2.add(&quot;0&quot;);
arr2.add(&quot;path1&quot;);
arr2.add(&quot;path3&quot;);
arr2.add(&quot;path5&quot;);
////////
List &lt; String &gt; arr3 = new ArrayList &lt; &gt; ();
arr3.add(&quot;1&quot;);
arr3.add(&quot;path1&quot;);
arr3.add(&quot;path2&quot;);
arr3.add(&quot;path5&quot;);
////////
all.add(arr1);
all.add(arr2);
all.add(arr3);
Map &lt; String, Pair &lt; Integer, Integer &gt;&gt; information;
System.out.println(all);
for (int i = 0; i &lt; all.size(); i++) {
for (int j = 0; j &lt; all.get(i).size(); j++) {
System.out.println(all.get(i).get(j));
}
}
}
public class Pair &lt; L, R &gt; {
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()) &amp;&amp;
this.right.equals(pairo.getRight());
}
}
}

答案1

得分: 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<Integer, Integer>> map = new HashMap<String, Tuple<Integer, Integer>>();
        for (int i = 0; i < inputArray.length; i++) {
            String[] individualArray = inputArray[i];

            for (int j = 0; j < individualArray.length; j++) {
                Tuple<Integer, Integer> tuple;
                if(map.containsKey(individualArray[j]))
                {
                    tuple = map.get(individualArray[j]);

                    if(individualArray[0].equals("1"))
                    {
                        int counter = tuple.getLeft();
                        counter = counter + 1;
                        tuple.setLeft(counter);
                    }
                    else
                    {
                        int counter = tuple.getRight();
                        counter = counter + 1;
                        tuple.setRight(counter);
                    }
                    map.put(individualArray[j], tuple);

                }
                else
                {
                    if(individualArray[0].equals("1"))
                    {
                        tuple = new Tuple<Integer, Integer>(1, 0);
                    }
                    else
                    {
                        tuple = new Tuple<Integer, Integer>(0, 1);
                    }
                    map.put(individualArray[j], tuple);
                }
            }
        }
    }
}
    
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;
    }
}
英文:

Since arithmetic operations are not allowed on generics, we should cast that generic value to an integer (only if you are sure that value is an integer) or else instead of generics you can use primitive data types. Since you coded using generics, I used generics for implementation. Otherwise, I would recommend using primitive data types for your requirement. The following code would do what you asked:


public class HelloWorld {
public static void main(String[] args) {
String[] inputArray[] = {{&quot;1&quot;, &quot;path1&quot;, &quot;path2&quot;, &quot;path3&quot;, &quot;path5&quot;}, {&quot;0&quot;, &quot;path1&quot;, &quot;path3&quot;, &quot;path5&quot;}, {&quot;1&quot;, &quot;path1&quot;, &quot;path2&quot;, &quot;path5&quot;}};
HashMap&lt;String, Tuple&gt; map = new HashMap&lt;String, Tuple&gt;();
for (int i = 0; i &lt; inputArray.length; i++) {
String[] individualArray = inputArray[i];
for (int j = 0; j &lt; individualArray.length; j++) {
//int[] tuple = new int[2];
Tuple tuple;
if(map.containsKey(individualArray[j]))
{
tuple = map.get(individualArray[j]);
if(individualArray[0].equals(&quot;1&quot;))
{
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(&quot;1&quot;))
{
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(&quot;path5&quot;).getLeft() +&quot; , &quot;+ map.get(&quot;path5&quot;).getRight());
}
public class Tuple&lt;L,R&gt; {
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;
}
}

huangapple
  • 本文由 发表于 2020年4月4日 23:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61030513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定