Add tags

Add tags to the following comment
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class Sample {

    private List<String> header;

    private List<List<String>> data;

    public Sample(String str) {
        boolean isFirst = true;
        String[] lines = str.split("\n");
        data = new ArrayList<List<String>>(lines.length - 1);
        for (String line : str.split("\n")) {
            List<String> row = Arrays.asList(line.split("\t"));
            if (isFirst && !(isFirst = false)) {
                header = row;
            } else {
                data.add(row);
            }
        }
    }

    public Sample sort(final int column) {
        Collections.sort(data, new Comparator<List<String>>() {
            public int compare(List<String> l, List<String> r) {
                return r.get(column).compareTo(l.get(column));
            }
        });
        return this;
    }

    public Sample swapColumn(final int column1, final int column2) {
        List<List<String>> list = new ArrayList<List<String>>(data);
        list.add(0, header);
        forAll(list, new Closure() {
            public void each(List<String> row) {
                row.set(column1, row.set(column2, row.get(column1)));
            }
        });
        return this;
    }

    public Sample addValue(final int column, final int add) {
        forAll(new Closure() {
            public void each(List<String> row) {
                row.set(column,
                    Integer.toString(Integer.parseInt(row.get(column)) + add)); 
            }
        });
        return this;
    }

    private void forAll(Closure c) {
        forAll(data, c);
    }

    private void forAll(List<List<String>> list, Closure c) {
        for (List<String> row : list) {
            c.each(row);
        }
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(join(header)).append("\n");
        for (List<String> row : data) {
            builder.append(join(row)).append("\n");
        }
        return builder.toString();
    }

    private String join(List<String> list) {
        StringBuilder builder = new StringBuilder();
        boolean isFirst = true;
        for (String str : list) {
            if (!isFirst) {
                builder.append("\t");
            } else {
                isFirst = false;
            }
            builder.append(str);
        }
        return builder.toString();
    }

    private static interface Closure {
        public void each(List<String> row);
    }

    public static void main(String[] args) {
        String str =
            "ID\tSurname\tForename\tAge\n" +
            "1\tSato\tHanako\t17\n" +
            "0\tSuzuki\tTaro\t18\n";
        System.out.println(
            new Sample(str).sort(1).swapColumn(1, 2).addValue(3, 1));
    }
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...