Java 将获取的数据转换为字符串、字符串数组映射
我正在android中从sqlite获取一个数据,如下所示Java 将获取的数据转换为字符串、字符串数组映射,java,android,collections,hashmap,Java,Android,Collections,Hashmap,我正在android中从sqlite获取一个数据,如下所示 URL PHONE --------------------------------- /test/img1.png 98989898 /test/img1.png 61216121 /test/img2.png 75757575 /test/img2.png 40404040 /test/img3.png
URL PHONE
---------------------------------
/test/img1.png 98989898
/test/img1.png 61216121
/test/img2.png 75757575
/test/img2.png 40404040
/test/img3.png 36363636
/test/img1.png [98989898 , 61216121 ]
/test/img2.png [75757575 , 40404040 ]
/test/img3.png [36363636]
现在我想创建这样一个地图,它存储的数据如下
URL PHONE
---------------------------------
/test/img1.png 98989898
/test/img1.png 61216121
/test/img2.png 75757575
/test/img2.png 40404040
/test/img3.png 36363636
/test/img1.png [98989898 , 61216121 ]
/test/img2.png [75757575 , 40404040 ]
/test/img3.png [36363636]
这样我就可以将整个地图传递给函数,该函数最终会在后台提取图像url,并将数据发送到列出的电话号码数组中。那么,如何将我获取的数据转换为键到字符串数组样式呢?我将创建一个映射(也称为“多映射”)。如果您使用列表
,则在开始之前不必知道给定URL的电话号码。如果选择阵列路由,则情况并非如此
Map<String, List<String>> results = new HashMap<String, List<String>>();
while (rs.next()) {
String url = rs.getString(1);
String phone = rs.getString(2);
List<String> phones = (results.contains(url) ? results.get(url) : new ArrayList<String>());
phones.add(phone);
results.put(url, phones);
}
Map results=newhashmap();
while(rs.next()){
字符串url=rs.getString(1);
字符串phone=rs.getString(2);
List phones=(results.contains(url)?results.get(url):new ArrayList());
电话。添加(电话);
结果。输入(url、电话);
}
Google Collections有一个多地图,你可以开箱即用,但我想你会同意这已经足够了
如果您想存储更多项(例如名称),您应该开始考虑一个对象,该对象将所有项封装到一个连贯的对象中。Java是一种面向对象的语言。听起来你好像对自己的思维水平太低感到内疚。字符串、基本体和数据结构是对象的构建块。也许你需要一个人在这里:
package model;
public class Person {
private String name;
private Map<String, List<String>> contacts;
// need constructors and other methods. This one is key
public void addPhone(String url, String phone) {
List<String> phones = (this.contacts.contains(url) ? this.contacts.get(url) : new ArrayList<String>());
phones.add(phone);
this.contacts.put(url, phones);
}
}
包模型;
公共阶层人士{
私有字符串名称;
私人地图联系人;
//需要构造函数和其他方法。这是关键
公用void addPhone(字符串url,字符串phone){
List phones=(this.contacts.contains(url)?this.contacts.get(url):new ArrayList());
电话。添加(电话);
this.contacts.put(url、电话);
}
}
剩下的我留给你
如果您这样做,则需要将结果集映射到一个人。但是你应该从我发布的代码中看到这个想法。我将创建一个映射(也称为“多映射”)。如果您使用列表
,则在开始之前不必知道给定URL的电话号码。如果选择阵列路由,则情况并非如此
Map<String, List<String>> results = new HashMap<String, List<String>>();
while (rs.next()) {
String url = rs.getString(1);
String phone = rs.getString(2);
List<String> phones = (results.contains(url) ? results.get(url) : new ArrayList<String>());
phones.add(phone);
results.put(url, phones);
}
Map results=newhashmap();
while(rs.next()){
字符串url=rs.getString(1);
字符串phone=rs.getString(2);
List phones=(results.contains(url)?results.get(url):new ArrayList());
电话。添加(电话);
结果。输入(url、电话);
}
Google Collections有一个多地图,你可以开箱即用,但我想你会同意这已经足够了
如果您想存储更多项(例如名称),您应该开始考虑一个对象,该对象将所有项封装到一个连贯的对象中。Java是一种面向对象的语言。听起来你好像对自己的思维水平太低感到内疚。字符串、基本体和数据结构是对象的构建块。也许你需要一个人在这里:
package model;
public class Person {
private String name;
private Map<String, List<String>> contacts;
// need constructors and other methods. This one is key
public void addPhone(String url, String phone) {
List<String> phones = (this.contacts.contains(url) ? this.contacts.get(url) : new ArrayList<String>());
phones.add(phone);
this.contacts.put(url, phones);
}
}
包模型;
公共阶层人士{
私有字符串名称;
私人地图联系人;
//需要构造函数和其他方法。这是关键
公用void addPhone(字符串url,字符串phone){
List phones=(this.contacts.contains(url)?this.contacts.get(url):new ArrayList());
电话。添加(电话);
this.contacts.put(url、电话);
}
}
剩下的我留给你
如果您这样做,则需要将结果集映射到一个人。但是你应该从我发布的代码中看到这个想法。如果我也想存储name字段呢?名称url[phone1,Phone2]?如果我也想存储名称字段呢?名称url[电话1,电话2]?