两个集合中都是字符串,交叉组合,用Java怎么写
时间: 2023-06-03 11:01:39 浏览: 60
可以使用两层循环,分别遍历两个集合中的字符串,将每一对字符串组合起来进行比较,如果相等则加入结果集。Java代码实现如下:
```
public List<String> crossCombination(List<String> list1, List<String> list2) {
List<String> result = new ArrayList<>();
for (String s1 : list1) {
for (String s2 : list2) {
if (s1.equals(s2)) {
result.add(s1);
}
}
}
return result;
}
```
相关问题
java 编写方法获取两个字符串数组中都存在的元素的数量
下面是 Java 代码,实现统计两个字符串数组中都存在的元素数量:
```
import java.util.HashSet;
public class Main {
public static int getIntersectionCount(String[] arr1, String[] arr2) {
HashSet<String> set1 = new HashSet<>();
for (String s : arr1) {
set1.add(s);
}
int count = 0;
for (String s : arr2) {
if (set1.contains(s)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
String[] arr1 = {"a", "b", "c", "d"};
String[] arr2 = {"c", "d", "e", "f"};
System.out.println(getIntersectionCount(arr1, arr2));
}
}
```
运行结果:2,表示两个数组中都存在的元素数量为 2。
python实现:1.两个列表对比2.两个列表中都是路由网段的字符串
好的,我来分别回答你的两个问题。
1. 两个列表对比
如果你想要比较两个列表是否相同,可以直接使用 `==` 运算符进行比较。例如:
```python
list1 = [1, 2, 3]
list2 = [1, 2, 3]
if list1 == list2:
print("两个列表相同")
else:
print("两个列表不同")
```
上述代码会输出 "两个列表相同"。
如果你想要比较两个列表是否包含相同的元素,可以使用 `set` 函数将列表转换为集合,然后使用 `==` 运算符进行比较。例如:
```python
list1 = [1, 2, 3]
list2 = [3, 2, 1]
if set(list1) == set(list2):
print("两个列表包含相同的元素")
else:
print("两个列表不包含相同的元素")
```
上述代码同样会输出 "两个列表相同",因为集合中的元素是无序的。
2. 两个列表中都是路由网段的字符串
如果你想要比较两个列表中都是路由网段的字符串,可以使用 Python 的 ipaddress 模块来处理 IP 地址。例如:
```python
import ipaddress
list1 = ["192.168.0.0/24", "10.0.0.0/8"]
list2 = ["10.0.0.0/8", "192.168.1.0/24"]
# 将列表中的字符串转换为 ipaddress.IPv4Network 对象
networks1 = [ipaddress.IPv4Network(s) for s in list1]
networks2 = [ipaddress.IPv4Network(s) for s in list2]
# 比较两个列表是否包含相同的网络
if set(networks1) == set(networks2):
print("两个列表包含相同的网络")
else:
print("两个列表不包含相同的网络")
```
上述代码会输出 "两个列表不包含相同的网络",因为 "192.168.0.0/24" 和 "192.168.1.0/24" 不相同。如果你想要忽略掉 IP 地址的具体值,只比较网络地址和子网掩码,可以使用 `ip_network` 函数代替 `IPv4Network`。例如:
```python
networks1 = [ipaddress.ip_network(s) for s in list1]
networks2 = [ipaddress.ip_network(s) for s in list2]
if set(networks1) == set(networks2):
print("两个列表包含相同的网络")
else:
print("两个列表不包含相同的网络")
```
上述代码会输出 "两个列表包含相同的网络",因为网络地址和子网掩码相同。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)