根据Java中的字符列表对单词列表进行排序

时间:2016-04-17 20:58:52

标签: java algorithm sorting

我有:

List<String> alphabet; (contains 26 unique characters as elements, for example 
qwertyuiosapdfghjklzcxvbnm)

List<String> wordsToArrange; contains words as elements, for example: 

 - apple 
 - stream
 - posthouse
 - sea
 - seed

我需要根据我制作的字母排列单词。

我目前的方法是循环。

alphabet(i) compare it with all the words charAt(0)
if only 1 is found i put it to a new list arrangedList 

but if 2 is found i go alphabet(i+1) till the letter is found and now i can put them in a right order to arrangedList....

then move back to alphabet(i+1) till alphabet(26) and now all should be arranged correctly...

我为这段代码写了一些基础,但我想在开始严肃的“循环”之前询问其他方法是什么。

谢谢!

后续 Changing specific characters in List of Strings in Java

3 个答案:

答案 0 :(得分:0)

我会:

  1. 将您单词中的每个字母映射到字母表的“倒数”。例如:
    • 'a'是该字符串的第11个字母,因此请将其映射到'k'
    • 'b'是该字符串的第24个字母,因此请将其映射到'x'
  2. 使用Collections.sort
  3. 对列表进行排序
  4. 将排序后的单词中的每个字母映射回原始字母,例如'k' -> 'a'; 'x' -> 'b'

答案 1 :(得分:0)

我使用Java 8的过滤器和流来过滤掉以某个字符开头的那些..然后我对结果进行排序。如果它需要在一个数组中,那么结合结果。

import java.util.Arrays;
/**
 * Created by Brandon on 2016-04-17.
 */
public class Main {
    public static void main(String[] args) {

        String[] array = new String[]{"apple", "stream", "posthouse", "sea", "seed"};
        //char[] indices = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char[] indices2 = new char[]{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};

        for (char key : indices2) {
            String[] result = Arrays.stream(array).filter(value -> value.charAt(0) == key).toArray(length -> new String[length]);

            if (result.length > 0) {
                Arrays.sort(result);
                System.out.println(Arrays.toString(result));
            }
        }
    }
}

如果使用indices,则结果为:

[apple]
[posthouse]
[sea, seed, stream]

如果使用indices2,则结果为:

[posthouse]
[apple]
[sea, seed, stream]

答案 2 :(得分:0)

如何将String包装在一个实现可比较的新类中?

可能是我未测试的一些边缘案例错误。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CompString {

    public static void main(String[] args) {
        List<ComparableString> list = new ArrayList<ComparableString>();

        list.add(new ComparableString("apple"));
        list.add(new ComparableString("stream"));
        list.add(new ComparableString("posthouse"));
        list.add(new ComparableString("sea"));
        list.add(new ComparableString("seed"));

        Collections.sort(list);

        System.out.println(list);
    }

}

class ComparableString implements Comparable<ComparableString> {

    String str;
    static String sortOrder = "qwertyuiosapdfghjklzcxvbnm";

    public ComparableString(String string) {
        str = string;
    }

    @Override
    public String toString() {
        return str;
    }

    @Override
    public int compareTo(ComparableString other) {
        for (int i = 0; i < Math.min(this.str.length(), other.str.length()); i++) {
            int thisOrder = ComparableString.sortOrder.indexOf(this.str.charAt(i));
            int thatOrder = ComparableString.sortOrder.indexOf(other.str.charAt(i));

            int order = thisOrder - thatOrder;
            if (order != 0) {
                return order;
            }

        }

        if (this.str.length() > other.str.length()) {
            return -1;
        } else if (this.str.length() < other.str.length()) {
            return 1;
        }
        return 0;
    }
}