Hacks

Build your favorite sort into your custom built collection, aka LinkedList

  • Implement a Sort into your LL in Jupyter Notebook ... Here is concept.- Utilize Capabilities of Object overrides with toString and compareTo to Sort using Object overrides (these are built in by extending Collectable, formerly Generics). Build toString to return JSON for LinkedList, Queue and/or Stack so they could be utilized with API.

  • Demostrate changing Sort keys with Tester Methods

  • Look at this for FrontEnd ideas using these new Data Ops

public class Animal{ 
    private String name; //ignore from here to 
    private int age;

    public Animal(){
        this.name = "default";
        this.age = 0;
    };

    public Animal(String a, int b){
        name = a;
        age = b;
    };

    public String getName(){
        return name;
    };  

    public int getAge(){
        return age;
    };                              //here

    //focus on this toString method, later below we will how does the object override works
    public String toString(){
        return "Animal name: " + name  + ", age: " + age;
    }

}

//Inheritance, class Bird is a subclass to class Animal
public class Bird extends Animal{     
    private String type;

    public Bird(){          //Skip here to 
        super();
        this.type = "default";
    };

    public Bird(String a, int b, String Birdtype){
        super(a, b);
        this.type = Birdtype;
    };

    public String getType(){
        return type;
    };                      //here
                                

    //Remember this toString method, we will use it later.
    public String toString(){
        return "Bird name: " + getName() + ", type: " + type + ", age: " + getAge();
    }
}

Animal list = new Bird("Landroval", 1, "sparrow"); //Animal type reference refers to a Bird object 
System.out.println(list); //Bird's toString is called. This is called RUN TIME POLYMORPHISM.
Bird name: Landroval, type: sparrow, age: 1
[Animal name: Landroval, age: 1, Bird name: Landroval, type: sparrow, age: 1]
  • It is using the toString method from the subclass Bird
  • Method override : When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
LinkedList<Animal> test = new LinkedList<Animal>();
test.add(new Animal("Landroval", 1));
test.add(new Bird("Landroval", 1, "sparrow"));
System.out.println(test);

LinkedList<Bird> test2 = new LinkedList<Bird>();
test2.add(new Bird("Landroval", 1, "sparrow"));
System.out.println(test2);

System.out.println(test.toString().compareTo(test2.toString()))
[Animal name: Landroval, age: 1, Bird name: Landroval, type: sparrow, age: 1]
[Bird name: Landroval, type: sparrow, age: 1]
-1

return as JSON

@GetMapping("/{a}/{b}/{c}")
public ResponseEntity<String> Bird(@PathVariable("a") String a, @PathVariable("b") int b, @PathVariable("c") String c) throws JsonMappingException, JsonProcessingException {

  LinkedList<Animal> test = new LinkedList<Animal>();
  test.add(new Bird("Landroval", 1, "sparrow"));

  return ResponseEntity.ok(test.toString());  // JSON response, see ExceptionHandlerAdvice for throws
}

Or

@GetMapping("/{a}/{b}/{c}")
public ResponseEntity<String> Bird(@PathVariable("a") String a, @PathVariable("b") int b, @PathVariable("c") String c) throws JsonMappingException, JsonProcessingException {

  LinkedList<Animal> test = new LinkedList<Animal>();
  test.add(new Bird("Landroval", 1, "sparrow"));

  ObjectMapper mapper = new ObjectMapper(); 
  JsonNode json = mapper.readTree(test.toString()); 

  return ResponseEntity.ok(json);  // JSON response, see ExceptionHandlerAdvice for throws
}