Is it possible to get the values in a map associated to a key, without using the key?
I have this code.
val map = prices.associateBy({it.productName},{it.productPrice})
which gives me this
{Coffee=3.0, Gum=0.5, Beer=18.0}
I want to be able to just grab all the prices (3.0, 0.5, etc.) and save them to a list.
Any help is appreciated!
So to get all the values from a map, you can just use the built-in values property on the map like this.
val map = prices.associateBy({it.productName},{it.productPrice})
val values = map.values
It returns a read-only Collection of all values in this map. Note that this collection may contain duplicate values.
Related
How to add an element with the same first key to map and save each element there?
'''
var records = mutableMapOf<Int, Map<Int, List<String>>>()
records.put(1, mutableMapOf(Pair(2, listOf<String>("first"))))
records.put(1, mutableMapOf(Pair(3, listOf<String>("second"))))
'''
Maps can only have one value per key, if you add another entry with a key that already exists, it'll overwrite it. You need a value type that's a collection (like List or Set) that you can put multiple things into.
Assuming you want to use Int keys to store items that are Pair<Int, String>, you'll need a MutableMap<Int, MutableList<Pair<Int, String>>> (or a Set, you won't get duplicates and it's unordered usually).
You need to get the collection for a key, and then add to it (or whatever you're doing):
var records = mutableMapOf<Int, MutableList<Pair<Int, String>>>()
// this creates a new empty list if one doesn't exist for this key yet
records.getOrPut(1) { mutableListOf() }.add(Pair(2, "first"))
records.getOrPut(1) { mutableListOf() }.add(3 to "second") // another way to make a Pair
println(records)
>> {1=[(2, first), (3, second)]}
https://pl.kotl.in/AlrSG0KH-
That's a bit wordy so you might want to make some nice addRecord(key, value) etc functions that let you access those inner lists more easily
I'm deserializing an entire document, to keep things in handy and prevent me to check for each value and construct the object I use
myList.add(documentSnapshot.toObject(House::class.java))
Now, lets say House is this
data class House(val name:String,val:address:String)
Now, if I want to also get the House document Id and put it inside my document I do this
data class House(val houseId:String,val name:String,val:address:String)
But after doing that , the first line of code transforms into this
val houseId = documentSnapshot.id
val houseName = docuementSnapshot.getString("name")
val houseAddress = documentSnapshot.getString("address")
myList.add(House(houseId,houseName,houseAddress))
What I want to do is use .toObject() to also map that extra field that is the document id inside of it because if the House object expands in size, I will need to hand write again each property, and now think that house has 100 properties and I just need the id of it inside the object. I will need to write off 99 get fields to just place the document Id inside that house object.
Is there a way to map that id to the object without doing the above and just placing .toObject ?
Thanks
You need just add annotation #DocumentId
data class House(#DocumentId val houseId:String,val name:String,val:address:String)
What I want to do is use .toObject() to also map that extra field that is the document id
This will be possible only if the document already contains the id property that holds the document id as a value. If you only have the name and the address, then you cannot map the id, because it doesn't exist in the document. To be able to map all those three properties you should update each and every document in that collection so it contains the document id. If you have lots of documents in the collection, I recommend you use batch writes.
Right after that, you'll be able to use:
val house = documentSnapshot.toObject(House::class.java)
And now this house object will contain the id too.
I am making an app which downloads LatLngs from firebase and shows them as markers in google maps API, users can add new LatLngs.
In my database I also have the pricepoint and types of markers. In the main screen the user can choose what types of marker he wants to see on the map.
So my application does something like this:
locations.orderByChild(pricepoint).equalTo(choosenPricepoint);
and then I check programmatically if types match those chosen by the user
int type = Integer.parseInt(locations.child("restaurantType").getValue().toString();
if(type == funCode|| type == runingCode|| type == sportsCode
{
mMap.addMarker(new MarkerOptions().position(snapshot.getLatlng);
}
And it works fine with 250 records, but I'm expecting over 10,000 of them in my database so I am worried that it will be too slow.
I don't know if showing markers only where user's maps camera is and deleting other will be faster. What do you suggest?
You can use GeoFire , a firebase library that uses GeoHashes to merge lat+lon into a single property.That way you can do the distance filtering directly on the database.
You should have 2 entries in firebase database,one for setting your object location and one for setting your object with its fields.
As you can see they have the same id.So first you are queriing for nearby object by GeoFire in geo_data entry,and you will get the ids of the object which are nearby,then you can retreive object with its properties directly from database using the ids in my case in user_data entry
I have added all lat/lng to arraylist and .Iwould like to map it to string name and another string along with its id which is an int .Basically I would like to get :
Association : (arraylist)-->Name---->Another Name------>id
how do I do the above association.I am a noob in android and I am using hashmap but it only puts(key,value) which does not satisfy the above condition.
Please let me know how I can implement the above requirement.
I appreciate any help.
Thanks in Advance.
There are a number of ways you could accomplish this. Here are two different ways off the top of my head:
Create an object that will hold the two names and id. Then create a hashmap that maps each lat/long value to the appropriate object. The prototype would be like:
HashMap<Long/Lat, Object>
Or if you don't want to create a new object, then create a hashmap that maps each lat/long value to a hashmap that contains the two names and id. The prototype would be like:
HashMap<Long/Lat, HashMap<String, String>>
I am trying to implement a data structure which allows me to keep track of an index (so I can blindly access the data points), a key (which needs to be there to identify the data in the rest of the program), and a value.
I've looked at a map, but that does not allow me to access the data points without any key. I need some combination of a Queue and a Map. Does this exist and I'm just missing it? Thanks for the help.
I believe what you are looking for is a LinkedHashMap. It will return an ordered collection and you can access values via a key.
LinkedHashMap<Key, Value> myMap = new LinkedHashMap<Key, Value>();
myMap.put(aKey, aValue); //adds to map.
myMap.values(); //returns collection of values
aValue = myMap.get(Key); //returns a value with the given key