anthony
anthony

Reputation: 23

adding list to another Arraylist clears the output

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.

Upvotes: 2

Views: 89

Answers (2)

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

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"]

Upvotes: 1

Sushant
Sushant

Reputation: 30

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]}]

Upvotes: 0

Related Questions