Question

I have a List in Java whose isEmpty() method returns false for some entries. Also when i check size for those entries it is One but when i print the List it is empty [].Is it because of whitespaces that List size is coming out to be One My Code is like this

    String str= "Meeting is scheduled";
      String[] strArray = str.split(" ");
      for(int i=0;i<strArray.length;i++){
    // Retrieve all IWiktionaryEntries for the words in a String "str".
        List<IWiktionaryEntry> entries = wkt.getEntriesForWord(strArray[i].trim());
        if(strArray[i].trim().length()>3){
            if(entries.size()!=0){
                IWiktionaryEntry Iwikiword=entries.get(0);
                List<IWikiString> words =Iwikiword.getGlosses();
                Iterator<IWikiString> wordItr = words.iterator();
                int k=0;
                System.out.println("Related Meanings of:"+strArray[i].trim().toUpperCase());
                System.out.println("Word List Size:"+words.size());
                System.out.println("Word List:"+words);
                System.out.println("Is List Empty:"+words.isEmpty());

                while(wordItr.hasNext()){
                    //System.out.println("["+k+"]"+":"+(IWikiString)wordItr.next());
                    System.out.println(k+":"+(IWikiString)wordItr.next());
                    k++;
                }
            }
            else{
                System.out.println("*****No suitable meaning found for the word-"+strArray[i].trim().toUpperCase());
            }
          }
      }

This is what i see in console : Related Meanings of:MEETING Word List Size:1 Word List:[] Is List Empty:false

0:


Related Meanings of:SCHEDULED Word List Size:1 Word List:[] Is List Empty:false 0:

Was it helpful?

Solution

It looks like you have one element in your list, and that element's toString() method returns the empty String. This produces the output [] while returning false for isEmpty():

List<String> output = Arrays.asList("");
System.out.println(output);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top