Android Code Snippet [B4X] Count specific week days in a month + tutorial about B4XSet

Depends on DateUtils and B4XCollections:
B4X:
Sub CountSpecificDaysInMonth (Year As Int, Month As Int, RelevantDays As B4XSet) As Int
    Dim day As Long = DateUtils.SetDate(Year, Month, 1)
    Dim p As Period
    p.Days = 1
    Dim total As Int
    Do While DateTime.GetMonth(day) = Month
        If RelevantDays.Contains(DateTime.GetDayOfWeek(day)) Then total = total + 1
        day = DateUtils.AddPeriod(day, p)
    Loop
    Return total
End Sub

Usage example:
B4X:
Sub AppStart (Args() As String)
    Dim InterestingDays As B4XSet = B4XCollections.CreateSet2(Array(1, 2, 3, 4, 5)) 'number of days excluding Friday and Saturday
    Log(CountSpecificDaysInMonth(2020, 12, InterestingDays)) 'December 2020
End Sub
 

Brian Michael

Active Member
Licensed User
Excelent i try it, it's more better and powerfull!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I guess that most developers are less familiar with B4XSet so it's worth explaining this very nice type of collection. I use it a lot.
A set collection is similar to a list where all items are unique. Adding an item that already exists doesn't change anything.
Under the hood, B4XSet is based on B4XOrderedMap. We just don't use the values. There are only keys.
This means that checking whether an item exists is a fast operation - O(1).

In this case we could have used a list as the number of items is very small, but with larger datasets it can make a difference.
Bottom line:
- Use B4XSet when you want a list with unique items. No need to check whether an item already exists before adding it.
- Use B4XSet when you want to quickly test whether an item exists in the collection.

Note that B4XSet.AsList returns a list with the items.
 

Brian Michael

Active Member
Licensed User
I guess that most developers are less familiar with B4XSet so it's worth explaining this very nice type of collection. I use it a lot.
A set collection is similar to a list where all items are unique. Adding an item that already exists doesn't change anything.
Under the hood, B4XSet is based on B4XOrderedMap. We just don't use the values. There are only keys.
This means that checking whether an item exists is a fast operation - O(1).

In this case we could have used a list as the number of items is very small, but with larger datasets it can make a difference.
Bottom line:
- Use B4XSet when you want a list with unique items. No need to check whether an item already exists before adding it.
- Use B4XSet when you want to quickly test whether an item exists in the collection.

Note that B4XSet.AsList returns a list with the items.

Thank you very much, this is a great piece of information, it will change my project a lot since it used maps and lists to store big data and to obtain the unique values I had to create conditions in the loops which slowed down the sub and made it last a long time to load.

PS: You should make a new post explaining B4XSet in detail since it is a very useful and powerful method.
 
Top