### Question:
AcceptableLanguage { public static Locale bestCandidate(List<AcceptableLanguage> ranges, Collection<Locale> candidates) { List<AcceptableLanguage> sorted = ranges.stream().sorted(COMPARATOR).collect(toList()); return candidates .stream() .map(m -> new Candidate(index(sorted, m), m)) .filter(c -> c.index != Integer.MAX_VALUE) .sorted() .findFirst() .map(c -> c.locale) .orElse(null); } private  AcceptableLanguage(String primaryTag, String subTags, double quality); String getPrimaryTag(); String getSubTags(); double getQuality(); Locale asLocale(); boolean includes(Locale tag); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); static List<AcceptableLanguage> valuesOf(String values); static AcceptableLanguage valueOf(String value); static Locale bestCandidate(List<AcceptableLanguage> ranges, Collection<Locale> candidates); static boolean accepts(List<AcceptableLanguage> ranges, Locale candidate); static final AcceptableLanguage ANY; }### Answer:
@Test public void testBestCandidate() { List<AcceptableLanguage> languages = AcceptableLanguage.valuesOf("da, en-gb;q=0.8, en;q=0.7"); assertThat(bestCandidate(languages, Arrays.asList(new Locale("en", "US"), new Locale("en", "GB"))), equalTo(new Locale("en", "GB"))); assertThat(bestCandidate(languages, Arrays.asList(new Locale("en", "US"), new Locale("da"))), equalTo(new Locale("da"))); assertThat(bestCandidate(languages, Arrays.asList(new Locale("en", "US"), new Locale("en", "BE"))), equalTo(new Locale("en", "US"))); assertThat(bestCandidate(languages, Arrays.asList(new Locale("nl", "BE"), new Locale("fr", "BE"))), nullValue()); }