CrazySabbath
CrazySabbath

Reputation: 1334

Remove non-object From List<Object>

As topic states, I want to remove non-objects from List of objects.

I have a web service (xml file with some data) and generated classes from XSD file. Among generated XSD classes there is a class ASMENYS and it has method getContent(). This method returns a list of generated.ASMUO objects.

Now the problem is, that this method for some reason returns empty lines (spaces?) among objects. I need to iterate through asmenysList, and because of empty spaces I'm forced to use if() which deprecates my code performance...Any ideas? Why are those empty spaces generated? Or maybe some ideas how to better filter them without iteration if possible?

//m here is generated.ASMENYS object,.: generated.ASMENYS@10636b0
List<Object> asmenysList = ((ASMENYS) m).getContent();
System.out.println(asmenysList);

[
, generated.ASMUO@1799640, 
, generated.ASMUO@b107fd, 
, generated.ASMUO@10636b0, 
...
, generated.ASMUO@1df00a0, 
]

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlRootElement(name = "ASMENYS")
public class ASMENYS {

@XmlElementRef(name = "ASMUO", type = ASMUO.class, required = false)
@XmlMixed
protected List<Object> content;

/**
 * Gets the value of the content property.
 * 
 * <p>
 * This accessor method returns a reference to the live list,
 * not a snapshot. Therefore any modification you make to the
 * returned list will be present inside the JAXB object.
 * This is why there is not a <CODE>set</CODE> method for the content property.
 * 
 * <p>
 * For example, to add a new item, do as follows:
 * <pre>
 *    getContent().add(newItem);
 * </pre>
 * 
 * 
 * <p>
 * Objects of the following type(s) are allowed in the list
 * {@link ASMUO }
 * {@link String }
 * 
 * 
 */
public List<Object> getContent() {
    if (content == null) {
        content = new ArrayList<Object>();
    }
    return this.content;
}

}

EDIT

/**
 * Gets the value of the asmId property.
 * 
 */
public int getAsmId() {
    return asmId;
}

List<Object> asmenysList = ((ASMENYS) m).getContent();
for(Object asm : asmenysList){
    //trying to cast empty asmenysList element, gives me an error.
    //that's why I'm forced to use if() as I said before.
    if(asm.getClass().getName() == "generated.ASMUO"){
        int asm_id = ((ASMUO) asm).getAsmId();
    } 
}

Upvotes: 0

Views: 150

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

By definition a List<Object> only holds Object(s). If there are primitive types in there, they must be autoboxed. If it's an array, that is an Object. The only empty spaces I see are from the toString() method of List.

Edit

based on your edit,

if(asm.getClass().getName() == "generated.ASMUO"){

Is not how you test String equality,

if(asm.getClass().getName().equals("generated.ASMUO")) {

Upvotes: 2

Related Questions