我有一个对象ArrayList<Tile> list
的数组列表Tile的属性为new Tile("colour", value)
,我想创建一个搜索功能,在其中迭代Tile的每个Colour
属性和每个{{1} } arraylist中每种颜色内的属性(就像每个循环嵌套一样),是否有一种简单的方法?
答案 0 :(得分:3)
假设Tile
类具有两个属性String
colour
和int
value
。它具有toString
(java.lang.Object class
的替代方法),如下所示:
@Override public String toString() {
return colour + ":" + value;
}
制作一些瓷砖:
Tile t1 = new Tile("red", 7); // constructor takes a colour and a value
Tile t2 = new Tile("red", 2);
Tile t3 = new Tile("blue", 9);
Tile t4 = new Tile("white", 17);
Tile t5 = new Tile("blue", 3);
Tile t6 = new Tile("red", 15);
Tile t7 = new Tile("white", 10);
方案1:
该函数将Tile
对象的列表和String
颜色作为输入,并返回具有输入颜色(及其值)的所有图块。有两种方法可以实现,并且在两种方法中显示出来:
private static List<Tile> getTilesWithColor1(List<Tile> tilesList, String searchColor) {
return tilesList.stream()
.filter(tile -> tile.getColour().equals(searchColor))
.collect(Collectors.toList());
}
private static List<Tile> getTilesWithColor2(List<Tile> tilesList, String searchColor) {
List<Tile> result = new ArrayList<>();
for (Tile t : tilesList) {
if (t.getColour().equals(searchColor)) {
result.add(t);
}
}
return result;
}
tilesList
,colour="red"
[red:7, red:2, red:15]
我想做一个搜索功能,在其中我迭代每种颜色 Tile的属性和每个颜色中的每个Value属性 arraylist(就像每个循环的嵌套)一样,有没有一种简单的方法 这样吗?
可以更改此功能以添加其他条件或过滤器以获得所需的结果。
方案2:
获取所有颜色及其值:
private static Map<String, List<Integer>> getTileColorsAndValues(List<Tile> tilesList) {
return tilesList.stream()
.collect(Collectors.groupingBy(Tile::getColour,
Collectors.mapping(Tile::getValue, Collectors.toList())));
}
tilesList
{red=[7, 2, 15], white=[17, 10], blue=[9, 3]}
请注意,可以从生成的地图中像这样在“红色”图块中获取值:
List<Integer> valuesList = map.get("red");
方案3:
按颜色获取所有图块:
private static Map<String, List<Tile>> getTilesByColorsAndValues(List<Tile> tilesList) {
return tilesList.stream()
.collect(Collectors.groupingBy(Tile::getColour));
}
tilesList
{red=[red:7, red:2, red:15], white=[white:17, white:10], blue=[blue:9, blue:3]}
请注意,可以从生成的地图中像这样在“红色”图块中获取图块:
List<Tile> tilesList = map.get("red");
示例代码:
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
public class TilesExample {
public static void main(String [] args) {
Tile t1 = new Tile("red", 7);
Tile t2 = new Tile("red", 2);
Tile t3 = new Tile("blue", 9);
Tile t4 = new Tile("white", 17);
Tile t5 = new Tile("blue", 3);
Tile t6 = new Tile("red", 15);
Tile t7 = new Tile("white", 10);
List<Tile> tilesList = Arrays.asList(t1, t2, t3, t4, t5, t6, t7);
System.out.println(getTilesWithColor1(tilesList, "red"));
System.out.println(getTilesWithColor2(tilesList, "red"));
System.out.println(getTileColorsAndValues(tilesList));
System.out.println(getTilesByColorsAndValues(tilesList));
}
private static Map<String, List<Tile>> getTilesByColorsAndValues(List<Tile> tilesList) {
return tilesList.stream()
.collect(Collectors.groupingBy(Tile::getColour));
}
private static Map<String, List<Integer>> getTileColorsAndValues(List<Tile> tilesList) {
return tilesList.stream()
.collect(Collectors.groupingBy(Tile::getColour,
Collectors.mapping(Tile::getValue, Collectors.toList())));
}
private static List<Tile> getTilesWithColor1(List<Tile> tilesList, String searchColor) {
return tilesList.stream()
.filter(tile -> tile.getColour().equals(searchColor))
.collect(Collectors.toList());
}
private static List<Tile> getTilesWithColor2(List<Tile> tilesList, String searchColor) {
List<Tile> result = new ArrayList<>();
for (Tile t : tilesList) {
if (t.getColour().equals(searchColor)) {
result.add(t);
}
}
return result;
}
}