Sorting in java for Map? - comparator

As we use Collections.sort(list) for Collection but can we able to use that in the case of map like by implementing Comparable and Comparator? Or we can just do iterator over a Map in java?
========================================================================

An important feature of a list is that there is an order to the elements, whereas within a Map there is no such order. Therefore it doesn't make sense to sort a Map. If order is important to you, you really should be using a list.

For that purpose, we can use a treemap. Each entry we insert are automatically sorted
For the treemap to sort, you have to implement the comparable or comparator in the object(key) that you wish to put in treemap
Check out this link
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html

Related

Swap the position of map item

How can I swap an item's order in a TreeMap in Kotlin? Something like Collections.swap() but on a map, not on a list. Like swapping an item's position with another item.
A TreeMap is a self-sorting collection, so you can't swap elements. The map elements gets sorted in a tree according to their natural ordering (if they implement Comparable interface) or according to a custom Comparator that you need to provide to the map.
More info in the JavaDoc: https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
If arbitrary ordering (and/or re-ordering) is important to you, you may want to consider other kinds of collections, like List for example, or a combination of multiple collections.

Creating a dictionary/map in Coq

I want to be able to have a map from any type to any type in Coq. So far I have had some success using Coq.FSets.FMapList from the standard library, but I have only been able to create maps where the key is a natural number, by doing the following.
Require Import Coq.FSets.FMapList.
Require Import Coq.Structures.OrderedTypeEx.
Module Import NatMap := FMapList.Make(Nat_as_OT).
(* whatever I want to do with my NatMap *)
I know that NatMap := FMapList.Make(Nat_as_OT). is declaring that I want to use a map whose keys are Nat_as_OT. However, FMapList.Make will only accept an OrderedType as an argument. Its there a way I can make my own OrderedType? Or is there a better way of creating a map?
You won't easily find maps from any type to any type, because for such a map to work you at least need to be able to distinguish two element of the type used for keys. If you only have this capability (testing whether two elements are the same), then maps can be implemented in a fairly inefficient way: you basically handle a list of pairs (key, value) and the cost of retrieving the value associated to a key is proportional to the number of keys that are already handled in your map, on average.
If you want to be slightly more efficient, a usual trick is to have hash keys, but you need to be able to compute hash values, so your type cannot be completely arbitrary.
In the end, there is the possibility to use maps based on a type that has an order. In this case, it is quite easy to implement the map is such a way that retrieving the value for a key with a cost that is logarithmic in the number of keys, on average.
Now, how do you create an OrderedType object? You need to instantiate module type MiniOrderedType. So, for your type you need to say what function will play the role of eq, lt, and show the various important properties listed in the MiniOrderedType module type (see this file). There are several example of this construction in this example file).
a map from any type to any type in Coq
To refine Yves's answer, the only thing that is a map from any type A to any type B in Coq is the function space A -> B (or A -> option B)
That being said, this is not such a bad type for maps, once you add functional extensionality, but of course it lacks some basic operations that could be added with a bit of effort.

OpenLayers ::: Counting the houses in a polygon?

I need to be able to count all the houses/addresses that exist inside a polygon. I've thought it through and it seems possible to find if a single address exists inside a polygon (by finding if the geolocation intersects with it?)
Is it possible to pull a list of all the addresses in an area and check if they intersect? Or might there be some other method?
If I were to try to do this on the browser client, I would get an array of OpenLayers.LonLat objects or OpenLayers.Geometry.Point objects. I would iterate over that array and ask if each object was within a polygon by calling the containsPoint function of OpenLayers.Geometry.Polygon. I would count or collect the points that fall within the polygon.
Geospatial operations like you are describing are something we do on the server using open source GeoTools. We tend to use OpenLayers almost exclusively for map visualization.
I may have not answered your question entirely because I sense you are trying to get that list of addresses/points in the first place for an area on the map, and I don't think you've provided enough information to answer that part of the question.

Working with google maps api

I am trying to build a map based query interface for my website and I am having difficulty finding a starting point besides http://developer.google.com. I assume this is a rather simple task but I feel as though I am on a wild goose chase. Anyway the problem is the existing site places people into a category based on their address (primarily the zip code), this is not working out because of odd shapes and user density so I would like to solve the problem by creating custom zones.
I am not looking for a proprietary solution because I would really like to accomplish this on my own, I just need some better places to start or better suggestions for searches.
I understand that I will need to create a map with my predetermined polygons.
I understand how to create a map with polygons via js.
I do not understand how data will request which zone it is within and how it will return it as a hash I can store. eg. user=>####, zone=>####, section=>#####
http://blog.appdelegateinc.com./point-in-polygon-checking-with-google-maps.html
has some JS you can add to give the ability to test whether a point is within a polygon (sample: http://blog.appdelegateinc.com./static/samples/point_in_polygon.html ) using this approach: http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
I think as you place the markers, you'll hold them in an array (of objects)...then loop through, doing some sort of reduction of which polygons to test, testing those that remain, if inPoly, set marker.zone and marker.section to whatever suits your needs

couchdb complex map reduce over multiple documents give back single json object

How to create a complex map reduce function in couchdb to span a view over multiple documents with same attribute names to give back a single json object?
What is the most efficient way to manage this?
Is a nested set/source algorithm suitable for couchdb (changes are very write intensive)?
If you want the whole documents you shouldn't use reduce. Just do a proper map function for the "type" attribute you talked about earlier and query the view with include_docs=true.

Resources