Java - 迭代ArrayList和Find Anagrams

时间:2015-03-09 04:09:20

标签: java loops linked-list anagram

我有一个已排序的规范单词列表,我想用迭代器迭代列表,找到匹配的规范单词,这些单词将具有相同的Anagrams,然后将它们添加到一个单独的LinkedList中,如果它们匹配,则将它们组合在一起。我该怎么做呢?我会为同一个列表一次运行两个迭代器,并将它们放在一个嵌套的while循环中,第一个元素上有一个迭代器,第二个迭代器里面搜索匹配的所有元素的相同列表然后如果匹配则将它添加到List中发现了什么?有什么想法吗?

这是我到目前为止所做的事情:

驱动程序类文件:

import java.util.*;
import java.io.*;

public class Programming9Driver {

   public static void main(String[] theArgs) {
      LinkedList<Word> wordObjects = new LinkedList<Word>();
      ArrayList<String> localWords = new ArrayList<String>();
      BufferedReader localInput = null;
      BufferedWriter localOutput = null;


      try {
         String localLine;
         localInput = new BufferedReader(new FileReader("words.txt"));
//         localOutput = new BufferedWriter(new FileWriter("out9.txt"));
         while ((localLine = localInput.readLine()) != null) {
            if (localLine != "") {
            localWords.add(localLine);
            }
//          
//         
         }
/*
*/
         localInput.close();
//       localOutput.close();
      } catch (Exception e) {
         System.out.println("Difficulties opening the file! " + e);
         System.exit(1);
      }

      localWords.removeAll(Collections.singleton(""));
      Map<String, List<String>> anagramList = new HashMap<String, List<String>>();
      Iterator<String> iterAdd = localWords.iterator();

      while (iterAdd.hasNext()) {
         wordObjects.add(new Word(iterAdd.next()));   
      }
      Collections.sort(wordObjects);

      LinkedList<AnagramFamily> anagramObjects = new LinkedList<AnagramFamily>();

  }       
}

Word.java:

import java.util.*;

public class Word implements Comparable<Word>{

   private String myWord;

   private String myCanon;

   private Map<String, List<String>> myCanonKey = new HashMap<String, List<String>>();

   public Word(final String theWord) { 
      myWord = theWord;
      myCanon = canonForm();
      myCanonKey = canonWords();    
   }

   public String canonForm() {
      String canonWord = "";
      Character[] localChars = new Character[myWord.length()];

      for (int i = 0; i < localChars.length; i++) {
         localChars[i] = myWord.charAt(i);
      }

      Arrays.sort(localChars);

      for (int i = 0; i < localChars.length; i++) {
         canonWord += localChars[i];
      }
      return canonWord;         
   }

   public Map<String, List<String>> canonWords() {
      ArrayList<String> canonList = new ArrayList<String>();
      Map<String, List<String>> canonKey = new HashMap<String, List<String>>();
      canonList.add(myWord);
      canonKey.put(myCanon, canonList);
      return canonKey;
   }

   public int compareTo(Word theOther) {
      int result = canonForm().compareTo(theOther.canonForm());
      return result;
   }

   public String toString() {
      String result = "";
      result = myWord;
      if (myWord == "" || myCanon == "") {
         result = "";
      }
      return result;
   }        
}

如何从我的主驱动程序调用以使用Word类java文件中的canonWords()方法?

1 个答案:

答案 0 :(得分:1)

实现它的一种方法是遍历所有字符串,从原始字母顺序排列的char数组创建一个新字符串,并使用新字符串作为Map<String, List<String>>中的键。

编辑:说实话,您发布的代码似乎有点过头了。这是对我的想法的证明:

Collection<LinkedList<String>> groupAnagrams(List<String> words) {
    Map<String, LinkedList<String>> anagramMap = new HashMap<>();
    for(String word : words) {
        char[] wordChars = word.toCharArray();
        Arrays.sort(wordChars);
        String sortedKey = new String(wordChars);
        LinkedList<String> anagramList = anagramMap.get(sortedKey);
        if(anagramList == null) {
            anagramMap.put(sortedKey, anagramList = new LinkedList<>());
        }
        anagramList.add(word);
    }
    return anagramMap.values();
}

编辑2:只是为了好玩,这里是Java 8实现:

Collection<List<String>> groupAnagrams(List<String> words) {
    return words.stream().collect(groupingBy(w -> {
                char[] chars = w.toCharArray();
                Arrays.sort(chars);
                return new String(chars);
            })).values();
}