[英]Java ArrayList: Get Distinct Values from ArrayList which contain HashMap
Please see problem defination as describe below. 请按照以下说明查看问题定义。 I have ArrayList which contain HashMap.
我有包含HashMap的ArrayList。 In HashMap one or many key-value pairs exist.
在HashMap中,存在一个或多个键值对。 ArrayList contain following:
ArrayList包含以下内容:
HashMap1 contains key: column1 value:0001 key: column2 value:1 key: column3 value:1 HashMap1包含键:column1值:0001键:column2值:1键:column3值:1
HashMap2 contains key: column1 value:0001 key: column2 value:1 key: column3 value:2 HashMap2包含键:column1值:0001键:column2值:1键:column3值:2
Both HashMap1 and HashMap2 are kept inside ArrayList. HashMap1和HashMap2都保留在ArrayList中。
Now I enter a String like (column1, column2) Then I need output like Create new ArrayList containing following data: 现在,我输入一个类似(column1,column2)的字符串,然后需要类似Create new ArrayList的输出,其中包含以下数据:
HashMap1 contains key: column1 value:0001 key: column2 value:1 HashMap1包含键:column1值:0001键:column2值:1
When I enter String like (column1, column3) Create new ArrayList containing following data: 当我输入类似(column1,column3)的String时,创建包含以下数据的新ArrayList:
HashMap1 contains key: column1 value:0001 key: column3 value:1 HashMap1包含键:column1值:0001键:column3值:1
HashMap2 contains key: column1 value:0001 key: column3 value:2 HashMap2包含键:column1值:0001键:column3值:2
ie I want to get distinct values from ArrayList according to Input String. 即我想根据输入字符串从ArrayList获得不同的值。 Please Help me Logically as How can I achieve the same.
请从逻辑上帮助我,就像我该如何做到一样。
WHat you can do is; 你可以做什么?
for each map in the list.
copy the map.
map.keySet().retainAll(keys-you-want-to-keep)
set-of-results.add(map)
This way you will have the set of sub maps. 这样,您将拥有一组子映射。
Nothing is more strict than code. 没有什么比代码更严格的了。 So here is the to do what you want.
所以这是您想要做的。 Take a special look at the method selectColumns.
特别看一下selectColumns方法。 The list of Maps is stored on the class field list .
地图列表存储在类字段列表中 。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Matrix {
private List<Map<String, String>> list = new ArrayList<Map<String,String>>();
public Matrix(int numRows) {
for (int i = 0; i < numRows; i++) {
list.add(new HashMap<String, String>());
}
}
private void set(int row, String string, String string2) {
list.get(row).put(string, string2);
}
public Matrix selectColumns(String... columns) {
Matrix select = new Matrix(list.size());
for (String column : columns) {
for (int row = 0; row < list.size(); row++) {
select.set(row, column, list.get(row).get(column));
}
}
return select;
}
public static Matrix createDummy(int dim) {
Matrix matrix = new Matrix(dim);
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
matrix.set(i, "col" + j, "val:" + i + "|" + j);
}
}
return matrix;
}
public static void main(String[] args) {
Matrix mainMatrix = Matrix.createDummy(3);
System.out.println(mainMatrix.list);
System.out.println(mainMatrix.selectColumns("col1", "col2").list);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.