# QueryMap

This BIF will iterate over each row in the query and invoke the callback function for each item so you can do any operation on the row and return a new value that will be set at the same index in a new query.

The callback function will be passed the row as a struct, the current row number (1-based), and the query itself.

* If the callback requires strict arguments, it will only receive the row as a struct.
* If the callback does not require strict arguments, it will receive the row as a struct, the row number (1-based), and the query itself.

## Parallel Execution

If the `parallel` argument is set to true, and no `max_threads` are sent, the map will be executed in parallel using a ForkJoinPool with parallel streams. If `max_threads` is specified, it will create a new ForkJoinPool with the specified number of threads to run the map in parallel, and destroy it after the operation is complete. Please note that this may not be the most efficient way to map, as it will create a new ForkJoinPool for each invocation of the BIF. You may want to consider using a shared ForkJoinPool for better performance.

## Method Signature

```
QueryMap(query=[query], callback=[function:Function], parallel=[boolean], maxThreads=[any], virtual=[boolean])
```

### Arguments

| Argument     | Type                | Required | Description                                                                                                                                                                                                                                                                             | Default |
| ------------ | ------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `query`      | `query`             | `true`   | The query to iterate over                                                                                                                                                                                                                                                               |         |
| `callback`   | `function:Function` | `true`   | The function to invoke for each item. The function will be passed 3 arguments: the row, the currentRow, the query. You can alternatively pass a Java Function which will only receive the 1st arg.                                                                                      |         |
| `parallel`   | `boolean`           | `false`  | Whether to run the filter in parallel. Defaults to false. If true, the filter will be run in parallel using a ForkJoinPool.                                                                                                                                                             | `false` |
| `maxThreads` | `any`               | `false`  | <p>The maximum number of threads to use when running the filter in parallel. If not passed it will use the default number of threads for the ForkJoinPool.<br>If parallel is false, this argument is ignored. If a boolean is passed it will be used as the virtual thread argument</p> |         |
| `virtual`    | `boolean`           | `false`  | Whether to use virtual threads when running the filter in parallel. Defaults to false. Ignored if parallel is false.                                                                                                                                                                    | `false` |

## Examples

### Maps query results

Manipulates query column

[Run Example](https://try.boxlang.io/?code=eJxlj0FLxDAQhc%2FJr3jkIF0Iwnq09NY9LNgVoeBBPMTd6RrdpmuaGIrsf3dSZUU9Zebjvbw3jtKICm%2BR%2FLShVEDZnQ42HEhpnl2gPXn9bvz22XhGD5DiQwrBMoVrLHWev%2FS8qpoSTdhRRyaMaH3sjVNSnPQv19UfV2Mckjm8jhgcmmGYLfIRi1ImbwPVsT8WcLkqI9lFtw2Wpb053uXiBYybcPv0ggU4xnZFXi7XNaoKS4Z5a9ftzYpPVZvVPWfi4oeWUngK0buMSnmSHDV%2FzPL5bcx3vj5n%2Fi93xp8Zw2OR)

```java
news = queryNew( "id,title", "integer,varchar", [ 
	{
		"id" : 1,
		"title" : "Dewey defeats Truman"
	},
	{
		"id" : 2,
		"title" : "Man walks on Moon"
	}
] );
writeDump( news );

function mapQuery( any Obj ) {
	if( Obj.ID == 1 ) Obj.TITLE = "NEW: " & Obj.TITLE;
	return Obj;
}
newQuery = QueryMap( news, mapQuery );
writeDump( newQuery );

```

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJx1UE1Lw0AQPWd%2FxWNPCQ6SCNaGUEENHgQrIp5CD2kz1YD5cJIYgvjfnZUKIukyC7Mz7828fS037RtjhceBZVrz6MPWecVUNFvKX9gS7Ecuu9dcqMh7prLutZbBeJnxPPs0dKUlzW6EtZ3q9RHFFyEh0kDgeqHxNnQgPEs3g4%2FPj%2BFvhYsZwuLogruymsEvl%2F%2FxZoMgMcKdfv7dff4%2Bb320P34QfFzVE6QZ6TdZD9WW5fDkXSNFxz0CrC7xaTwFZLDOMGx0oFublvu9ujnpUccUcJo%2BXBP%2B6joLowVBI9ZBAU4QJTqK%2B0FqR0jMl9M4SqmEoVJ5Tq5WvgF9r3EJ)

```java
people = QueryNew( "name,dob,age", "varchar,date,int", [ 
	[
		"Susi",
		CreateDate( 1970, 1, 1 ),
		0
	],
	[
		"Urs",
		CreateDate( 1995, 1, 1 ),
		0
	],
	[
		"Fred",
		CreateDate( 1960, 1, 1 ),
		0
	],
	[
		"Jim",
		CreateDate( 1988, 1, 1 ),
		0
	]
] );
res = queryMap( people, ( Any row, Any rowNumber, Any recordset ) => {
	row[ "age" ] = DateDiff( "yyyy", row.DOB, CreateDate( 2016, 6, 9 ) ) + 1;
	return row;
} );
writeDump( res );

```

## Related

* [QueryAddColumn](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryaddcolumn)
* [QueryAddRow](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryaddrow)
* [QueryAppend](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryappend)
* [QueryClear](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryclear)
* [QueryColumnArray](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querycolumnarray)
* [QueryColumnCount](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querycolumncount)
* [QueryColumnData](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querycolumndata)
* [QueryColumnExists](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querycolumnexists)
* [QueryColumnList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querycolumnlist)
* [QueryCurrentRow](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querycurrentrow)
* [QueryDeleteColumn](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querydeletecolumn)
* [QueryDeleteRow](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querydeleterow)
* [QueryEach](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryeach)
* [QueryEvery](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryevery)
* [QueryFilter](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryfilter)
* [QueryGetCell](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querygetcell)
* [QueryGetResult](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querygetresult)
* [QueryInsertAt](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryinsertat)
* [QueryKeyExists](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querykeyexists)
* [QueryNew](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querynew)
* [QueryNone](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querynone)
* [QueryPrepend](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryprepend)
* [QueryRecordCount](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryrecordcount)
* [QueryRecordCount](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryrecordcount)
* [QueryReduce](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryreduce)
* [QueryRegisterFunction](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryregisterfunction)
* [QueryReverse](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryreverse)
* [QueryRowData](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryrowdata)
* [QueryRowSwap](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryrowswap)
* [QuerySetCell](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querysetcell)
* [QuerySetRow](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querysetrow)
* [QuerySlice](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/queryslice)
* [QuerySome](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querysome)
* [QuerySort](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querysort)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/query/querymap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
