我有一个对象数组列表 ArrayList<Tile> list
Tile 的属性为 new Tile("colour", value)
我想创建一个搜索功能,在其中遍历每个 Colour
Tile 的属性和每个 Value
arraylist 中每种颜色的属性(就像每个循环的嵌套),是否有一种简单的方法可以做到这一点?
最佳答案
假设 Tile
类有两个属性 String
colour
和 int
value
。它有一个 toString
(java.lang.Object 类
的覆盖方法),如下所示:
@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
颜色作为输入,并返回具有输入颜色(及其值)的所有图 block 。有两种方法可以做到这一点,这些方法以两种方法显示:
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]
I want to make a search function where I iterate though each Colour attribute of the Tile and each Value attribute within each colour in the arraylist (like a nested for each loop), is there an easy way of doing this?
可以更改此函数以添加其他条件或过滤器以获得所需的结果。
场景 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]}
请注意,可以从生成的 map 中像这样获取“红色”图 block 内的值:
List<Integer> valuesList = map.get("red");
场景 3:
按颜色获取所有图 block :
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]}
请注意,可以从生成的 map 中像这样获取“红色”瓦片内的瓦片:
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;
}
}
关于java - 搜索对象数组列表的特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52660914/