Skip to content

Instantly share code, notes, and snippets.

@minhtran83
Created March 10, 2018 12:35
Show Gist options
  • Select an option

  • Save minhtran83/5f6c554682889835e4b006018b1f9932 to your computer and use it in GitHub Desktop.

Select an option

Save minhtran83/5f6c554682889835e4b006018b1f9932 to your computer and use it in GitHub Desktop.
Truong Nguyen interview
package interview;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.incubator.http.HttpClient;
import jdk.incubator.http.HttpRequest;
import jdk.incubator.http.HttpResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
public class Interview {
public static void main(String[] args) throws URISyntaxException, InterruptedException, ExecutionException, IOException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://gist.githubusercontent.com/samuraitruong/2451c6cb7596863f1b15993334b08c52/raw/006040d3b09f309c370c9582118b346ce43d609a/pets.json"))
.GET()
.build();
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandler.asString());
Thread.sleep(3000);
if(response.isDone()) {
String body = response.get().body();
ObjectMapper objectMapper = new ObjectMapper();
List<Person> people = objectMapper.readValue(body, new TypeReference<List<Person>>(){});
Map<String, Set<String>> result = new TreeMap<>(Comparator.reverseOrder());
people.forEach(person -> {
Set<String> cats = result.computeIfAbsent(person.getGender(), k -> new TreeSet<>(Comparator.naturalOrder()));
if (person.getPets() != null) {
cats.addAll(
person.getPets().stream()
.filter(pet -> pet.getType() == AnimalType.Cat)
.map(Pet::getName).collect(Collectors.toSet())
);
}
});
System.out.println(result);
} else {
response.cancel(true);
System.out.println("Request took more than 5 seconds... cancelling.");
}
}
private enum AnimalType {
Cat,
Dog,
Fish
}
private static class Pet {
private String name;
private AnimalType type;
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
AnimalType getType() {
return type;
}
void setType(AnimalType type) {
this.type = type;
}
}
private static class Person {
private String name;
private String gender;
private byte age;
private List<Pet> pets;
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
String getGender() {
return gender;
}
void setGender(String gender) {
this.gender = gender;
}
byte getAge() {
return age;
}
void setAge(byte age) {
this.age = age;
}
List<Pet> getPets() {
return pets;
}
void setPets(List<Pet> pets) {
this.pets = pets;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment