# Writing a Search Function

**URL:** https://discourse.processing.org/t/writing-a-search-function/11403
**Category:** Coding Questions
**Created:** [May 20, 2019, 4:49am UTC](https://discourse.processing.org/t/writing-a-search-function/11403 "2019-05-20T04:49:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)
#### Post date: [May 20, 2019, 4:49am UTC](https://discourse.processing.org/t/writing-a-search-function/11403/1 "2019-05-20T04:49:28Z")

</div>

Hello,

I have a program that creates custom objects from text documents that contain certain pieces of data. Each object has its own Boolean tags that correspond to different states the object could be under based upon the data in the text file. A sample of the object would look something like this.

```auto
class Object {

String [] startingtext;

Boolean datastate1;
Boolean datastate2;
Boolean datastate3;

Object (String [] textfile){
startingtext = textfile;
}

}

```

When the program is finished there will be hundreds of different datastate Booleans possible for each object, each with unique names that correspond to different things I want to track about each object (aka, the Booleans will not have sequential names). A separate function runs that will read each object.textfile and activate the proper datastate Booleans to true. What I would like to be able to do is write a search function that would look something like the following pseudo code:

```auto
SomeSearchFunction(Object [] someobjectarray, Parameter adatastate){
//This code takes in an array of objects and searches for all objects where
//the Parameter variable is true. The Parameter value can be any of the
//Boolean tags. This program would return an Object [] where all the chosen
//Parameter Boolean tags were true.
}

```

In short, I’m a little stumped on how best to accomplish this. Any ideas, examples, or tutorials that might speak to this would be very helpful.

Thank you.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 20, 2019, 6:05am UTC](https://discourse.processing.org/t/writing-a-search-function/11403/2 "2019-05-20T06:05:31Z")

</div>

> [@dtools22](#):
>
> Each object has its own Boolean tags…

If those tags are String names, maybe you should store them in a HashMap container: #️⃣

> **[HashMap / Reference](https://processing.org/reference/HashMap.html)**
>
> A HashMap stores a collection of objects, each referenced by a key. This is similar to an Array, only instead of accessing elements with a numeric index, a String is used. (If you…

```auto
import java.util.Map;
final Map<String, Boolean> tags = new HashMap<String, Boolean>();

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 20, 2019, 7:42am UTC](https://discourse.processing.org/t/writing-a-search-function/11403/3 "2019-05-20T07:42:06Z")

</div>

Gotoloop is right

When you have a central hashmap with all booleans and all objects use this hashmap, then the search can for loop over it and compare a search pattern with the objects

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 20, 2019, 8:12am UTC](https://discourse.processing.org/t/writing-a-search-function/11403/4 "2019-05-20T08:12:35Z")

</div>

On 2nd thought, given we’re simply checking whether an object contains some tag, we can just use a StringList instead: 💡  
[Processing.org/reference/StringList.html](http://Processing.org/reference/StringList.html)

And call its method **hasValue()** in order to check whether a String tag is stored in the container: 🎫  
[Processing.org/reference/StringList\_hasValue\_.html](http://Processing.org/reference/StringList_hasValue_.html)

---

<div class="post-metadata">

### Author: ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)
#### Post date: [May 20, 2019, 8:49am UTC](https://discourse.processing.org/t/writing-a-search-function/11403/5 "2019-05-20T08:49:11Z")

</div>

I like @GoToLoop suggestion, but in this case maybe Java `Set` is even better thanks to `containsAll` method:

```auto
class MyObject {
  Set<String> tags = new HashSet<>();
  ...
}

MyObject[] search(MyObject[] objects, Set<String> tags) {
  List<MyObject> result = new LinkedList<>();
  for (MyObject object : objects) {
    if (object.tags.containsAll(tags)) {
      result.add(object);
    }
  }
  return result.toArray(new MyObject[0]);
}

```

It should be already quite fast thanks to hash functions, but if the performance is a concern, you might consider calling `intern()` on each string.

---

<div class="post-metadata">

### Author: ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)
#### Post date: [May 20, 2019, 8:59am UTC](https://discourse.processing.org/t/writing-a-search-function/11403/6 "2019-05-20T08:59:53Z")

</div>

The more idiomatic Java 8 way would be:

```auto
class MyObject {
  Set<String> tags = new HashSet<>();
}

MyObject[] search(MyObject[] objects, Set<String> tags) {
  return Stream.of(objects)
    .filter(myObject -> myObject.tags.containsAll(tags))
    .toArray(MyObject[]::new);
}

```

But I believe this one cannot be used in Processing directly due to lack of lambdas. Still it can be used when developing on top of Processing directly in Java, like I do in this project: [https://github.com/morisil/processing-shaders/](https://github.com/morisil/processing-shaders/)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 20, 2019, 10:12am UTC](https://discourse.processing.org/t/writing-a-search-function/11403/7 "2019-05-20T10:12:02Z")

</div>

> [@morisil](#):
>
> But I believe this one cannot be used in Processing directly due to lack of lambdas.

Not only that, but even the empty `<>` diamond can’t be used inside “.pde” files, only in “.java” 1s! ☹

And lambdas aren’t allowed even in “.java” files under the Processing’s IDE (PDE)! 😲

---

<div class="post-metadata">

### Author: ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)
#### Post date: [May 24, 2019, 12:55pm UTC](https://discourse.processing.org/t/writing-a-search-function/11403/8 "2019-05-24T12:55:30Z")

</div>

Thanks for the suggestions everyone. I am still testing out my code but so far it looks like the first suggestion by GoToLoop works the way I want it to. I can add all the Boolean tags that an object might have into a HashMap by String name, then type that String name in as the parameter in the search function, if the parameter has a stored value in the HashMap then I can find out if that Boolean is true or false and run a for loop to cover each object.

That seems like it is doing exactly what I need. I haven’t tried any of the other methods but I will to see how each method functions.
