B4A Library [B4X] B4XCollections - More collections

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jan 3, 2019.

  1. Erel

    Erel Administrator Staff Member Licensed User

    B4XCollections is a b4x lib. It adds several cross platform collections.
    It includes a static module named B4XCollections which should be used to create new instances of the collections.
    You can either create new empty collections or pass the initial data.

    B4XSet - A set is a collection of unique items. It is similar to a map where only the keys are used. Note that the order of items is preserved.
    Example:
    Code:
    Dim s As B4XSet = B4XCollections.CreateSet
    For i = 1 To 1000
       s.Add(
    Rnd(15))
    Next
    For Each Value As Int In s.AsList
       
    Log(Value)
    Next
    Output:
    1
    4
    2
    3

    Like with other types of collections the values in the set can be of any type.

    B4XOrderedMap - Similar to a Map with two advantages:

    1. The order of items is preserved. This is the case with regular Maps in B4A and B4J as well, however not the case with B4i regular Maps.

    2. You can sort the items based on the keys.

    Code:
    Dim om As B4XOrderedMap = B4XCollections.CreateOrderedMap2(Array("a""b""c""d"), Array(1234))
    For Each k As Object In om.Keys
       
    Log(k & ": " & om.Get(k))
    Next
    Log("sorting...")
    om.Keys.Sort(
    False'sorts descending
    For Each k As Object In om.Keys
       
    Log(k & ": " & om.Get(k))
    Next
    Output:
    a: 1
    b: 2
    c: 3
    d: 4
    sorting...
    d: 4
    c: 3
    b: 2
    a: 1


    B4XBitSet - An efficient collection of bits. Similar to an array of booleans but requires less memory (approximately 1/8 of a similar sized array).

    Finding all primes up to 1000 based on Sieve of Eratosthenes algorithm (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes):

    Code:
    Dim bitset As B4XBitSet = B4XCollections.CreateBitSet(1000)
    Dim n As Int = bitset.Size -1
    For i = 2 To Sqrt(n)
       
    If bitset.Get(i) = False Then
           
    For j = i * i To n Step i
               bitset.Set(j, 
    True)
           
    Next
       
    End If
    Next
    For i = 2 To bitset.Size - 1
       
    If bitset.Get(i) = False Then Log(i)
    Next
    B4XBytesBuilder - Replaces BytesBuilder.

    A collection with features similar to String and StringBuilder features. Holds raw bytes instead of characters.
    Very useful when you need to work with binary data.

    Q: What is a b4xlib?
    A: https://www.b4x.com/android/forum/threads/new-feature-b4x-lib-a-new-type-of-library.100383/#content

    Updates

    V1.05 - B4XBytesBuilder added.
     

    Attached Files:

    Last edited: Jan 9, 2019
  2. LucaMs

    LucaMs Expert Licensed User

    Wow, very useful!


    I don't understand this (and probably i'll open a thread for this question) but it seems to me that, eventually, it uses only values rather than keys.
     
    Last edited: Jan 3, 2019
  3. Erel

    Erel Administrator Staff Member Licensed User

    You are correct. There are only values. However you can think about it as a Map where you only use the keys (and this is how it is actually implemented).
     
  4. Mashiane

    Mashiane Expert Licensed User

    Looks good. Can one serialize to json and back?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Not sure which collection are you asking about. I guess that you are asking about OrderedMap. You will need to convert it to a regular Map first and you will lose the order once you read it.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    V1.05 released. B4XBytesBuilder collection added.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice