I guess I can move this to the tutorials section, since this seems to work just fine:
B4X:
Sub BuffersEqual (b1() As Byte, b2() As Byte) As Boolean
If b1.Length <> b2.Length Then Return False
For i = 0 To b1.Length - 1
If b1(i) <> b2(i) Then Return False
Next
Return True
End Sub
'name this whatever you want!
Public Sub MapsAreSame(aMap as Map, bMap as Map) As Boolean
Dim ser As B4XSerializator
Dim am() As Byte = ser.ConvertObjectToBytes(aMap)
Dim bm() As Byte = ser.ConvertObjectToBytes(bMap)
Return BuffersEqual(am,bm)
End Sub
Sub areEqual(mp1 As Map ,mp2 As Map) As Boolean
If mp1.Size<>mp2.Size Then Return False
For Each key As Object In mp1.Keys
If mp1.Get(key) <> mp2.Get(key) Then Return False
Next
Return True
End Sub
Sub areEqual(mp1 As Map ,mp2 As Map) As Boolean
If mp1.Size<>mp2.Size Then Return False
For Each key As Object In mp1.Keys
If mp1.Get(key) <> mp2.Get(key) Then Return False
Next
Return True
End Sub
With this slight modification you could also handle embedded maps (maybe other data types need a similar check):
B4X:
Sub areEqual(mp1 As Map ,mp2 As Map) As Boolean
If mp1.Size<>mp2.Size Then Return False
For Each key As Object In mp1.Keys
If mp1.Get(key) Is Map Then
If areEqual(mp1.Get(key),mp2.Get(key)) = False Then Return False
Else
If mp1.Get(key) <> mp2.Get(key) Then Return False
End If
Next
Return True
End Sub
Since this line would not be effective if the two elements were objects, whose contents you want to compare and not their pointers, you should serialize them first.
If you want to support maps that hold other collections or custom types then using serialization is probably the best option. You will need to replace the second map with a new one where the keys are added in the order of the first map.
If you want to support maps that hold other collections or custom types then using serialization is probably the best option. You will need to replace the second map with a new one where the keys are added in the order of the first map.
Better (quickest) solution: add a Map Id to your Maps ?
EDIT:
I probably wrote yet another nonsense:
comparing the two Ids would be the same as comparing the Map objects themselves (their pointer: If m1 = m2 ...)
OK, so how about this (works for me but not extensively tested):
B4X:
Private Sub isSame(a As Object, b As Object) As Boolean
Dim ser As B4XSerializator
Dim ab() As Byte = ser.ConvertObjectToBytes(a)
Dim bb() As Byte = ser.ConvertObjectToBytes(b)
For i = 0 To ab.Length - 1
If ab(i) <> bb(i) Then Return False
Next
Return True
End Sub
Sub MapsAreEqual(mp1 As Map ,mp2 As Map) As Boolean
If mp1.Size <> mp2.Size Then Return False
For Each Key As Object In mp1.Keys
' Log(Key)
If mp1.Get(Key) Is Map Then
If mp2.Get(Key) Is Map Then
If MapsAreEqual(mp1.Get(Key),mp2.Get(Key)) = False Then
' Log("Maps for " & Key & " are not equal.")
Return False
End If
Else
Return False
End If
Else
If Not(isSame(mp1.Get(Key),mp2.Get(Key))) Then
' Log("Unequal is " & Key)
' Log(mp1.Get(Key))
' Log(mp2.Get(Key))
Return False
End If
End If
Next
Return True
End Sub