0

I can't figure it out.

List<String[]> dogList = new ArrayList<String[]>();

dogList.add(new String[] { "id_tag", "breed", "rank", "nickname"});
dogList.add(new String[] { "t4639", "Akita", "First", "Marshal"});
dogList.add(new String[] { "t4638", "Akita", "First", "Tom"});
dogList.add(new String[] { "t4637", "Beagle", "First", "Eddy"});
dogList.add(new String[] { "t4636", "Beagle", "Second", "Franky"});

I would like to put in an other ArrayList sum by breed and rank

List<String[]> dogTotal = new ArrayList<String[]>();

To get an output like this if i iterate over dogTotal

"Akita", "First", "2"
"Beagle", "First", "1"
"Beagle", "Second", "1"

I have tried to use HashMap without success.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
OlZ
  • 346
  • 1
  • 3
  • 11

4 Answers4

1

You need to create a Class Dog.

Once you have a Dog class you can do this:

Dog d2 =  new Dog("t4639", "Akita", "First", "Marshal");
Dog d3 = new Dog( "t4638", "Akita", "First", "Tom");
Dog d4 =  new Dog("t4637", "Beagle", "First", "Eddy");
Dog d5 =new Dog("t4636", "Beagle", "Second", "Franky");

List<Dog> dogs = Arrays.asList(d2, d3, d4, d5);

And then there are some nifty things you can use with stream. The below will create a hashmap that you can group anything by. See here: Group by in Java 8 on multiple fields with aggregations

dogs.stream()
    .collect(Collectors.groupingBy(Dog::getBreed,
                 Collectors.groupingBy(Dog::getRank, Collectors.counting())));

Look how much cleaner this code is. This is using declarative style rather than imperative.

Returns:

{Akita={First=2}, Beagle={Second=1, First=1}}
Community
  • 1
  • 1
Nick Ziebert
  • 1,258
  • 1
  • 9
  • 17
0

Implement a Comparator<String[]> to check if two elements are equal. Then you can have two for-loops to check for a specific dog if there are also other dogs with the same breed and rank (a simple algo would be O(n^2)) or you can find a way to use this Comparator in an system-implemented method.

Try sorting the dogs by breed and rank and then do a for-loop to add them in the dogsTotal. This would take O(nlogn). You can also try to create your own algo based on the symmetry of your data.

tur1ng
  • 1,082
  • 1
  • 10
  • 24
0

Hey what you can do is use <breed>_<rank> as key for maintaining the counts for the HashMap. When done with counting just iterate the hash map and split the key using _ as delimiter to get back breed and rank along with count value from value.

    List<String[]> dogList = new ArrayList<String[]>();
    dogList.add(new String[] { "id_tag", "breed", "rank", "nickname"});
    dogList.add(new String[] { "t4639", "Akita", "First", "Marshal"});
    dogList.add(new String[] { "t4638", "Akita", "First", "Tom"});
    dogList.add(new String[] { "t4637", "Beagle", "First", "Eddy"});
    dogList.add(new String[] { "t4636", "Beagle", "Second", "Franky"});
    List<String[]> dogTotal = new ArrayList<String[]>();
    HashMap<String,Integer> map = new HashMap<>();
    for(int i=1;i<dogList.size();i++){
        String key = dogList.get(i)[1]+"_"+dogList.get(i)[2];

        map.put(key,map.get(key)==null?1:map.get(key)+1);
    }
    for(Map.Entry<String,Integer> entry : map.entrySet()){
        String key = entry.getKey();
        String breedRank[] = new String[3];
        String getBackValues[] = key.split("_");
        breedRank[0] = getBackValues[0];
        breedRank[1] = getBackValues[1];
        breedRank[2] = String.valueOf(entry.getValue());
        System.out.println(breedRank[0]+ ", " + breedRank[1]+","+ breedRank[2]);
        dogTotal.add(breedRank);
    }
    System.out.println(dogTotal);
Samarth
  • 773
  • 1
  • 6
  • 14
0
List<String[]> dogList = new ArrayList<String[]>();

        dogList.add(new String[] { "t4639", "Akita", "First", "Marshal"});
        dogList.add(new String[] { "t4638", "Akita", "First", "Tom"});
        dogList.add(new String[] { "t4637", "Beagle", "First", "Eddy"});
        dogList.add(new String[] { "t4636", "Beagle", "Second", "Franky"});

        HashMap<String, Integer> hmap = new HashMap();

        for(String[] dog : dogList)
        {
            String type = dog[1] + ":" + dog[2];
            if(hmap.containsKey(type))
            {
                hmap.put(type, hmap.get(type) + 1);
            }
            else
            {
                hmap.put(type, 1);
            }            
        }

        for (String entry: hmap.keySet()){

            String key =entry.toString();
            String value = hmap.get(entry).toString();  
            System.out.println(key + " " + value); 
        } 
    }

enter image description here

SedJ601
  • 12,173
  • 3
  • 41
  • 59