简体   繁体   中英

How to check for the same ID, and then increment for that particular id?

I have a hashmap, let's call it hashMap , that is in a method whereby I'll pass in a string called id . I also have an object, let's call it UserObject . So currently what I want to do is write my output to a csv file, using the below code:

for (UserObject user: hashMap.get(id)) {
            System.out.println(id);
            writer.println(id + "," + user.getTime() + "," + user.getLat() + "," + user.getLng()); // csv
        }

but this id may have multiples of the same one. So what I wanna do is whenever one id is used for the for loop, there'll be a counter that increments by one. So, when the same id is used again, the increment will increase. However, when a different id is being used, it is another increment operation. So basically what I mean is that whenever the for loop is running I want to count how many instances the same id will be run. How can I do so? I can't seem to figure out the logic.

PS System.out.print(id) is a line of test code, the output is one chunk of IDs.

**Edit: the logic would work something like SQL's count function, but I'm not using SQL, I just need it in pure java

Not sure if I understand it correctly, but if you want to count elements in the HashMap, you can try something like this.

public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("1", "A");
    map.put("2", "B");
    map.put("3", "C");
    map.put("4", "B");
    map.put("5", "B");
    map.put("6", "C");

    System.out.println(count("B", map)); // output is 3
}

static int count(String id, Map<String, String> map) {
    int i = 0;
    for (String val : map.values()) {
        if (id.equals(val))
            i++;
    }
    return i;
}

EDIT: If you want to wrap the funcionality where every time you touch particular value, counter increments, you can achieve it by this approach.

public class IdHandler {

    Map<String, Integer> count = new HashMap<String, Integer>();

    public int count(String id) {
        return count.get(id);
    }

    public void export(Map<String, String> map) {
        for (String value : map.values()) {
            System.out.println(value);

            if (!count.containsKey(value)) {
                count.put(value, 1);
            } else {
                int i = count.get(value);
                count.put(value, ++i);
            }
        }
    }
}

public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("1", "A");
    map.put("2", "B");
    map.put("3", "C");
    map.put("4", "B");
    map.put("5", "B");
    map.put("6", "C");

    IdHandler id = new IdHandler();
    id.export(map);

    System.out.println(id.count("B")); // output is 3
    System.out.println(id.count("C")); // output is 2
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM