How to count the value of a custom data types in lists?

428
March 25, 2017, at 10:05 AM

I've seen something about hashMaps, but we haven't gotten that far in our work. Please keep your answers and helpful suggestions as simple as you can manage.

I have a custom data type that is already made and works perfectly called Color. The only values of the type are Color.BLUE, Color.RED, Color.YELLOW, and Color.GREEN.

My task is to return Color.BLUE if there are more Color.BLUE in the list than the other colors, to return Color.RED if there are more Color.RED in the list than other colors, same for Color.GREEN and Color.YELLOW.

I have researched and and came up with this code:

public Color callColor(List<Card> hand) {
    int blueCards = Collections.frequency(hand, Color.BLUE);
    int redCards = Collections.frequency(hand, Color.RED);
    int greenCards = Collections.frequency(hand, Color.GREEN);
    int yellowCards = Collections.frequency(hand, Color.YELLOW);
    Color changeColorTo = Color.NONE;
    if ((blueCards > redCards) || (blueCards > greenCards) || (blueCards > yellowCards)) {
        changeColorTo = Color.BLUE;
    }
    if ((redCards > blueCards) || (redCards > greenCards) || (redCards > yellowCards)) {
        changeColorTo = Color.RED;
    }
    if ((greenCards > redCards) || (greenCards > blueCards) || (greenCards > yellowCards)) {
        changeColorTo = Color.GREEN;
    }
    if ((yellowCards > redCards) || (yellowCards > greenCards) || (yellowCards > blueCards)) {
        changeColorTo = Color.YELLOW;
    }
    return changeColorTo;
}

but this code causes blueCards, redCards, greenCards, and yellowCards all to be 0 when they should definitely not be zero.

So, in this case, my Collections implementation did not work at all. Help!

Answer 1

You are passing a List<Card> to the method, but then you're searching for the frequency of a certain Color in that list. That's why all counts are equal to 0.

Answer 2

Your input list contains cards not colors.

So the possible problem solve is to convert you card list to color list first:

hand.stream().map(Card::getColor).collect(Collectors.toList());

As the result you will get the list of colors, so you can now use Collection.frequency on it, not on initial card list.

However there are plenty of other ways to solve your problem, like using another Collection.

Answer 3

Collections#frequency looks up occurrence of passed object in given collection but in your case you want to match on an attribute of collection of cards. That's why it gives you 0 for every frequency calculation.

Following is an iterative way of finding out card count for each color

for(Card card:hand) {
  if(card color is equal to Color.Blue) blueCards++
  else if(card color is equal to Color.Red) redCards ++
  // same code for other colors
}
READ ALSO
Count partial matches in an ordered ArrayList using only .get() and .size()

Count partial matches in an ordered ArrayList using only .get() and .size()

This code here which returns the number of elements in the list (sorted custom array list) that have the specified prefix and suffixIt works, however it should cost log(n) + k, where k is the number of strings in the list that have that prefix

432
javaFX: Wrong Merge sort animation result

javaFX: Wrong Merge sort animation result

I am implementing a merge sort animation with some rectangles using javaFXI use some function of animation to do it

529
How to convert object to String array? [on hold]

How to convert object to String array? [on hold]

I want to convert fab object to String array and set it in ListAdapterHow can i do that?

414