HashMap objHashMap = new HashMap();
objHashMap.put("key1", "Value1");
objHashMap.put("key1", "Value2");
System.out.println(objHashMap.get("key1"));
Run Code Online (Sandbox Code Playgroud)
上面的代码显示了“Value2”的方式和原因
这里我有一个查询,其中我根据条件获取名称和值,我在列表中存储empid和empname的值
这是一段代码:
List<String> _empid = new ArrayList<String>();
List<String> _empname = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
在这里,我从数据库中获取值:
while (cur1.moveToNext()) {
if (cur1.getString(cur1.getColumnIndex("name"))
.equalsIgnoreCase("DIS_FIELD_COLLECTION_EXECUTIVE")) {
_empid.add(cur1.getString(cur1.getColumnIndex("value")));
} else if (cur1.getString(cur1.getColumnIndex("name"))
.equalsIgnoreCase("FIELD_EXECUTIVE_NAME")) {
_empname.add(cur1.getString(cur1.getColumnIndex("value")));
}
}
for (int i = 0; i < _empid.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", _empid.get(i));
map.put("name", _empname.get(i));
Log.e("forloop", _empid.get(i));
Log.e("forloop", _empname.get(i));
emp.add(map);
adp_emp = new Listadapter_emp(emp, Tracker_filter_Activity.this);
lst_employee.setAdapter(adp_emp);
}
Run Code Online (Sandbox Code Playgroud)
这里的输出是这样的
01-17 12:16:49.696 29369-29369/com.wp.focus E/forloop: 5
01-17 12:16:49.696 29369-29369/com.wp.focus E/forloop: AZAD NAGAR-TAN001
01-17 12:16:49.697 29369-29369/com.wp.focus E/forloop: 3 …Run Code Online (Sandbox Code Playgroud) 我想创建一个util方法,它使用键和值将HashMap转换为long String:
HashMap<String, String> map = new LinkedhashMap<>();
map.put("first_key", "first_value");
map.put("second_key", "second_value");
Run Code Online (Sandbox Code Playgroud)
我需要得到这个最终结果:
first_key=first_value&second_key=second_value
Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手
我的问题是
我知道 unordered_map 使用散列,而 map 使用红黑树。但我很困惑,为什么 unordered_map 需要散列,而 map 不需要。可能是什么原因?
#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const
{
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
int main()
{
unordered_map<pair<int, int>, int, hash_pair> hmap1;
hmap1[{1,1}]=2;
if (hmap1.find({1,1})!=hmap1.end()) cout<<"Value found :"<<hmap1[{1,1}]<<endl;
if (hmap1.find({0,0})==hmap1.end()) cout<<"Value not found"<<endl;
map<pair<int, int>, int> hmap2;
hmap2[{2,2}]=4;
if (hmap2.find({2,2})!=hmap2.end()) cout<<"Value found :"<<hmap2[{2,2}]<<endl;
if (hmap2.find({0,0})==hmap2.end()) cout<<"Value not …Run Code Online (Sandbox Code Playgroud) class{
public HashMap<String, String> eachperson;
apple(){
this.eachperson.put("thekey","thevalue");
}
}
Run Code Online (Sandbox Code Playgroud)
(请原谅班级和职能面前的公众/私人.我只是想知道我是否正确地使用哈希地图.)
有关真实代码,请参阅以下内容:
class ParsedDataSet{
public HashMap<String, String> eachperson;
public List<HashMap<String,String>> peoplelist = new ArrayList<HashMap<String,String>>();
}
class ListprofileHandler extends DefaultHandler{
private boolean in_results = true;
private boolean in_item = false;
private boolean in_first_name = false;
private boolean in_last_name = false;
private boolean in_picture_url = false;
private boolean in_enditem_dummy = false;
private ParsedDataSet dset = new ParsedDataSet();
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(localName.equals("results")){
this.in_results = …Run Code Online (Sandbox Code Playgroud) 我有一个关于从我的价值中获取价值的问题ArrayList<HashMap<String, String>>.
我的代码是:
ArrayList<HashMap<String, String>> myArrayList;
Run Code Online (Sandbox Code Playgroud)
然后:
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
myArrayList.add(map);
Run Code Online (Sandbox Code Playgroud)
例如,如果我想获取名称,我尝试如下,但我获得了运行时错误(应用程序崩溃):
System.out.println(myArrayList.get(1).get(TAG_NAME));
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
非常感谢你!
for(int i=0; i< n; i++) {
hashMap.put("Name",name.get(i));
hashMap.put("Website",website.get(i));
}
Run Code Online (Sandbox Code Playgroud)
我想迭代地向HashMap添加多个值我希望我的输出是这样的
我如何添加到hashmap,我没有得到预期的结果.
如何向 HashMap 内的 HashSet 添加值?
Map<String, Set<String>> myMap;
Run Code Online (Sandbox Code Playgroud) 我想List<HashMap<String, Object>>投入Set<StudentInfo>
我有方法
public List<HashMap<String,Object>> getStudentData(studentId);
Run Code Online (Sandbox Code Playgroud)
我想将结果转换为Set,所以我用过
Set<StudentInfo> studentFilteredInfo = new HashSet<>();
List<Map<String, Object>> studentCompleteRecord = getStudentData(1005);
studentFilteredInfo.addAll((Collection<? extends StudentInfo>studentCompleteRecord ));
Run Code Online (Sandbox Code Playgroud)
最初当我在localhost上使用java 8,eclipse和tomcat 8执行它时工作正常.
当我试图用maven构建它时
mvn clean package
Run Code Online (Sandbox Code Playgroud)
它将通过一个错误:
不兼容的类型: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
cannot be converted to java.util.Collection<? extends com.school.model.StudentInfo>
Run Code Online (Sandbox Code Playgroud) PS:这个 HashSet 是如何产生排序输出的? 这篇文章没有回答我的问题。我知道如果我将任何数字放入哈希集中,我将不会得到排序顺序。
但是,我发现如果我将所有 [1, 2, 3, ..., n] 放入具有任何混洗顺序的 HashSet 中并迭代 HashSet,我将得到一个guranteed sorted order。我不明白为什么它总是会发生。我已经多次测试过任何 n < 10000 ,它总是正确的,因此这不应该是巧合,应该有一些原因!即使我不应该依赖这个实现细节,请告诉我为什么它总是发生。
PS:我知道如果我将 [0,1,2, ..., n-1] 或 [1+k, 2+k, .., n+k] (k != 0) 插入 HashSet,迭代顺序未排序,我已经测试过。HashSet 的迭代顺序未排序是正常的。但是,为什么 [1,2,3,4,..,n] 的任何插入顺序都意外地总是正确的?我已经检查了实现细节。如果我跟踪路径,整个过程将包括调整桶数组的大小,以及从链表到红黑树的转换。如果我以无序的顺序插入整个 [1-n],则 HashSet 的中间状态是未排序的。但是,如果我完成所有插入,它会意外地排序。
我使用 JDK 1.8 进行了以下测试。
public class Test {
public static void main(String[] args) throws IOException {
List<Integer> res = printUnsortedCase(10000);
System.out.println(res);
}
private static List<Integer> printUnsortedCase(int n){
List<Integer> res = new …Run Code Online (Sandbox Code Playgroud)