Created
May 1, 2012 04:27
-
-
Save peas/2564994 to your computer and use it in GitHub Desktop.
ideias para a palestra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Palestrante { | |
private String name; | |
private int postsNoGuj; | |
public String getName() { | |
return name; | |
} | |
public int getPostsNoGuj() { | |
return postsNoGuj; | |
} | |
} | |
List<Palestrante> palestrantes = Arrays.asList(); | |
// problema 1: ordenar palestrantes por nome | |
Collections.sort(palestrantes, new Comparator<Palestrante>() { | |
public int compare(Palestrante o1, Palestrante o2) { | |
return 0; | |
} | |
}); | |
// problema 2: calcula a media de posts dos palestrante | |
int soma = 0; | |
for (Palestrante palestrante : palestrantes) { | |
soma += palestrante.getPostsNoGuj(); | |
} | |
double media = (double) soma / palestrantes.size(); | |
// problema 3: como generalizar o calculo da media? | |
interface Counter<T> { | |
int valueFor(T t); | |
} | |
static <T> double average(List<T> lista, Counter<T> contador) { | |
int soma = 0; | |
for (T t : lista) { | |
soma += contador.valueFor(t); | |
} | |
return (double) soma / lista.size(); | |
} | |
double m = average(palestrantes, new Counter<Palestrante>() { | |
public int valueFor(Palestrante p) { | |
return p.getPostsNoGuj(); | |
} | |
}); | |
// pessimo!!! solucao melhor deveria ser algo como: | |
// palestrantes.busca(media()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment