0

I have read a CSV file into a ArrayList but need to use a for loop to sum all the values that have a specific name with them, then return the top strings, in this case letters, in a string array. For example,

"A", 2
"B", 3 
"C", 4
"A", 1
"B", 3

I have a class which reads the csv into objects so i have getters if that is of any help.

The result would give back a String [] that would have, in order, [B, C, A] as B totals 6, C totals 4 and A totals 3. Thank you.

Code I have so far,

public ArrayList<String> getTopRooms(int n){
    ArrayList<String> roomNames = new ArrayList<>();
    for (int i =0; i<recordList.size();i++){
        if(!roomNames.contains(recordList.get(i).getRoomName()))
            roomNames.add(recordList.get(i).getRoomName());
    }

recordList contains data from the csv file, in this case i am trying to get the top rooms that have been booked. all rooms have a length of time which is shown by an int so for example, kitchen would have the length of 2.

5
  • 1
    Can you show us your code please? Commented Mar 11, 2020 at 16:33
  • Have you made any effort to solve this problem yourself? Commented Mar 11, 2020 at 16:34
  • You could just use a Map<String, Integer> with letter as key and count as value. Commented Mar 11, 2020 at 16:34
  • @TimBiegeleisen I have tried this myself but only got as far as to add the names into a separate array. Commented Mar 11, 2020 at 16:36
  • @YCF_L I have edited the post as to help everyone. Thank you. Commented Mar 11, 2020 at 16:40

2 Answers 2

1

Just use a map to keep track of the tallies for each letter/key.

Map<String, Integer> map = new HashMap<>();
for (String line : yourList) {
    String[] parts = line.split(",\\s*");
    String key = parts[0];
    Integer value = Integer.parseInt(parts[1]);
    Integer currValue = map.get(key);
    map.put(key, Objects.isNull(currValue) ? value : currValue + value);
}

map.entrySset().stream().forEach(e -> System.out.println(e));

I am assuming here that your flat file actually looks like:

A, 2
B, 3 
C, 4
A, 1
B, 3

and that each entry in your record list would be one CSV tuple.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the suggestion but I need to use a for loop for this, not sure if its possible as of yet. I have used a map but trying to see if another method will run faster or slower for practice purposes.
That interpretation of the file is correct, if there is another way of doing this without using a map of any kind would be great.Would like to add that the CSv has already been read an put into an arraylist. Appreciate the help.
@HumzahHanif You can of course use any other custom class or data structure that models your relation between Key->Count. But I doubt anyone will go through the effort of writing that class/code for you if there already exists dozens of Map implementations that already do just that. Also if you understood the principle of how this works with a map, writing the same code without a Map should be pretty easy.
@OHGODSPIDERS although that is true, dont maps work by key,value pairs. So im not necessarily asking for code, just asking for advice mainly so I can try it myself. I have seen the many implementations of using a map but none of using a for loop or any other way of doing this. Also have not seen any sort of guidance on this problem. Im relatively new to coding so just trying to learn ways of doing certain methods and trying to find other ways of doing the same method.Thanks
1

Create a plain old java object from the String and Integer values, then store those objects in a list. We then take that list of objects and group them based on their identifier, and find the sum of each of the subsequent matching pojos with that identifier.

    class Pojo {

        final String identifier;

        final int value;

        Pojo(String identifier, int value) {
            this.identifier = identifier;
            this.value = value;
        }

        public String getIdentifier() {
            return identifier;
        }

        public int getValue() {
            return value;
        }
    }

    List<Pojo> pojos = new ArrayList<>(
            Arrays.asList(
                    new Pojo("A", 2),
                    new Pojo("B", 3),
                    new Pojo("C", 4),
                    new Pojo("A", 1),
                    new Pojo("B", 3)));

    Map<String, Integer> map =
            pojos.stream().collect(Collectors.groupingBy(
                            Pojo::getIdentifier, Collectors.summingInt(Pojo::getValue)));

Output

{A=3, B=6, C=4}

1 Comment

I agree, just added.

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.