Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I get the values from this array list to string. I need to know about that now to use my program.
JSONFunctions a=new JSONFunctions();
jsonarray= a.getJSONfromURL(URL);
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
JSONFunctions a=new JSONFunctions();
jsonarray= a.getJSONfromURL(URL);
try
{
for (int i = 0; i < jsonarray.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject js=jsonarray.getJSONObject(i);
map.put("artid",js.getString("artid"));
map.put("arttitle",js.getString("arttitle"));
map.put("artdescription",js.getString("artdescrption"));
map.put("artimage", js.getString("artimage"));
map.put("artdate",js.getString("artdate"));
arraylist.add(map);
}
}
catch (JSONException e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
It seems you've created a list of HashMaps. I wonder what could have been such a requirement.
Anyways, here's how to iterate over this arraylist.
i. Iterate over the arraylist
ii. You'll get a HashMap as you iterate
iii. Iterate over this HashMap within the above iteration.
for(HashMap<String,String> hMap:arrayList){
for(Map.Entry<String,String> entry:map.entrySet()){
String key=entry.getKey();
String value=entry.getKey();
//...Do what you require
}
}
Since you have an arraylist of hashmaps, you should do something like:
String s = arraylist.get(someindex).get("somestring");
If you provide further details, we will be able to help you more.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am working on a personal project on Java. I have a Map called allow and the second parameter is another Map. I am trying to compare the second parameter of the Map inside allow. If anyone can help me that would be a big help.
public boolean checkBank(String bank, int cNumber){
Map <String, Map<String, String> > allow = new HashMap<>();
String num = Integer.toString(cNumber);
Iterator<Map.Entry<String, Map<String, String>>> entries = allow.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, Map<String, String>> entry = entries.next();
if (entry.getKey().equals(bank)) {
String all = entry .getValue().get(0);
for (int i = 0; i < entry.getValue().size(); i++) {
if(entry.getValue().equals(num)) {
return true;
}
}
}
}
return false;
}
On the statement: if(entry.getValue().equals(num))
entry.getValue() is a Map, but num is a string. These two are not compatible types, so they can never be equal.
It's worth noting that you are looking for the one entry with the key value equal to bank. Rather than scan through all Map.Entry objects for the one which has the right value, why not just use the statement:
Map<String,String> map = allow.get(bank);
Let the outer map do this work for you.
Your question didn't exactly make clear what you wanted, but I'm guessing that you either want to look, in the inner Map, for an Entry where either the key or the value matches num. You can do that with either
map.containsKey(num)
or
map.containsValue(num)
Is that basically what you are looking for?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have an ArrayList like this:
[{1=R111, 2=Red, 3=50000}, {1=R123, 2=Blue , 3=50000}]
and i want to remove the array by value (R111 or R123).
how to remove the array using array.remove method for array like that?
I've try this link
but it's doesn't work for my problem.
Assuming your ArrayList is this:
List<String[]> arrayList = new ArrayList<>();
arrayList.add(new String[]{"R111","Red","50000"});
arrayList.add(new String[]{"R123","Blue","50000"});
you can do something like:
for (Iterator<String[]> iterator = arrayList.iterator();iterator.hasNext();) {
String[] stringArray = iterator.next();
if("R111".equals(stringArray[0])) {
iterator.remove();
}
}
You can safely remove an element using iterator.remove() while iterating the ArrayList. Also see The collection Interface.
An alternative shorter approach using Streams would be:
Optional<String[]> array = arrayList.stream().filter(a -> "R111".equals(a[0])).findFirst();
array.ifPresent(strings -> arrayList.remove(strings));
Thanks pieter, I used Iterator like this:
for (Iterator<HashMap<String, String>> iterator = RegulerMenu.iterator(); iterator.hasNext();) {
HashMap<String, String> stringArray = iterator.next();
if("R111".equals(stringArray.get("1"))) {
iterator.remove();
}
}
It's work now, Thankyou verymuch.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to search for a specific value in a HashMap, using an iterator, currently I have this method. I am very new to Java so your help would be greatly appreciated. helper.readAMap is hashmap which stores responses, which are generated when a user types in a certain word.
public String generateResponse(String words)
{
HashMap<String, String> map = new HashMap();
map = helper.readAMap("replies.txt");
Iterator<String> it = map.keySet();
while(it.hasNext()) {
String word = it.next();
String response = map.get(word);
if(response != null) {
return response;
}
}
return pickDefaultResponse();
}
Here:
if(key.equals(words)) {
You compare a String to a HashSet of Strings. That is like comparing an apple to a pear; it will always be false.
So you either want a single String as argument to your method, or you want to generate responses to all of the words.
I expect you want to do this:
if(words.contains(key)) { // Your input contains the key
return map.get(key); // Retrieve the response to the key from the map
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
List<Map.Entry<Integer,Integer>> openTimes = new ArrayList<Map.Entry<Integer,Integer>>();
for (int i=0; i<_elem.size(); i++)
{
openTimes.add(i,_elem.get(i));
}
How to properly add elements to the list openTimes?
What you are doing is wrong you declared your List as List<Map.Entry<Integer,Integer>> so therefore when you execute this line of code openTimes.add(i,_elem.get(i)); what you are inserting not a Map. I think you are looking for something similar to this. What you can do is
I have no idea what do you want to do here but you can use this
//We get the first Map
Map<Integer,Integer> yourMap = _elem.get(0);
for (int i=0; i<_elem.size(); i++)
{
yourMap.put(i,_elem.get(i))
}
or you can also use what dasblinkenlight suggested.
If you must have map entries there you need a Map from which to harvest these entries. One way to do this is as follows:
Map<Integer,Integer> tmp = new HashMap<Integer,Integer>();
for (int i=0; i<_elem.size(); i++) {
tmp.put(i, _elem.get(i));
}
List<Map.Entry<Integer,Integer>> openTimes =
new ArrayList<Map.Entry<Integer,Integer>>(tmp.entrySet());
You could also use AbstractMap.SimpleEntry<K,V> directly, or provide your own anonymous implementation.
A better solution would be defining your own class to represent key-value pairs.
for (int i=0; i<_elem.size(); i++)
{
Map.Entry<Integer,Integer> entry =
new AbstractMap.SimpleEntry<Integer, Integer>(i,_elem.get(i));
openTimes.add(entry);
}
iterate over List
for (Map.Entry<Integer,Integer> entry : openTimes)
{
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've got 2 maps and 1 array. First map is empty and second is full of data. Array includes some String keys. So I want to put data from second map to 1st if there are some equals keys in array.
Example:
1st map: empty
2nd map(key,value): foo,1; bar,2; java,3; pojo,4; tom,5; jerry,6;
array(string): foo,java,pojo;
So I need to put (foo,1;java,3;pojo,4;) in 1st map.
public static void main(String[] args) throws IOException {
HashMap<String, Integer> map1= new HashMap<String, Integer>();
HashMap<String, Integer> map2= new HashMap<String, Integer>();
map2.put("foo", 1);
map2.put("bar", 2);
map2.put("java", 3);
map2.put("pojo", 4);
map2.put("tom", 5);
map2.put("jerry", 6);
String[] arr={"foo","java","pojo"};
for (String arrItem : arr)
{
map1.put(arrItem, map2.get(arrItem));
System.out.println(arrItem);
System.out.println(map2.get(arrItem));
}
}
Vote up / Accept the answer if this solves your query
Is this what your after?
for(int i = 0; i < myArray.length; i++)
{
if(myMap2.get(myArray[i])!=null)
myMap1.put(myArray[i], myMap2.get(myArray[i]));
}
How about:
for(String key : array) {
map2.put(key,map1.get(key);
}
Assuming all keys in the array actually exist in map1.