gudii9
asked on
map related example
Hi,
above code generated below code.
key is--->b
key, b value 0
key is--->b
key, b value -1
result is--->[bd, bc]
I have not understood how above code is working.
Basically array is being passed from main method as below into the firstSwap method
then writing firstSwap() method creating hash map whose key is string and value is integer
then looping the input array
if map contains a key say b how it check same key at second element?
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class FirstSwap {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arr = { "bc", "bd" };
System.out.println("result is--->"+Arrays.toString(firstSwap(arr)));
}
public static String[] firstSwap(String[] strings) {
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < strings.length; i++) {
String key = String.valueOf(strings[i].charAt(0));
System.out.println("key is--->"+key);
if (map.containsKey(key)) {
int val = map.get(key);
if (val == -1) {
continue;
}
// swap
int pos = map.get(key);
String tmp = strings[pos];
strings[pos] = strings[i];
strings[i] = tmp;
// set a flag
map.put(key, -1);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String keys = entry.getKey().toString();
Integer value = entry.getValue();
System.out.println("key, " + keys + " value " + value);
}
} else {
map.put(key, i);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String keys = entry.getKey().toString();
Integer value = entry.getValue();
System.out.println("key, " + keys + " value " + value);
}
}
}
return strings;
}
}
above code generated below code.
key is--->b
key, b value 0
key is--->b
key, b value -1
result is--->[bd, bc]
I have not understood how above code is working.
Basically array is being passed from main method as below into the firstSwap method
String[] arr = { "bc", "bd" };
System.out.println("result is--->"+Arrays.toString(firstSwap(arr)));
then writing firstSwap() method creating hash map whose key is string and value is integer
public static String[] firstSwap(String[] strings) {
Map<String, Integer> map = new HashMap<String, Integer>();
then looping the input array
for (int i = 0; i < strings.length; i++) {
String key = String.valueOf(strings[i].charAt(0));
System.out.println("key is--->"+key);
if map contains a key say b how it check same key at second element?
if (map.containsKey(key)) {
int val = map.get(key);
if (val == -1) {
continue;
}
Below code also not clearint pos = map.get(key);
String tmp = strings[pos];
strings[pos] = strings[i];
strings[i] = tmp;
// set a flag
map.put(key, -1);//why we need -1???
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String keys = entry.getKey().toString();
Integer value = entry.getValue();//why we need get value what we are we doing with empty map by getting each row of entrySet set entries??
System.out.println("key, " + keys + " value " + value);
}
} else {//when the code comes to else loop??
map.put(key, i);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String keys = entry.getKey().toString();
Integer value = entry.getValue();
System.out.println("key, " + keys + " value " + value);
}
}
}
return strings;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
please tell me what the exact requirement is
ASKER
I have not clearly understood above code in terms of how the flow and workings
Did you not write the code?
I wouldn't spend time trying to understand a badly written peice of code. It wont achieve anything.
Move onto shaving the next yak.
I wouldn't spend time trying to understand a badly written peice of code. It wont achieve anything.
Move onto shaving the next yak.
ASKER