Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 两个ArrayList之间的同步排序_Java_Collections_Arraylist_Sorting - Fatal编程技术网

Java 两个ArrayList之间的同步排序

Java 两个ArrayList之间的同步排序,java,collections,arraylist,sorting,Java,Collections,Arraylist,Sorting,我有两个数组列表 第一个包含一组大写和小写的单词 标点符号 另一个包含相同的词组,但带有 删除大小写和标点符号 第二个数组将所有单词按字母顺序排列,每个单词中的所有字母按字母顺序排列。它看起来像这样: aemnsy demrru ehllo ist no 我希望这样,当我将ArrayList 2中的单词按字母顺序排列时,ArrayList one中的所有单词都会按照以下顺序排列: ArrayList1 ..... ArrayList2 Yes-Man ........ aemnsy

我有两个数组列表

  • 第一个包含一组大写和小写的单词 标点符号

  • 另一个包含相同的词组,但带有 删除大小写和标点符号

第二个数组将所有单词按字母顺序排列,每个单词中的所有字母按字母顺序排列。它看起来像这样:

aemnsy
demrru
ehllo
ist
no
我希望这样,当我将ArrayList 2中的单词按字母顺序排列时,ArrayList one中的所有单词都会按照以下顺序排列:

ArrayList1 ..... ArrayList2

Yes-Man ........ aemnsy

MURDER! ........ demrru

Hello .......... ehllo

It's ........... ist

ON ............. no

我试图用一个for语句或两个for语句做一个循环,但它最终不起作用,变得很长。我该怎么做?如何有效地执行此操作?

第一种方法-我使用的映射的键来自ArrayList 2,值来自ArrayList 1。将数据放到地图上取决于您。对ArrayList 2进行排序后,我从map中获取其值。

  List<String> arrList1 = new ArrayList<String>();

            arrList1.add("MURDER!");
            arrList1.add("It's");
            arrList1.add("Hello");
            arrList1.add("Yes-Man");
            arrList1.add("ON");

            List<String> arrList2 = new ArrayList<String>();
            arrList2.add("demrru");
            arrList2.add("aemnsy");
            arrList2.add("ist");
            arrList2.add("ehllo");
            arrList2.add("no"); 

            Map<String, String> map1 = new HashMap<String, String>();
            map1.put("aemnsy", "Yes-Man");
            map1.put("demrru", "MURDER!");
            map1.put("ehllo", "Hello");
            map1.put("ist", "It's");
            map1.put("no", "ON");

            Collections.sort(arrList2);

            for (String s : arrList2){
                System.out.println(s + "..........." + map1.get(s));
            }
