Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:13:28+00:00 2026-06-03T23:13:28+00:00

private Collection<Episode> episodes = new ArrayList<Episode>(); public void checkEpisodes(String string) { for(Episode episode :

  • 0
private Collection<Episode> episodes = new ArrayList<Episode>();

public void checkEpisodes(String string) {
    for(Episode episode : this.episodes){
        System.out.println(string + this.episodes.contains(episode));
    }
}

Why would the above code print false?

I’m using a collection because that’s what ormlite allows. I think the issue may be caused by ormlite anyways, because a very similar class has an identical method that prints true.

What I’m trying to do is modify an object returned by:

public Episode findEpisode(int num) {
    checkEpisodes("Find: ");
    for(Episode episode : this.episodes) {
        if(episode.getNumber()==num) {
            return episode;
        }
    }
    return null;
}

But modification of that object isn’t saved. I’m assuming because it’s not contained within the Collection.

My solution, which works but isn’t ideal:

public void modifyEpisode(Episode episode) {
    checkEpisodes("Modify: ");
    for (Iterator<?> it = this.episodes.iterator(); it.hasNext();) {
        if (((Episode) it.next()).getNumber()==episode.getNumber()) {
            it.remove();
            addEpisode(episode.getNumber(), episode.getLink(), episode.getLinkLabel());
        }
    }
}

If you need to see more of my code, just ask, the project is somewhat involved, but it’s hosted on sourceforge and I can post a link to it if necessary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:13:29+00:00Added an answer on June 3, 2026 at 11:13 pm
    for(Episode episode : this.episodes) {
        System.out.println(string + this.episodes.contains(episode));
    }
    

    Why would the above code print false?

    In a generic sense, using a standard collection, the only way that I can see if if there are bugs in your equals() method. With most collections, contains() iterates through the collection and uses equals(). The Object.equals() would work so it could be that you have overridden the default equals method and there is a bug in it.

    This is also what ORMLite does. If the collection is eager then it calls contains() on the inner ArrayList for the item which would use equals(). If the collection is lazy then it uses the iterator across the table and, again, uses equals() to see if matches.

    Edit:

    Aha. You state that you have not overridden the equals() method.

    One important thing to remember (in terms of ORMLite) is that this is a lazy collection, there is no storage of the objects in memory. When you are iterating across the lazy collection, you get an instance of your Episode object from the database. Then, when you call contains(), it iterates again through the collection creating new Episode objects from the database. It tries to compare the two objects but they will never be equal if you are using Object.equals() because there are not the same object reference.

    You must to override the equals() method to get contains() to work for lazy collections.

    Also, although your post is probably a simplification of your code, you might consider pulling the lazy collection into an array and then iterating across that. You can’t do a contains() on the array but if you needed to search the array, you wouldn’t be iterating twice through the database.

    Edit:

    So the solution turned out to be more complicated. Turns out that the OP had a Show object, with an eager collection of Season objects, each with another eager collection of Episode objects. By default, whenever ORMLite has nested eager collections the inner one is turned into a lazy collection for performance reasons. This is not well documented in version 4.40 unfortunately. You can change this by setting the maxEagerLevel = 2 (or more) on the Show object’s collection. See the documentation on maxEagerLevel.

    @ForeignCollectionField(eager = false, maxEagerLevel = 2)
    ForeignCollection<Season> seasons;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection similar to: Public Class MyCollection Inherits ObservableCollection(Of MyCollection) Private _Name
private void PersonalInfoList_SelectionChanged(object sender, SelectionChangedEventArgs e) { System.Collections.IList list = e.AddedItems; if (e.AddedItems.Count ==
private string _itemId; [Browsable(true), Description(Required identifier for the Item.)] public string ItemId { get
Simplified: Class1 { private Collection<Class2> items; } Class2 { private String name; } Now
I have this class public class Room{ private int n_bed; private float cost; private
I have a collection private ObservableCollection<Contact> _contacts; In the constructor of my class I
I have a private class variable that holds a collection of order items: Private
I have a class which has two HashSet<String> collections as private members. Other classes
these are my entities (simplified): public class IdentificationRequest { private int id; private ICollection<IdentificationRequestStateHistoryItem>
How do you convert the following C# code to VB.NET? private static readonly ICollection<string>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.