RCM5000
RCM5000

Reputation: 23

How to searching part of object inside array list without looping method

I save the client data into an array list. How to find part of data(currentIP) inside the array list(clientList) with fast method ?

Code as follow:

' Array list to keep Clients Object
Protected Friend clientList As ArrayList = ArrayList.Synchronized(New ArrayList())

Public Class Clients
    public clientIPAdrress As IPAddress
    public clientTCP As TcpClient
    public clientStream As SslStream
End Class


Public Sub Test()

  ' Create objClients Object from Clients Class
  Dim objClients as new Clients 

  ' Add objClients to Array List  
  clientList.Add(objClients)

  Dim currentIP as IPAdress = IPAddress.Parse("192.168.1.2")
  Dim isIPFound as Boolean = False

  ' Search currentIP inside clientList with looping method
  For Each ip As Clients In clientList                                                                              
     If ip.ClientIPAdrress = currentIP Then
        isIPFound = True
        Exit For
     End If
  Next

End Sub

Thanks for the advice.

Upvotes: 0

Views: 2371

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460208

You should use a typed List(Of Clients) instead, then cou can use LINQ:

Dim clientList As New List(Of Clients)
clientList.Add(new Clients())
Dim isIPFound=clientList.Any(Function(ip) ip.ClientIPAdrress = currentIP)

But essentially it is the same as your loop just in one row.

If you need to find the fastest way, you could sort your list and BinarySearch the IP with a custom comparer. Or, if the IP is unique in the list, you could use a Dictionary(Of IPAdress,Clients) instead.

Upvotes: 1

Related Questions