// Sorted by key:
[Yes-Man, MURDER!, Hello, It's, ON]
[aemnsy, demrru, ehllo, ist, no]
[1, 2, 3, 4, 5]
[0.1, 0.2, 0.3, 0.4, 0.5]
List arrList1=new ArrayList();
arrList1.添加(“谋杀!”);
arrList1.添加(“其”);
arrList1.添加(“你好”);
arrList1.添加(“应声虫”);
arrList1.添加(“ON”);
List arrList2=新的ArrayList();
arrList2.添加(“demrru”);
arrList2.添加(“aemnsy”);
arrList2.添加(“ist”);
arrList2.添加(“ehllo”);
arrList2.添加(“否”);
Map map1=新的HashMap();
地图1.put(“aemnsy”、“Yes Man”);
地图1.put(“demrru”,“谋杀!”);
map1.put(“ehllo”,“Hello”);
map1.put(“ist”,“It's”);
地图1.放置(“否”、“开启”);
Collections.sort(arrList2);
用于(字符串s:arrList2){
System.out.println(s+“……”+map1.get(s));
}
第二种方法-另一种方法是只能使用已排序的树状图,而不是两个ArrayList。

Map<String, String> map2 = new TreeMap<String, String>();
            map2.put("ehllo", "Hello");
            map2.put("aemnsy", "Yes-Man");
            map2.put("demrru", "MURDER!");
            map2.put("no", "ON");
            map2.put("ist", "It's");

            for (Map.Entry<String, String> entry : map2.entrySet())
            {
                System.out.println(entry.getKey() + "/" + entry.getValue());
            }
  selectionSort1(arrList2, arrList1);

    for(int i = 0; i < arrList1.size(); i++){
        System.out.println(arrList2.get(i) + "---" + arrList1.get(i));
    }

   public static void selectionSort1(List<String> x, List<String> y) {
    for (int i=0; i<x.size()-1; i++) {
        for (int j=i+1; j<x.size(); j++) {
            if (x.get(i).compareTo(x.get(j)) > 0) {
                //... Exchange elements in first array
                String temp = x.get(i);
                x.set(i, x.get(j));
                x.set(j, temp);

                //... Exchange elements in second array
                temp = y.get(i);
                y.set(i, y.get(j));
                y.set(j, temp);
            }
        }
    }
}
map2=newtreemap();
map2.put(“ehllo”,“Hello”);
地图2.put(“aemnsy”、“Yes Man”);
地图2.put(“demrru”,“谋杀!”);
map2.放置(“否”、“开启”);
map2.put(“ist”,“It's”);
对于(Map.Entry:map2.entrySet())
{
System.out.println(entry.getKey()+“/”+entry.getValue());
}
第三种方法-仅使用2个ArrayList,但我们必须自己编写排序方法。您是否注意到您的两个ArrayList元素,例如ArrayList 2中的aemnsy和ArrayList 1中的Yes Man,具有相同的索引?我使用了这一点。

Map<String, String> map2 = new TreeMap<String, String>();
            map2.put("ehllo", "Hello");
            map2.put("aemnsy", "Yes-Man");
            map2.put("demrru", "MURDER!");
            map2.put("no", "ON");
            map2.put("ist", "It's");

            for (Map.Entry<String, String> entry : map2.entrySet())
            {
                System.out.println(entry.getKey() + "/" + entry.getValue());
            }
  selectionSort1(arrList2, arrList1);

    for(int i = 0; i < arrList1.size(); i++){
        System.out.println(arrList2.get(i) + "---" + arrList1.get(i));
    }

   public static void selectionSort1(List<String> x, List<String> y) {
    for (int i=0; i<x.size()-1; i++) {
        for (int j=i+1; j<x.size(); j++) {
            if (x.get(i).compareTo(x.get(j)) > 0) {
                //... Exchange elements in first array
                String temp = x.get(i);
                x.set(i, x.get(j));
                x.set(j, temp);

                //... Exchange elements in second array
                temp = y.get(i);
                y.set(i, y.get(j));
                y.set(j, temp);
            }
        }
    }
}
selectionSort1(arrList2,arrList1);
对于(int i=0;i
public class MapAlph {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<String, String>();
        String txt = "Murde!r!";
        ArrayList<Character> alph = new ArrayList<Character>();
        for (int i = 0; i < txt.length(); i++)
            if (Character.isLetter(txt.charAt(i)))
                alph.add(txt.charAt(i));

        Collections.sort(alph);
        Collections.reverse(alph);
        String val = "";
        for (Character c : alph)
            val += c;

        map.put(txt, val);
        System.out.print(txt + " ........ " + map.get(txt));
    }
}
公共类MapAlph{
公共静态void main(字符串[]args){
HashMap=newHashMap();
String txt=“Murde!r!”;
ArrayList alph=新的ArrayList();
对于(int i=0;i
如果您愿意,您可以利用

Map<String, String> sortedMap = new TreeMap<String, String>();
sortedMap.put("demrru", "MURDER!");
sortedMap.put("ist", "It's");
sortedMap.put("aemnsy", "Yes-Man");
sortedMap.put("ehllo", "Hello");
sortedMap.put("no", "ON");
Map sortedMap=newtreemap();
分类地图放置(“demrru”,“谋杀!”);
分类地图放置(“ist”,“它是”);
分类地图放置(“aemnsy”、“Yes Man”);
分类地图放置(“ehllo”,“Hello”);
分类地图放置(“否”、“打开”);
您可以尝试以下方法:

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


public class SortService {

    public static void main(String[] args) {
        Map<String, String> originalMap = new HashMap<String, String>();
        originalMap.put("aemnsy", "Yes-Man");
        originalMap.put("demrru", "MURDER!");
        originalMap.put("ehllo", "Hello");
        originalMap.put("ist", "It's");
        originalMap.put("no", "ON");

        Map<String, String> sortedMap = new TreeMap<String, String>(originalMap);
        System.out.println(sortedMap);
    }

}

这是一个基于单个“键”列表对多个列表进行排序的函数。这些列表不需要是相同的类型,这里的键列表是类型
String
,用于对
字符串
整数
双倍
列表()进行排序:


排序函数 可以找到一个Ideone示例,其中包括参数验证和测试用例

public static <T extends Comparable<T>> void keySort(
                                        final List<T> key, List<?>... lists){
    // Create a List of indices
    List<Integer> indices = new ArrayList<Integer>();
    for(int i = 0; i < key.size(); i++)
        indices.add(i);

    // Sort the indices list based on the key
    Collections.sort(indices, new Comparator<Integer>(){
        @Override public int compare(Integer i, Integer j) {
            return key.get(i).compareTo(key.get(j));
        }
    });

    // Create a mapping that allows sorting of the List by N swaps.
    Map<Integer,Integer> swapMap = new HashMap<Integer, Integer>(indices.size());

    // Only swaps can be used b/c we cannot create a new List of type <?>
    for(int i = 0; i < indices.size(); i++){
        int k = indices.get(i);
        while(swapMap.containsKey(k))
            k = swapMap.get(k);

        swapMap.put(i, k);
    }

    // for each list, swap elements to sort according to key list
    for(Map.Entry<Integer, Integer> e : swapMap.entrySet())
        for(List<?> list : lists)
            Collections.swap(list, e.getKey(), e.getValue());
}
publicstaticvoidkeysort(
最终列表键,列表…列表){
//创建索引列表
列表索引=新的ArrayList();
对于(int i=0;i
听起来你想使用一个
映射而不是两个
列表
如果可以的话,可以使用
树映射
,其中键是ArrayList 2中的字符串,值是ArrayList 1中的值。这两个集合有什么问题。排序调用?@Thihara你确定你的评论吗?你读过这个问题吗?哦,乍一看我认为ught他只需要对两个数组列表进行排序…在第二次阅读时,我明白了为什么地图是答案…你只是坐在旁边或什么的吗?我非常喜欢只使用地图,但遗憾的是这不是练习的目标。但是,对两个数组列表使用地图…好吧,让我玩一会儿,我会得到bac如果你要使用
TreeMap
,最好将其声明为
TreeMap-map2
NavigableMap
,以便使用类/接口中的所有方法(检查我的注释)。在尝试使用
public static <T extends Comparable<T>> void keySort(
                                        final List<T> key, List<?>... lists){
    // Create a List of indices
    List<Integer> indices = new ArrayList<Integer>();
    for(int i = 0; i < key.size(); i++)
        indices.add(i);

    // Sort the indices list based on the key
    Collections.sort(indices, new Comparator<Integer>(){
        @Override public int compare(Integer i, Integer j) {
            return key.get(i).compareTo(key.get(j));
        }
    });

    // Create a mapping that allows sorting of the List by N swaps.
    Map<Integer,Integer> swapMap = new HashMap<Integer, Integer>(indices.size());

    // Only swaps can be used b/c we cannot create a new List of type <?>
    for(int i = 0; i < indices.size(); i++){
        int k = indices.get(i);
        while(swapMap.containsKey(k))
            k = swapMap.get(k);

        swapMap.put(i, k);
    }

    // for each list, swap elements to sort according to key list
    for(Map.Entry<Integer, Integer> e : swapMap.entrySet())
        for(List<?> list : lists)
            Collections.swap(list, e.getKey(), e.getValue());
}