-2

Ok so, I have several text documents containing similar format as below:

    497 Q0 WTX091-B06-138 0 0.415344133 GROUP001
    497 Q0 WTX091-B09-92 1 0.362342072 GROUP001
    497 Q0 WTX091-B09-76 2 0.354034424 GROUP001
    497 Q0 WTX091-B09-57 3 0.349649535 GROUP001
    497 Q0 WTX091-B43-79 4 0.3217919 GROUP001
    497 Q0 WTX091-B43-56 5 0.321450733 GROUP001
    .
    .
    .
    (actually there are 1000 lines in each text document)

I have stored each document into an arraylist separated by line. The challenging part is that I have to look for the third column (WTX***-B**-**) in each document and if they are the same then I have to add their fifth (0.3********) column together.

How do I compare the third column in different text documents?

mha
  • 551
  • 2
  • 11
  • 22
  • http://stackoverflow.com/questions/20176087/comparing-elements-of-two-arraylist-in-java – Harsha W Apr 03 '17 at 06:05
  • 1
    [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – Erwin Bolwidt Apr 03 '17 at 06:07
  • Fifth column or the fourth one? – user218046 Apr 03 '17 at 06:08
  • "497" is the first column "Q0" is the second, "WTX***-B**-**" is third, the singe increasing digit is fourth, "(0.3********)" is fifth and "GROUP001" is sixth. – mha Apr 03 '17 at 06:12
  • O.K. I'm on it. – user218046 Apr 03 '17 at 06:15
  • How far did you come? Did you consider regular expressions to parse your lines? The rest should be straight. Maybe you should be more concrete about the possible values of your columns. – wumpz Apr 03 '17 at 06:19
  • What I think about now is that it would be easier to somehow make a class which will have all these values, store them in collection and then using steam api call collect() function and add those two numbers if plate numbers are the same. – user218046 Apr 03 '17 at 06:22
  • 1
    It starts with a wrong beginning. You do not create several lists. If that data "belongs" together logically; then create a class that represents a **single** complete line; and then have a single list with all that data. – GhostCat Apr 03 '17 at 06:23
  • Possible duplicate of [comparing elements of two arrayList in java](http://stackoverflow.com/questions/20176087/comparing-elements-of-two-arraylist-in-java) – Matej Marconak Apr 03 '17 at 06:33
  • I am curious. Why is this downvoted? Duplicate? No example code? Trivial question? – wumpz Apr 03 '17 at 07:44

2 Answers2

2

As I assume your text file lines are space separated, this should do it. It assumes you are using Java 8. You could easily upgrade this solution to read the files directly, since Java 8 provides Files.lines - method.

So the solution splits every line of your document by a whitespace separator.

List<String> list = Arrays.asList(
            "497 Q0 WTX091-B06-138 0 0.415344133 GROUP001",
            "497 Q0 WTX091-B09-92 1 0.362342072 GROUP001",
            "497 Q0 WTX091-B09-76 2 0.354034424 GROUP001",
            "497 Q0 WTX091-B09-57 3 0.349649535 GROUP001",
            "497 Q0 WTX091-B43-79 4 0.3217919 GROUP001",
            "497 Q0 WTX091-B43-56 5 0.321450733 GROUP001");

Map<String, Double> sumMap = list.stream()
            .map(s -> s.split("\\s+"))
            .collect(groupingBy(s -> s[2], 
                 summingDouble(s -> Double.valueOf(s[4]))));

System.out.println(sumMap);

To process all files, you could read all files contents into list or you begin with a stream of files and do a flatMap for each files contents.

wumpz
  • 8,257
  • 3
  • 30
  • 25
0

Maybe something like this? Create something like an entity class and then use Stream API to turn it into map and if there is same key then add its double values:

public static class Data
{
    public int id;
    public String stringId;
    public String plateNumber;
    public int numId;
    public double value;
    public String name;

    public Data(int id, String stringId, String plateNumber, int numId, double value, String name)
    {
        this.id = id;
        this.stringId = stringId;
        this.plateNumber = plateNumber;
        this.numId = numId;
        this.value = value;
        this.name = name;
    }
}

public static void main(String[] args)
{
    List<Data> ls = new ArrayList<>();
    ls.add(new Data(497, "Q0", "WTX091-B06-138", 0, 0.415344133, "GROUP001"));
    ls.add(new Data(497, "Q0", "WTX091-B09-92", 1, 0.362342072, "GROUP001"));
    ls.add(new Data(497, "Q0", "WTX091-B09-76", 2, 0.354034424, "GROUP001"));
    ls.add(new Data(497, "Q0", "WTX091-B09-57", 3, 0.349649535, "GROUP001"));
    ls.add(new Data(497, "Q0", "WTX091-B43-79", 4, 0.3217919, "GROUP001"));
    ls.add(new Data(497, "Q0", "WTX091-B43-56", 5, 0.321450733, "GROUP001"));

    Map<String, Double> map = ls.stream()
                                .collect(Collectors.toMap(k -> k.plateNumber, v -> v.value, (v1, v2) -> v1 + v2));

    System.out.println(map);
}
user218046
  • 623
  • 6
  • 20