- 第一次因为遍历出错
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for(int i=0; i<paths.length; i++){
String files = paths[i];
String[] tmp = files.split(" ");
String dir = tmp[0];
for(int j=1; j<tmp.length ;j++){
StringBuilder str = new StringBuilder(dir);
str.append('/');
int h=0;
while(h<tmp[j].length() && tmp[j].charAt(h)!='('){
str.append(tmp[j].charAt(h));
h++;
}
h++;
StringBuilder fileContent = new StringBuilder();
while(h<tmp[j].length() && tmp[j].charAt(h)!=')'){
fileContent.append(tmp[j].charAt(h));
h++;
}
if(!map.containsKey(fileContent.toString())){
ArrayList<String> listDir = new ArrayList<String>();
// System.out.println(str.toString());
listDir.add(str.toString());
map.put(fileContent.toString(),listDir);
} else{
ArrayList<String> listDir= map.get(fileContent.toString());
listDir.add(str.toString());
map.put(fileContent.toString(),listDir);
}
}
}
List<List<String>> result = new ArrayList<List<String>>();
for(int i=0; i<map.size(); i++){
ArrayList<String> str = new ArrayList<String>();
for(int h=0; h<map.get(i).size(); h++)
System.out.println(map.get(i).get(h));
result.add(map.get(i));
}
return result;
}
}
这样方式遍历get(i)是Null,原因当然是API里面没有get(i),只有get(Object o).
- for( : )对map的values遍历
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for(int i=0; i<paths.length; i++){
String files = paths[i];
String[] tmp = files.split(" ");
String dir = tmp[0];
for(int j=1; j<tmp.length ;j++){
StringBuilder str = new StringBuilder(dir);
str.append('/');
int h=0;
while(h<tmp[j].length() && tmp[j].charAt(h)!='('){
str.append(tmp[j].charAt(h));
h++;
}
h++;
StringBuilder fileContent = new StringBuilder();
while(h<tmp[j].length() && tmp[j].charAt(h)!=')'){
fileContent.append(tmp[j].charAt(h));
h++;
}
if(!map.containsKey(fileContent.toString())){
ArrayList<String> listDir = new ArrayList<String>();
// System.out.println(str.toString());
listDir.add(str.toString());
map.put(fileContent.toString(),listDir);
} else{
ArrayList<String> listDir= map.get(fileContent.toString());
listDir.add(str.toString());
map.put(fileContent.toString(),listDir);
}
}
}
List<List<String>> result = new ArrayList<List<String>>();
for(List<String> s: map.values()) {
if(s.size() > 1) {
result.add(s);
}
}
return result;
}
}
- 看到一个博主用getOrDefault写的很简洁。
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for(int i=0; i<paths.length; i++){
String files = paths[i];
String[] tmp = files.split(" ");
String dir = tmp[0];
for(int j=1; j<tmp.length ;j++){
StringBuilder str = new StringBuilder(dir);
str.append('/');
int h=0;
while(h<tmp[j].length() && tmp[j].charAt(h)!='('){
str.append(tmp[j].charAt(h));
h++;
}
h++;
StringBuilder fileContent = new StringBuilder();
while(h<tmp[j].length() && tmp[j].charAt(h)!=')'){
fileContent.append(tmp[j].charAt(h));
h++;
}
System.out.println(fileContent);
ArrayList<String> newContent = (ArrayList<String>) map.getOrDefault(fileContent.toString(), new ArrayList<String>());
newContent.add(str.toString());
map.put(fileContent.toString(),newContent);
}
}
List<List<String>> result = new ArrayList<List<String>>();
for(List<String> s: map.values()) {
if(s.size() > 1) {
result.add(s);
}
}
return result;
}
}
- map的遍历方式
- 获得key-value的Set集合
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
// 然后用for循环遍历
for(Map.Entry<String, Integer> entry : entrySet){
System.out.print(entry.getKey());
System.out.println(entry.getValue());
}
//也可以用迭代器遍历
Iterator iter = entrySet.iterator();
while (iter.hasNext()) {
// 遍历时,需先获取entry,再分别获取key、value
Map.Entry entry = (Map.Entry) iter.next();
System.out.print((String) entry.getKey());
System.out.println((Integer) entry.getValue());
}
- 获得key的Set集合 再遍历
Set<String> keySet = map.keySet();
// 通过for循环
for(String key : keySet){
System.out.print(key);
System.out.println(map.get(key));
}
// 通过迭代器:先获得key的Iterator,再循环遍历
Iterator iter2 = keySet.iterator();
String key = null;
while (iter2.hasNext()) {
key = (String)iter2.next();
System.out.print(key);
System.out.println(map.get(key));
}
- 获得value的Set集合 再遍历
Collection valueSet = map.values();
Iterator iter3 = valueSet.iterator();
while (iter3.hasNext()) {
System.out.println(iter3.next());
}
网友评论