add and delete hashset objects in Java


I have Skills pojo class.SkillsParts is a string of array.Using skillsParts i am settings Skillsname.My Problem is that first time i set to null and then parse SkillsParts,Creating Skills new object & add it to set,then update.But i want avoid this if it is present then remove and if not present then add.

Pojo class

package com.cloudcodes.gdirectory.pojo;


public class Skills implements Serializable{

    private static final long serialVersionUID = 1L;
    private Long skillId;
    private String skillName;
    private String skillDesc;
    private Long categoryId;

    public Long getSkillId() {
        return skillId;

    public void setSkillId(Long skillId) {
        this.skillId = skillId;

    public String getSkillName() {
        return skillName;

    public void setSkillName(String skillName) {
        this.skillName = skillName;

    public String getSkillDesc() {
        return skillDesc;

    public void setSkillDesc(String skillDesc) {
        this.skillDesc = skillDesc;

    public Long getCategoryId() {
        return categoryId;

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;


This is my relation table

    private java.util.Set<Skills> skillsList = new HashSet<Skills>();

This is my controller

        public  @ResponseBody String saveSkills(HttpServletRequest request,@RequestParam String skills,@RequestParam String Email) throws IOException

            Domain domain1 = (Domain)request.getSession().getAttribute("Domain");
            Long domanId =domain1.getDomainId();
            String[] skillsParts = skills.split(",");
            UserProfile user = userProfileManager.getUserByEmail(domain1.getPrimary_Domain_Id(), Email);

                /* Here is problem  */
            Set<Skills> remove = new HashSet<Skills>();
            for(int i =0;i<skillsParts.length;i++){

                Skills skillObj = new Skills();

            Gson gson = new Gson();
            return gson.toJson(user);

To achieve what you want, you need to persist the pojo Skills in a table and define equals()/hashCode() for it. As far as I can see, the business key here is the name of the skill, so that's what you need to check. Don't use the skillId!!

In the controller, you need to ask Hibernate for Skills by name instead of creating them yourself. If Hibernate can't find one, create a new one, persist it and add it to the hash. Hibernate will then do the right thing.