Answers for "remove from hashmap java"

1

TreeMap remove(Object key) method in java

import java.util.TreeMap;
public class TreeMapRemoveMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      tm.put(32, "pineapple");
      tm.put(51, "watermelon");
      tm.put(38, "grapes");
      tm.put(69, "mango");
      tm.put(58, "apple");
      System.out.println("Given TreeMap is: " + tm);
      // remove existing key mapping
      String strReturn = (String)tm.remove(38);
      System.out.println("Returned value is: " + strReturn);
      System.out.println("New TreeMap is: " + tm);
   }
}
Posted by: Guest on October-28-2020
0

spring delete mapping

package com.zetcode.controller;

import com.zetcode.model.Post;
import com.zetcode.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Set;

import static org.springframework.http.ResponseEntity.ok;

@Controller
@RequestMapping("/posts")
public class MyController {

    @Autowired
    private IPostService postService;

    @GetMapping
    public ResponseEntity<Set<Post>> all() {
        return ok().body(postService.all());
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Long> deletePost(@PathVariable("id") Long id) {

        var isRemoved = postService.delete(id);

        if (!isRemoved) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<>(id, HttpStatus.OK);
    }
}
Posted by: Guest on October-21-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language