Sub Process_Globals
Dim myset As JavaObject
End Sub
Sub AppStart (Args() As String)
Dim myList As List
myList.Initialize
myList.AddAll(Array(0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 35, 36, 36, 37, 38, 38, 38, 39, 39, 39, 40, 41, 41, 41, 42, 43, 45, 46, 50, 50, 50, 50))
myset.InitializeNewInstance("java.util.HashSet",Null)
For Each value As Object In myList
If myset.RunMethod("add",Array(value)) = False Then
Log("Value [" & value & "] is duplicated")
End If
Next
End Sub
to remove the duplicates - just add this to the above code before the End Sub
B4X:
myList.Clear
Dim iterator As JavaObject
iterator = myset.RunMethod("iterator",Null)
Do While iterator.RunMethod("hasNext",Null)
myList.Add(iterator.RunMethod("next",Null))
Loop
Or you can do something like this without JavaObject:
B4X:
Sub RemoveDuplicates(NyList As List) As List
If NyList = Null Or Not(NyList.IsInitialized) Then Return NyList
Dim lstNew As List
lstNew.Initialize
For Each objItem As Object In NyList
If lstNew.IndexOf(objItem) = - 1 Then
lstNew.Add(objItem)
End If
Next
Return lstNew
End Sub
Or you can do something like this without JavaObject:
B4X:
Sub RemoveDuplicates(NyList As List) As List
If NyList = Null Or Not(NyList.IsInitialized) Then Return NyList
Dim lstNew As List
lstNew.Initialize
For Each objItem As Object In NyList
If lstNew.IndexOf(objItem) = - 1 Then
lstNew.Add(objItem)
End If
Next
Return lstNew
End Sub
Sub AppStart (Args() As String)
Dim firstlist As List = Array(0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 35, 36, 36, 37, 38, 38, 38, 39, 39, 39, 40, 41, 41, 41, 42, 43, 45, 46, 50, 50, 50, 50)
Dim Now As Long = DateTime.Now
Log(RemoveDuplicates(firstlist))
Log(Abs(DateTime.Now - Now))
Dim Now As Long = DateTime.Now
Log(RemoveDuplicates2(firstlist))
Log(Abs(DateTime.Now - Now))
End Sub
Sub RemoveDuplicates(NyList As List) As List
If NyList = Null Or Not(NyList.IsInitialized) Then Return NyList
Dim lstNew As List
lstNew.Initialize
For Each objItem As Object In NyList
If lstNew.IndexOf(objItem) = - 1 Then
lstNew.Add(objItem)
End If
Next
Return lstNew
End Sub
Sub RemoveDuplicates2(MyList As List) As List
If MyList = Null Or Not(MyList.IsInitialized) Then Return MyList
If MyList.Size <= 1 Then Return MyList
Dim lstNew As List
lstNew.Initialize
MyList.Sort(True)
Dim PrevItem As Object = MyList.Get(0)
lstNew.Add(PrevItem)
For Each objItem As Object In MyList
If PrevItem <> objItem Then
lstNew.Add(objItem)
End If
PrevItem = objItem
Next
Return lstNew
End Sub
How do you get the same result with Strings? And how do I copy for example b4xs elements into a List as list. I need to cast the elements to be able to save them in a SqlLite table.
Sub Process_Globals
dim List as List
'This list contains string elements loaded from a SqlLite database
End Sub
Sub TrimDuplicaRecordClienti
list.Initialize
Dim b4xs As B4XSet = B4XCollections.CreateSet
For Each objItem As Int In list
b4xs.Add(objItem)
list.Add(objItem)'NotWork'
Next
End Sub
Here is a complete example using strings instead of Int:
B4X:
Dim firstlist As List = Array As String("mark", "hello", "klaus", "jose", "4", "WebQuest", "erel", "WebQuest","klaus")
Dim b4xs As B4XSet = B4XCollections.CreateSet
For Each objItem As String In firstlist
b4xs.Add(objItem)
Next
Log(b4xs.AsList)
For Each Value As String In b4xs.AsList
Log(Value)
Next
'Displays:
(ArrayList) [mark, hello, klaus, jose, 4, WebQuest, erel]
mark
hello
klaus
jose
4
WebQuest
erel