遍历List<String> taskSources判断是否等于1,2,3,4,5,6改成a,b,c,d,e,f
时间: 2023-10-03 07:07:26 浏览: 34
可以使用Java中的Map来实现:
```java
Map<String, String> map = new HashMap<>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
map.put("5", "e");
map.put("6", "f");
List<String> taskSources = ...; // 假设已经初始化
for (String source : taskSources) {
String value = map.get(source);
if (value != null) {
// 执行相应的操作
}
}
```
在遍历`taskSources`时,通过`map.get(source)`方法获取对应的值,如果值不为空,则执行相应的操作。这样就可以将1,2,3,4,5,6转换为a,b,c,d,e,f。
相关问题
stream list<object> 遍历 List<String>去重
可以使用LINQ库中的Distinct()方法来去重,示例代码如下:
```csharp
List<object> stream = new List<object>();
List<string> stringList = new List<string>();
// 假设stream已经被初始化并填充
// 假设stringList已经被初始化并填充
var distinctStringList = stringList.Distinct();
foreach (object obj in stream)
{
foreach (string str in distinctStringList)
{
// 在这里处理obj和str
}
}
```
在上面的代码中,我们首先使用Distinct()方法来去重stringList中的元素,并将结果存储在distinctStringList变量中。然后,我们使用两个嵌套的foreach循环来遍历stream和distinctStringList,并在内部处理它们。
如何遍历List<List<<String>>
要遍历一个`List<List<String>>`,你可以使用嵌套的循环来遍历每个内部的`List<String>`。以下是一个示例代码:
```java
List<List<String>> listOfLists = new ArrayList<>();
// 添加一些数据到listOfLists
listOfLists.add(Arrays.asList("A", "B", "C"));
listOfLists.add(Arrays.asList("D", "E"));
listOfLists.add(Arrays.asList("F", "G", "H", "I"));
// 遍历listOfLists
for (List<String> innerList : listOfLists) {
// 遍历内部的List<String>
for (String str : innerList) {
System.out.println(str);
}
}
```
上述代码中,我们首先创建了一个`List<List<String>>`对象`listOfLists`,并向其中添加一些内部的`List<String>`。然后,我们使用两个嵌套的循环来遍历`listOfLists`和个内部的`List<String>`。在内部的循环中,我们打印出每个字符串。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)