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.
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 3 years ago.
Improve this question
I'm writing code to convert a single LinkedList<Integer> to an array.
My code converts it to an 'ArrayList' not a normal array (int[]).
public class CopyToArrayLinkedList {
public static void main(String[] args) {
LinkedList<Integer> linkedlist = new LinkedList<Integer>();
linkedlist.add(1);
linkedlist.add(2);
linkedlist.add(3);
linkedlist.add(4);
linkedlist.add(5);
Integer[] array = linkedlist.toArray();
for (int i = 0; i < linkedlist.size(); i++){
System.out.println( linkedlist.get(i)); }
}
}
}
Can't you use the build in toArray function thats available in the List api?
Integer[] array = linkedlist.toArray(new Integer[0]);
for (Integer i : array) {
System.out.println(i);
}
Simple, try like
Integer[] array = linkedlist.toArray(new Integer[linkedlist.size()]);
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 is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the below Java code excert. I am expecting to see the keys printed in a sorted manner (since I am using a TreeMap), but it is not sorting the keys. What I am missing?
CODE:
public class TreeMapTest {
static TreeMap<String,String> li=new TreeMap<String,String>();
static void readAndPrint(){
for (Map.Entry<String, String> entry:li.entrySet() ){
System.out.println(entry);
}
}
public static void main(String[] args) {
for (int i=0;i<10;i++){
String key = String.valueOf(new Random().nextInt(100));
String item = UUID.randomUUID().toString().substring(30);
li.put(key,item);
System.out.println(MessageFormat.format("inserting ({0},{1})",key,item));
}
readAndPrint();
}
}
Sample output:
inserting (7,f4b66a)
inserting (2,5f417d)
inserting (51,90bb9f)
inserting (99,4bfb73)
inserting (41,a4e9d5)
inserting (14,9286d6)
inserting (44,ec4fbd)
inserting (58,e7dd3a)
inserting (69,c54e66)
inserting (0,d1fbfe)
0=d1fbfe
14=9286d6
2=5f417d
41=a4e9d5
44=ec4fbd
51=90bb9f
58=e7dd3a
69=c54e66
7=f4b66a
99=4bfb73
As you see I am not getting the elements sorted ( I sometimes have the output sorted and sometime have it not sorted as above!). What I am missing or misunderstanding?
They are sorted, by the default sort order of Strings. Strings are ordered lexicographically, so "14" is considered less than "2".
If you want numerical sort order, you should have made the keys Integers instead of Strings.
One way of doing it whilst still keeping the keys as Strings would be to use the Treemap(Comparator) constructor:
static TreeMap<String, String> li = new TreeMap<>(Comparator.comparing(Integer::valueOf));
Of course, making the keys Integers also works.
If you want Integer based comparison then you need to have Integer keys in the map. Change
static TreeMap<String,String> li=new TreeMap<String,String>();
to
static TreeMap<Integer,String> li=new TreeMap<Integer,String>();
and , change put method to:
Integer key = new Random().nextInt(100);
String item = UUID.randomUUID().toString().substring(30);
li.put(key,item);
The map is ordering the keys lexicographically because they are strings (1 < 4 for the first character, and so on for the other characters).
The simplest way is to have the keys as Integers:
TreeMap<Integer,String> li=new TreeMap<>();
which will avoid the unnecessary need to convert the integer using String.valueOf.
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.
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.