Solved Can someone assure me that this will work as I expect it to?

Discussion in 'Plugin Development' started by Tecno_Wizard, Jul 9, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Tecno_Wizard

    I have this method.

    Code:
    public Set<Infraction> getInfractionHistory() {
            return new HashSet<>(configuration.getList(HISTORY_FIELD, new ArrayList<Infraction>()));
        }
    However, IntelliJ is giving me 2 warnings:
    Unchecked assignment: 'java.util.HashSet' to 'java.util.Set<com.tecno_wizard.plugingui.data.Infraction>
    Unchecked call to 'HashSet(Collection<? extends E>)' as a member of raw type 'java.util.HashSet

    Several source I have looked at state this as being a perfectly valid expression, But I wanted to make sure.

    Code:
    package com.tecno_wizard.plugingui.data;
    
    import java.io.Serializable;
    
    /**
    * Created by Ethan Zeigler on 7/7/2015 for PunismentGUI.
    */
    public class Infraction implements Serializable{
        private PunishType type;
        private String reason;
        private long date;
    
        public Infraction(PunishType type, String reason, long date) {
            this.type = type;
            this.reason = reason;
            this.date = date;
        }
    
        public long getDate() {
            return date;
        }
    
        public String getReason() {
            return reason;
        }
    
        public PunishType getType() {
            return type;
        }
    }
    
     
  2. @Tecno_Wizard you normaly define what a collection contains by setting a generic type.
    Code:
        Set<GENERIC_TYPE> someSet = new HashSet<>();
    but you are directly creating the HashSet without setting any generic type
    Code:
        new HashSet<>(); // what does it contain? String, Integer, Apples?
    to fix that, smiply insert the type-arguments
    Code:
        new HashSet<YOUR_TYPE>();
    This way you make sure, that the hashset which will be returnd can only contain the type of objects your returntype (Set<Inraction>) defines. So atm your editor is only complayning because it doesn't know, which kind of objects will be in the HashSet and because you didn't set any type. if there is none, it is called "raw type". This is something you shouldn't use. In your case you can just simply set the type because you know what the set will contain (Infraction). If you don't know what later on will be stored in the hashset, you could use raw type for that, but should rather use "?". so instead of
    Code:
        Set someSet = new HashSet<>();
        // rather use
        Set<?>  someOtherSet = new HashSet<>();
     
  3. Offline

    Tecno_Wizard

    @Shmobi, some thread necromancy going on here. Yes, it worked.
     
  4. @Tecno_Wizard well not that old and it wasn't set to solved and nobody answered so i thought i'll give it a shot ^^
     
  5. Offline

    Tecno_Wizard

Thread Status:
Not open for further replies.

Share This Page