1

I have python background so please allow me have my code in python.

In Java (Android) have an arrayList<customObject>. In each customObject corresponds a boolean (for visibility).

I want to perform operations as more efficient as possible to check the visibility boolean. In python I would create a generator.

Assuming my schema is :

list = [{"item": customObject, "visible": boolean}, {...}, {...}]
visible_matches = [x for x in list if x['visible']] 

for match in visible_matches: 
    dosomething(match)

or an alternative schema:

list = [[ customObject, boolean], [...], [...]]
visible_matches = [x for x in list if x[1]]

How could I perform the same in Java?

arrayList<arrayList<boolean,customObject>> or arrayList< SimpleEntry<"item",customObject>, SimpleEntry<"visible",boolean> >

look a really dirty solutions to me. Is there any simpler way to achieve this?

4
  • 2
    There's no generator in your Python code example... Commented Feb 13, 2014 at 19:55
  • @brunodesthuilliers [x for x in list if x['visible']] IS a generator. Commented Feb 13, 2014 at 19:59
  • @John No! That's a list-comprehension! In python a generator means either generator expressions (which are not list-comprehensions) or function generators (function that contain a yield). Commented Feb 13, 2014 at 20:04
  • @Bakuriu You are completely right. When I read it the first time, I read list(x for x in list if x['visible']). Now I see there is no list. It's the difference between () and []. Sorry about that! Commented Feb 13, 2014 at 20:09

2 Answers 2

1

Use a java.util.Map<CustomObject, Boolean> which maps objects to its visible state.

EDIT Getting all visible items in a map:

Map<CustomObject, Boolean> items = ...;
List<CustomObject> visibleItems = new ArrayList<CustomObject>();

for (Map.Entry<CustomObject, Boolean> entry : items.entrySet()) {visibleItems
    CustomObject customObject = entry.getKey();
    Boolean objectVisible = entry.getValue();

    if (objectVisible) {
        visibleItems.add(customObject);
    }
}

I really do miss something like closures in Java...

Sign up to request clarification or add additional context in comments.

Comments

0

Java doesn't have generators in the same sense that Python does (iterators are not exactly the same thing). Creating custom objects on the fly like you did in Python doesn't work either. Because Java is strongly typed, you must define your custom object before you can use it. Python is not strongly typed.

In a separate file, try this starting code. I named mine class MyCustomObject because it's inside MyCustomObject.java. The names have to match.

public class MyCustomObject {

    public boolean visible;

    // Constructor
    public MyCustomClass(boolean visible) {
        this.visible = visible;
    }

}

Now that you have a custom object, you can instantiate and manipulate it like this in your main class. I called mine class Main because again, it's in Main.java.

public class Main {

    public static void main(String[] args) {

        ArrayList<MyCustomObject> list = new ArrayList<>();
        list.Add(new MyCustomObject(true));
        list.Add(new MyCustomObject(false));
        list.Add(new MyCustomObject(true));
        list.Add(new MyCustomObject(true));

        ....

        for (MyCustomObject potentialMatch: list) {
            if(obj.visible) 
                doSomething(potentialMatch);
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.