fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Arrays;
  7. import java.util.stream.Collectors;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. LinkedList<Pet> pets = new LinkedList<>();
  15. pets.add(new CAT());
  16. pets.get(0).setName("Meow0");
  17. pets.add(new DOG());
  18. pets.get(1).setName("Woof0");
  19. pets.add(new CAT());
  20. pets.get(2).setName("Meow1");
  21. pets.add(new DOG());
  22. pets.get(3).setName("Woof1");
  23. pets.add(new CAT());
  24. pets.get(4).setName("Meow2");
  25.  
  26. System.out.println(Ideone.dequeueDog(pets).getName());
  27. }
  28.  
  29. public static class Pet {
  30. private String name;
  31.  
  32. public String getName() {
  33. return name;
  34. }
  35.  
  36. public Pet setName(String name) {
  37. this.name = name;
  38. return this;
  39. }
  40. }
  41.  
  42. public static class CAT extends Pet{}
  43.  
  44. public static class DOG extends Pet{}
  45.  
  46. private static Pet dequeueDog(LinkedList<Pet> linkedList) {
  47.  
  48. List<Pet> dogList = linkedList.stream().filter(u -> u.getClass() == DOG.class).collect(Collectors.toList());
  49. int locationDog = linkedList.lastIndexOf(dogList.get(dogList.size()-1));
  50. return linkedList.remove(locationDog);
  51. }
  52. }
  53.  
Success #stdin #stdout 0.25s 320832KB
stdin
Standard input is empty
stdout
Woof1