HiveBrain v1.2.0
Get Started
← Back to all entries
principlejavaMinor

Spotify Cat vs. Dog Challenge

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
dogspotifycatchallenge

Problem

I implemented an algorithm to solve a constraint satisfaction problem. It is a non-trivial one, so I do not intend to take your time with its details. I am mainly looking for good code suggestions rather than the specific details on the logic.

How could I made the code better? Any comments/recommendations are appreciated.

```
package com.muratdozen.playground.spotify;

import com.muratdozen.playground.util.io.FastReader;

import java.io.PrintWriter;
import java.util.*;

/**
* Spotify Challenge, Cat vs. Dog
*
* @author Murat Derya Ozen
* @since: 10/2/13 10:47 AM
* @see
* https://www.spotify.com/us/jobs/tech/catvsdog/
*/
public class CatVsDog {

public static class Vote {
public final boolean catLover;
public final int keep;
public final int throwout;

public Vote(final boolean catLover, final int keep, final int throwout) {
this.catLover = catLover;
this.keep = keep;
this.throwout = throwout;
}

public boolean isConflicting(Vote vote) {
return this.catLover != vote.catLover
&& (this.keep == vote.throwout || this.throwout == vote.keep);
}
}

// some kind of a bipartite graph matching
private static final int maxMatchings(final Map> conflictingVotes,
final Map> reverseConflictingVotes) {

// assign each cat lover vote to a conflicting dog lover vote
// we need to find out the maximum number of such assignments (matchings)
// we assign each cat lover vote to a conflicting vote by starting from
// the cat lover vote that has the least number of options (conflicting votes)

// list of cat lover votes sorted by the number of options each vote has
final List catLoversVotes = new ArrayList(conflictingVotes.keySet());
Collections.sort(catLoversVotes, new Comparator() {
@Override
public int compare(Vote v

Solution

Your original:

private static final  void addToMap(final Map> map, final T key, final Vote vote) {
    Set votes = map.get(key);
    if (votes == null) {
        votes = new HashSet();
        votes.add(vote);
        map.put(key, votes);
    } else {
        votes.add(vote);
    }
}


Alternative:

private static final  void addToMap(final Map> map, final T key, final Vote vote) {

    if (null == map.get(key)) {           
        map.put(key, new HashSet());
    } 

    map.get(key).add(vote);        
}


Edit to my preferred syntax:

private static final  void addToMap(final Map> map, final T key, final Vote vote) {
   if (!map.containsKey(key)) {           
     map.put(key, new HashSet());
   } 
   map.get(key).add(vote);        
}

Code Snippets

private static final <T> void addToMap(final Map<T,Set<Vote>> map, final T key, final Vote vote) {
    Set<Vote> votes = map.get(key);
    if (votes == null) {
        votes = new HashSet<Vote>();
        votes.add(vote);
        map.put(key, votes);
    } else {
        votes.add(vote);
    }
}
private static final <T> void addToMap(final Map<T,Set<Vote>> map, final T key, final Vote vote) {

    if (null == map.get(key)) {           
        map.put(key, new HashSet<Vote>());
    } 

    map.get(key).add(vote);        
}
private static final <T> void addToMap(final Map<T,Set<Vote>> map, final T key, final Vote vote) {
   if (!map.containsKey(key)) {           
     map.put(key, new HashSet<Vote>());
   } 
   map.get(key).add(vote);        
}

Context

StackExchange Code Review Q#32303, answer score: 6

Revisions (0)

No revisions yet.