B4A Library [B4X] B4XCollections - More collections

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:
B4X:
Dim s As B4XSet = B4XCollections.CreateSet
For i = 1 To 1000
   s.Add(Rnd(1, 5))
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.

B4X:
Dim om As B4XOrderedMap = B4XCollections.CreateOrderedMap2(Array("a", "b", "c", "d"), Array(1, 2, 3, 4))
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):

B4X:
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.07 - B4XOrderedMap.GetDataForSerializator / SetDataFromSerializator: https://www.b4x.com/android/forum/threads/b4x-b4xcollections-more-collections.101071/post-745277
V1.06 - B4XOrderedMap.Values property.
V1.05 - B4XBytesBuilder added.

B4XCollections is included as an internal library.
 

Attachments

Last edited:

LucaMs

Expert
Licensed User
2. You can sort the items based on the keys.
Wow, very useful!


B4XSet - A set is a collection of unique items. It is similar to a map where only the keys are used.
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:

LucaMs

Expert
Licensed User
Updates

V1.06 - B4XOrderedMap.Values property.
V1.05 - B4XBytesBuilder added.

B4XCollections is included as an internal library.
I have B4A V. 9.80 (the last one, If I'm not wrong) but B4XCollections version is 1.05.
 

DC1

Member
Licensed User
I have uploaded and can see B4XCollections (1.06) - but B4A is still telling me that the bytesbuilder is missing - any ideas ?
After re-compiling my script (Using B4A 9.8)
 
Last edited:

Myr0n

Active Member
Licensed User
I have uploaded and can see B4XCollections (1.06) - but B4A is still telling me that the bytesbuilder is missing - any ideas ?
After re-compiling my script (Using B4A 9.8)
Did you close B4A or refresh the libraries?
 

drgottjr

Well-Known Member
Licensed User
bytesbuilder or B4Xbytesbuilder ? the latter has replaced the former.
 

Erel

Administrator
Staff member
Licensed User
v1.07 released with two new methods in B4XOrderedMap that allow saving and loading such maps with B4XSerializator:
B4X:
Dim ser As B4XSerializator
Dim b() As Byte = ser.ConvertObjectToBytes(OrderedMap.GetDataForSerializator) 'save
File.WriteBytes(...)

'later load it:
Dim o2 As B4XOrderedMap
o2.Initialize
o2.SetDataFromSerializator(ser.ConvertBytesToObject(b))
 
Top