首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将一个大数组分解为一个hashmap中的几个数组列表?

如何将一个大数组分解为一个hashmap中的几个数组列表?
EN

Stack Overflow用户
提问于 2013-02-25 03:01:16
回答 2查看 279关注 0票数 0

示例:一个查询抛出下一个结果集:

姓名年龄大总数

  • 约翰·史密斯,45,1000岁
  • 约翰·史密斯,56,800岁
  • 约翰·史密斯,34,500岁
  • 约翰·斯迈思,56,500岁
  • 约翰·斯迈思,56,1100岁

我想把这个数组分成三个,并将它们存储在一个hashmap中,其中键是客户端名称。

我在想

代码语言:javascript
复制
Arraylist<Row> rows = dao.getClientActivity();
Map map = new HashMap<Clients Name, Clients Row>();
Arraylist<Row>  = null;


for (Row row : rows){

    if (map.get(row.clientName) == null) list = new ArrayList<Row>();

    list.add(row);

    if(map.get(row.clientName) == null) map.put(row.clientName, list);

}

列表将始终按名称排序。

以上面的代码段为伪代码,我家里没有编码程序,我只是把它从头上去掉,我想这个星期五我测试了类似的东西,但它只打印在行上;

我不知道有没有更好的方法,但这是我想出的第一件事。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-25 03:12:01

您的地图声明应该如下(假设Row.clientNameString):

代码语言:javascript
复制
Map<String, List<Row>> map = new HashMap<String, List<Row>>();

for循环应该如下所示:

代码语言:javascript
复制
for (Row row : rows){
    /*Get the list of rows for current client name.*/
    List<Row> currRows = map.get(row.clientName); 
    if (currRows == null) {/*If not list exists for*/
        currRows = new ArrayList<Row>(); /*create a new one*/
        map.put(row.clientName, currRows); /*and put it in the map*/
    }
    currRows.add(row);/*add the current row to the list*/
}
票数 3
EN

Stack Overflow用户

发布于 2013-02-25 03:20:14

我假设您无法更改输入格式。

我建议您创建一个表示客户端的模型:

代码语言:javascript
复制
public class Client {

    private final String name;
    private final byte age; //Nobody should be older than 256
    private final int total;

    /* Construct model */

    /* Getters/Functions */

}

我还建议您在Client中创建一个工厂方法,以便从您的字符串输入创建类。

代码语言:javascript
复制
public static Client parseClient(String clientRep){

    String[] clientData = clientRep.split(',');

    Client newClient = new Client(); //TODO: Name conventionally. 

    newClient.name = clientData[0];
    newClient.age = Byte.valueOf(clientData[1]);
    newClient.total = Integer.valueOf(clientData[2]);

    return newClient;

}

现在,您可以将这些添加到地图(Map<String, Client>)中。

代码语言:javascript
复制
String clientFromWherever = getWhateverDataFromWherever();

Map<String, Client> clientel = new HashMap<>();

Client addingToMap = Client.parseClient(clientFromWherever);

clientel.put(addingToMap.getName() /* or however the name should be got */, addingToMap);

那应该足够好了。

=====

但是-如果您不想使用客户端对象,我建议创建一个Map<String, int[]>并将其存储在数组中。如果您的收费不超过Short.MAXVALUE,请使用short[]。存储大量的数组(或任何复杂的集合)只是为了存储少量的数据是没有必要的。

代码语言:javascript
复制
ArrayList<Row> rows = dao.getClientActivity();
Map<String, int[]> clientelData = new HashMap<>();

for(Row clientRow : rows) {

    if (!map.containsKey(clientRow.clientName) {

        int[] clientNumericalData = new int[2];

        map.put(clientRow.clientName, clientNumericalData);

    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15059436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档