# Boids nearest 7 neighbours

**URL:** https://discourse.processing.org/t/boids-nearest-7-neighbours/18272
**Category:** Coding Questions
**Created:** [March 1, 2020, 10:41am UTC](https://discourse.processing.org/t/boids-nearest-7-neighbours/18272 "2020-03-01T10:41:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Blackclo](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@Blackclo](https://discourse.processing.org/u/Blackclo)
#### Post date: [March 1, 2020, 10:41am UTC](https://discourse.processing.org/t/boids-nearest-7-neighbours/18272/1 "2020-03-01T10:41:58Z")

</div>

I’m trying to adapt [https://www.processing.org/examples/flocking.html](https://www.processing.org/examples/flocking.html) limiting the number of neighbours a boid can see to 7 based on closest distance. But I am struggling can someone help

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 1, 2020, 8:08pm UTC](https://discourse.processing.org/t/boids-nearest-7-neighbours/18272/2 "2020-03-01T20:08:07Z")

</div>

Hi,

Welcome to the community! 🙂

You could use the following technique :

- For a specific boid, you compute the distance to all other boids and store them in a Map (like `HashMap<Boid,Float>`, FLoat being the distance and notice that `Float` is an Object).
- You sort the Map based on the values (the distances) and you take only the 7 closest boids.
- You apply the steering behavior to the boid based on those 7 boids

In fact you can generalize this to `n` noids, `n` being the number of boids that it can see.

---

<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: [March 1, 2020, 8:12pm UTC](https://discourse.processing.org/t/boids-nearest-7-neighbours/18272/3 "2020-03-01T20:12:37Z")

</div>

Hey, and welcome to the forum! Nice to have you here.

---

<div class="post-metadata">

### Author: ![Blackclo](https://avatars.discourse-cdn.com/v4/letter/b/d78d45/32.png) [@Blackclo](https://discourse.processing.org/u/Blackclo)
#### Post date: [March 3, 2020, 9:16am UTC](https://discourse.processing.org/t/boids-nearest-7-neighbours/18272/4 "2020-03-03T09:16:53Z")

</div>

Hello again, how does one sort the hashmap based on values?

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 3, 2020, 12:24pm UTC](https://discourse.processing.org/t/boids-nearest-7-neighbours/18272/5 "2020-03-03T12:24:29Z")

</div>

In fact, it’s a little bit tricky :

- [https://www.geeksforgeeks.org/sorting-a-hashmap-according-to-values/](https://www.geeksforgeeks.org/sorting-a-hashmap-according-to-values/) (`Comparator`, `LinkedList`, `LinkedHashMap`)
- [https://dzone.com/articles/how-to-sort-a-map-by-value-in-java-8](https://dzone.com/articles/how-to-sort-a-map-by-value-in-java-8) (Java 8 streams)
