简体   繁体   English

根据单词拆分ArrayList

[英]Split ArrayList based on word

Attempting (and failing) to split an ArrayList based on a 'start word'.尝试(但失败)基于“起始词”拆分 ArrayList。

The ArrayList is a collection of sockets, originally a HashSet - ArrayList 是套接字的集合,最初是一个 HashSet -

[[Socket[addr=/192.168.150.210,port=2001,localport=49671], Socket[addr=/192.168.150.211,port=2001,localport=49670], Socket[addr=/192.168.150.215,port=2001,localport=49672], Socket[addr=/192.168.150.213,port=10001,localport=49669]]]

In need to somehow split that array so I end up with a new ArrayList that then contains distinct elements (each beginning Socket[ etc so I can then iterate through (based on size) looking for a matching IP address.需要以某种方式拆分该数组,所以我最终得到一个新的 ArrayList,然后包含不同的元素(每个开头的 Socket[ 等,因此我可以遍历(基于大小)寻找匹配的 IP 地址。

ie (as 4 'records')即(作为 4 个“记录”)

Socket[addr=/192.168.150.210,port=2001,localport=49671]
Socket[addr=/192.168.150.211,port=2001,localport=49670]
Socket[addr=/192.168.150.215,port=2001,localport=49672]
Socket[addr=/192.168.150.213,port=10001,localport=49669]

I cant use for i = on the original ArrayList because its size is 1 record ( need 4 records)我不能在原始 ArrayList 上使用 for i = 因为它的大小是 1 条记录(需要 4 条记录)

ie IE

Object currentSock = null; 

for (int i =0; i<  theseSocks.size(); i++)
{
currentSock = theseSocks.get(i);
if (currentSock.toString().contains(ipAddress))
{
System.out.println(" the ArrayList element is " +currentSock);
break;
}
}

Thoughts appreciated (I am a newbie).想法表示赞赏(我是新手)。 Regards Ralph问候拉尔夫

Generally, it's better practice not to filter or find by generating a string representing the entire object.通常,最好不要通过生成表示整个对象的字符串来过滤或查找。 Match based on the property of interest instead, which in your case is the IP address.而是基于感兴趣的属性进行匹配,在您的情况下是 IP 地址。

In Java 8 and beyond, there is a feature called streams that makes this very quick.在 Java 8 及更高版本中,有一个称为流的功能可以非常快速地完成此操作。

In this case, I'm still using the inetAddress toString to string match the IP, but not calling toString on the whole object.在这种情况下,我仍然使用 inetAddress toString 来字符串匹配 IP,但没有在整个对象上调用 toString。

The line of interest looks like this:感兴趣的线看起来像这样:

Optional< Socket > matchingSocket = sockets.stream().findAny().filter( socket -> socket.getInetAddress().toString().equals( "/192.168.0.1" ) );

A runnable example follows.下面是一个可运行的示例。 You need the targets to actually connect, or you will get dropped into the exception handling.您需要实际连接目标,否则您将陷入异常处理。 So you need to find computers willing to accept connections on a port and change the add lines accordingly, or use your live HashSet.因此,您需要找到愿意接受端口连接的计算机并相应地更改添加行,或者使用您的实时 HashSet。

try
{
    List< Socket > sockets = Lists.newArrayList( );
    sockets.add( new Socket( "192.168.0.1", 7777 ) );
    sockets.add( new Socket( "192.168.0.2", 7777 ) );

    Optional< Socket > matchingSocket = sockets.stream().findAny().filter( socket -> socket.getInetAddress().toString().equals( "/192.168.0.1" ) );

}
catch( Exception e )
{
    //handle this
}

Here is an example using a for loop instead of streams:这是一个使用 for 循环而不是流的示例:

try
{
    List< Socket > sockets = Lists.newArrayList( );
    sockets.add( new Socket( "192.168.0.1", 7777 ) );
    sockets.add( new Socket( "192.168.0.2", 7777 ) );

    for( Socket socket : sockets)
    {
        if( socket.getInetAddress().toString().equals( "/192.168.0.1" ) )
            System.out.println( "Found socket " + socket.toString() );
    }

}
catch( Exception e )
{
    //handle this
}

Also worth noting this exact same for loop works with your HashSet.同样值得注意的是,这个完全相同的 for 循环适用于您的 HashSet。 I'm wondering if your ArrayList somehow contains a single entry of the HashSet instead of individual sockets.我想知道您的 ArrayList 是否以某种方式包含 HashSet 的单个条目而不是单个套接字。 You could just iterate the HashSet instead.您可以改为迭代 HashSet。

Replace my arraylist lines with the following to test:用以下内容替换我的 arraylist 行进行测试:

Set< Socket > sockets = Sets.newHashSet( );
sockets.add( new Socket( "192.168.0.1", 7777 ) );
sockets.add( new Socket( "192.168.0.2", 7777 ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM