I want to create Empty Mutable Sets from a list of...
# announcements
s
I want to create Empty Mutable Sets from a list of Strings as their name , for Example-
Copy code
var countryList= mutableSetOf<String>()
I want to somehow create empty sets as following -
Copy code
var us= mutableSetOf<String>()
var in= mutableSetOf<String>()
var br= mutableSetOf<String>()
but i get elements in countryList set dynamically from internet , so how do i create empty sets named as above dynamically? us, in br are fetched from internet and added to countryList set dynamically.
n
You need a Map of country name to set
d
hrm... i'm not sure if i'm following. Why do you need to have set of us, in, br, etc? What would be the content of the set? I presume the countryList is actually set of country names, correct?
j
Copy code
val c = listOf('us', 'in', 'br')
val sets = c.associate { it to mutableSetOf<String>() }
That will give you an empty mutable set for each of your country codes, but you might be able to use the functional paradigm to quite succinctly populate the sets too, rather than presumably doing some additional processing later. @deactivateduser is asking a good question
You might find then, that you can make do with immutable sets too
s
CountryList is initially empty ,but while running at runtime I add country codes like in , br , us in it So I cant be sure which elements are there in country list , after fetching and adding members to country list ,now I want to make empty sets of those members with their string name. @deactivateduser do I now make somewhat sense? Or may I clarify more?
I populate the countryList at runtime , so I cant define empty set names before compilation , I want a function of some sort , to read the country List when called and make new empty sets of the name of element in CountryList
d
ok, so the set of "us", "in", "br", etc will actually contain something else correct? not the full name of the country.... then @Jonathan Mew has given a working example....
you need a map for this 😉
s
Yeah , I will try to make one and make it work, I will ask follow ups if needed , thanks to all to show me the direction
j
@Shabinder Singh the thing that might give us more insight is how you will then use those mutableSets - what data will you add to them? Will you add it right after you create them? Will you add or remove data from them later?
s
@deactivateduser @Jonathan Mew handra I used your way of associate , but the thing I am stuck on is , when I use .add etc methods by using countryList[] index , it shows my compilation error , which is natural as it is a string , so how do I now , add or write into the new sets?
d
Can post your code? Having a sample code will probably make things easier to digest....
👍 2
s
Copy code
var dataList = mutableListOf<ProxyList>()
var proxyList = mutableListOf<String>()
var countryList= mutableSetOf<String>()

data class ProxyData(
    val data: MutableList<ProxyList>
    ){
    companion object{

    }
    init {
        dataList = data
    }
}


data class ProxyList(
    val country:String,
    val country_code: String,
    var createdAt: String
    ) {

    init {
        countryList.add(country_code)
    }
}
private fun proxyIntoSet(){
    val c = countryList
    val sets = c.associateWith { mutableSetOf<String>() }
}

private fun organisingProxies(){

    for (proxy in dataList) {
        proxy.country_code.add(proxy.createdAt)
    }
}
all proxylist objects and proxydata objects are created at runtime sample data input to these classes ove iternet is somewhat like this
Copy code
{
  "country":"United States",
  "country_code":"us",
  "createdAt":"123456"
Associate will give me a new key value pair iguess ,so are new empty sets are even being created at runtime @deactivateduser , if possible can u please give some insight on the working
d
So, you encountered error at
proxy.country_code.add(proxy.createdAt)
? What is the purpose of doing this, since you already have the createdAt field anyway? Sorry still can't really grasp what is the ultimate goal. Or maybe you just want to append the createdAt to the country_code ?
s
I am Sorting the whole data by matching country codes in to new indvidual sets named by their country_codes and ultimate goal is to have sets named by their country code and containing all their createdAt values using a forEach on whole dataList, this is just a placholder Example , there is more going on....
dataList is of almost 100 -200 objects and i just want their created at into individual sets by their country codes, I am not able to initialise the sets at compilation as i recieve data at runtime , so how to dynamically create sets is my goal? i suppose...
I am doing all creation And data appending at runtime , which i am able to figure out how to do so..? so if somebody can Help , it will be great
n
dataList.groupBy(ProxyList::country)
should do what you want.
s
@natpryce I will try and report back , Thanks
Thankyou everybody for hearing me out and helping me😊
n
That’ll give you a map of country codes to lists. If you want sets of created at dates, you’ll have to use the overload of groupBy that transforms the values too, and then transform the values of the map from lists to sets:
dataList.groupBy(ProxyList::countryCode,ProxyList::createdAt).mapValues { k, v -> v.toSet() }
s
I am reading and trying to wrap my head around its working , I surely will , it may just take some time , anyway Thanks
d
👍 1