简体   繁体   English

Java-根据后缀从列表创建子列表

[英]Java - Create sublist from a list based on suffix

I'm trying to create sublists from a list based on the suffix. 我正在尝试根据后缀从列表创建子列表。

public class Test {

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

        List<String> myList = new ArrayList<String>();
        myList.add("Text_1");
        myList.add("XYZ_3");
        myList.add("ABC_1");
        myList.add("Text_2");
        myList.add("Text_3");
        myList.add("XYZ_1");
        myList.add("XYZ_2");
        myList.add("ABC_2");

        for (String item : myList) {
            List<String> tempList = new ArrayList<String>();
            String suffix = item.substring(item.lastIndexOf("_"));
            tempList.add(item);
            for (String value : myList) {
                if (value.endsWith(suffix) && !tempList.contains(value)) {
                    tempList.add(value);
                }
            }
            System.out.println(tempList);
        }
    }
}

I'm expecting like below 我期望像下面

// Text_1, ABC_1, XYZ_1
// Text_2, ABC_2, XYZ_2
// Text_3, XYZ_3

But the actual is 但是实际是

[Text_1, ABC_1, XYZ_1]
[XYZ_3, Text_3]
[ABC_1, Text_1, XYZ_1]
[Text_2, XYZ_2, ABC_2]
[Text_3, XYZ_3]
[XYZ_1, Text_1, ABC_1]
[XYZ_2, Text_2, ABC_2]
[ABC_2, Text_2, XYZ_2]

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

Try using a TreeMap ? 尝试使用TreeMap吗?

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class QuickTester {

    public static void main(String[] args) {

        List<String> myList = new ArrayList<String>();
        myList.add("Text_1");
        myList.add("XYZ_3");
        myList.add("ABC_1");
        myList.add("Text_2");
        myList.add("Text_3");
        myList.add("XYZ_1");
        myList.add("XYZ_2");
        myList.add("ABC_2");

        TreeMap <String, ArrayList<String>> map = new TreeMap<>();

        for (String item : myList) {

            // contents[0] is value, contents[1] is suffix
            String [] contents = item.split("_");

            if(!map.containsKey(contents[1])) {
                ArrayList<String> values = new ArrayList<>();
                values.add(contents[0]);
                map.put(contents[1], values);
            }
            else {
                map.get(contents[1]).add(contents[0]);
            }
        }

        for(String key : map.keySet()) {
            System.out.println("Key: " + key);
            System.out.print("Values: ");
            for(String value : map.get(key)) {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }
}

Output: 输出:

Key: 1
Values: Text ABC XYZ 
Key: 2
Values: Text XYZ ABC 
Key: 3
Values: XYZ Text 

You can use a list for adding seen suffixes. 您可以使用列表来添加可见的后缀。 If this list contains the suffix, you do nothing. 如果此列表包含后缀,则您什么也不做。

    List<String> seenSuffixes = new ArrayList<String>();
    for (String item : myList) {

        String suffix = item.substring(item.lastIndexOf("_"));
        if(!seenSuffixes.contains(suffix)){
             seenSuffixes.add(suffix);
             List<String> tempList = new ArrayList<String>();
             tempList.add(item);
             for (String value : myList) {
                  if (value.endsWith(suffix) && !tempList.contains(value)) {
                       tempList.add(value);
                  }
             }
             System.out.println(tempList);
           }

        }

There are several problems with your code: 您的代码有几个问题:

1) you build the tempList on every item, so while the result are correct, you will see them for every item in myList. 1)您在每个项目上都建立了tempList,因此当结果正确时,您将在myList中的每个项目上看到它们。 you need to "remember" each suffix you process so that you won't process it again. 您需要“记住”您处理的每个后缀,以免再次处理。 a HashMap will do the job 一个HashMap可以胜任

2) if you want tempList to have uniqe values, use Set 2)如果您希望tempList具有唯一值,请使用Set

here is the complete solution 这是完整的解决方案

    public static void main(String args[]) {
    Map<String, Set<String>> subList = new HashMap<>();

      List<String> myList = new ArrayList<String>();
      myList.add("Text_1");
      myList.add("XYZ_3");
      myList.add("ABC_1");
      myList.add("Text_2");
      myList.add("Text_3");
      myList.add("XYZ_1");
      myList.add("XYZ_2");
      myList.add("ABC_2");

      for (String item : myList) {
          String suffix = item.substring(item.lastIndexOf("_"));
          if (subList.containsKey(suffix))  continue;
          Set<String> tempSet = new HashSet<String>();
          tempSet.add(item);
          for (String value : myList) {
              if (value.endsWith(suffix)) {
                  tempSet.add(value);
              }
          }
          subList.put(suffix, tempSet);
      }
      System.out.println(subList);
  }

output: 输出:

{_1=[Text_1, ABC_1, XYZ_1], _2=[ABC_2, XYZ_2, Text_2], _3=[XYZ_3, Text_3]}

The issues as @sharonbn, but also you can update the value when you get a suffix everytime. 该问题为@sharonbn,但您也可以在每次获得后缀时更新其值。

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

List<String> myList = new ArrayList<String>();
myList.add("Text_1");
myList.add("XYZ_3");
myList.add("ABC_1");
myList.add("Text_2");
myList.add("Text_3");
myList.add("XYZ_1");
myList.add("XYZ_2");
myList.add("ABC_2");

for (int i = 0; i < myList.size(); i++) {
    String item = myList.get(i);
    List<String> tempList = new ArrayList<String>();
    int location = item.lastIndexOf("_");
    if (location < 0) continue;
    String suffix = item.substring(location);
    tempList.add(item);
    for (int j = i + 1; j < myList.size(); j++) {
        String value = myList.get(j);
        if (value.endsWith(suffix) && !tempList.contains(value)) {
            tempList.add(value);
            myList.set(j, "-------");
        }
    }
    System.out.println(tempList);
}

Have a look at this ,i am using a map with suffix values as a key and List of common types as a value . 看看这个,我正在使用带有后缀值作为键和常见类型列表作为值的映射。

List<String> myList = new ArrayList<String>();
    myList.add("Text_1");
    myList.add("XYZ_3");
    myList.add("ABC_1");
    myList.add("Text_2");
    myList.add("Text_3");
    myList.add("XYZ_1");
    myList.add("XYZ_2");
    myList.add("ABC_2");
    Map<String, List<String>> map=new HashMap<String,List<String>>();
    for(String str:myList){
        String[] arryStr=str.split("_");
        if(! map.containsKey(arryStr[1])){
            List<String> tmpList=new ArrayList<String>();
            tmpList.add(str);
            map.put(arryStr[1],tmpList);
        }else{
            List<String> existingList=map.get(arryStr[1]);
            existingList.add(str);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM