2

I have a CSV file of neighboring regions. I have read the file with java and created 2D array of Strings. the data that I have looks something like this:

100 , 101
100, 102
100, 152
200, 201
200, 202

This data means that region 100 is neighbors with 101,102,152. I want to create a new 2D array where the fist element is the "key" and the following elements are the neighboring regions. something like this:

100 101 102 152
200 201 202

I used an array of strings because some of the values are not all integers.

Here is what I was trying:

List<List<String>> outputLines = new ArrayList<>();
    List<String> inner = new ArrayList<>();
    for(int i =0; i<lines.size();i++){

        if(inner.isEmpty()){
            inner.add(array[i][0]);
            inner.add(array[i][1]);
        }
        else if(inner.get(0).equals(array[i][0])){
            inner.add(array[i][1]);
        }
        else{
            outputLines.add(inner);
            inner.clear();
            inner.add(array[i][0]);
            inner.add(array[i][1]);
        }`

My issue is that when I clear the inner list it so I can start populating the list with the new one it also deletes the list that I passed to the output list. I can't figure out why.

6
  • 1
    They are the same Object, if you want to keep the value in the outputLines list unchanged, then you will need to clone it Commented Dec 11, 2018 at 5:43
  • thank you very much good sir this solved my issue! Commented Dec 11, 2018 at 5:48
  • 3
    Just create a new list instead of clearing the old one,ie inner = new ArrayList<>();instead of inner.clear(). Commented Dec 11, 2018 at 5:48
  • I suggest you use a Map<T, List<T>> instead of List<List<T>>. A map is more suited for this problem Commented Dec 11, 2018 at 6:06
  • Note that this doesn't necessarily add the last line in the input. Commented Dec 11, 2018 at 7:21

2 Answers 2

1
public static Map<String, Set<String>> group(List<String> lines) {
    final Pattern comma = Pattern.compile(",\\s*");
    Map<String, Set<String>> map = new TreeMap<>();

    lines.stream()
        .map(comma::split)
        .forEach(line -> map.compute(line[0], (region, neighbors) -> {
            neighbors = neighbors != null ? neighbors : new TreeSet<>();
            neighbors.add(line[1]);
            return neighbors;
        }));

    return map;
}

Demo:

List<String> data = Arrays.asList(
        "100, 101",
        "100, 102",
        "100, 152",
        "200, 201",
        "200, 202");
Map<String, Set<String>> map = group(data);

Output:

"100": ["101", "102", "152"]
"200": ["201", "202"]
Sign up to request clarification or add additional context in comments.

Comments

0

I have written one sudo logic for your output you can this way also.

Kindly go through below code,

public class Main {

    static int array[][]={{100,101},{100,102},{100,152},{200,300},{200,500}};
    static Map m =new HashMap();
    public static void main(String[] args) {
        for (int[] innerArray: array) {
            if(!m.containsKey(innerArray[0])){
                List tempLst=new ArrayList<>();
                tempLst.add(innerArray[1]);
                m.put(innerArray[0], tempLst);
            }
            else
            {
                List tempLst=(ArrayList)m.get(innerArray[0]);
                tempLst.add(innerArray[1]);
                m.put(innerArray[0], tempLst);
            }
        }
        System.out.println(Arrays.asList(m)); // method 1

    }
}

Output : [{100=[101, 102, 152], 200=[300, 500]}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.