将数组列表拆分为java中的多个列表

时间:2013-08-14 10:49:56

标签: java arraylist

我有一个数组列表,当填充时有一个键和一个我想知道的值,如果有一种方法可以在重复键上拆分它,例如我当前的数据是这样的:

[RoleID_123.0, UserHandel_tom, Password_12345.0, prevPassword_null, userCaption_thomas, Email_tom@tom.tom, RoleID_124.0, UserHandel_dave, Password_ghadf, prevPassword_sdfsd, userCaption_david, Email_dave@dave.dave, RoleID_125.0, UserHandel_trevor, Password_tre, prevPassword_null, userCaption_trev, Email_trev@trev.trev]

我希望它更像这样:

[RoleID_123.0, UserHandel_tom, Password_12345.0, prevPassword_null, userCaption_thomas, Email_tom@tom.tom]

[RoleID_124.0, UserHandel_dave, Password_ghadf, prevPassword_sdfsd, userCaption_david, Email_dave@dave.dave]

[RoleID_125.0, UserHandel_trevor, Password_tre, prevPassword_null, userCaption_trev, Email_trev@trev.trev]

有没有办法将其拆分为说出角色ID或我是否采用了错误的方式?

5 个答案:

答案 0 :(得分:1)

如果它是ArrayList,则没有内置函数来分割这样的数据;你必须手动完成。如果你知道构成单个结构的连续字段的数量,这应该不会太难;像这样的东西:

// 6 because there are 6 fields
for (int i = 0; i < arrayList.size(); i = i + 6) {
  List thisList = arrayList.subList(i, i + 5);
  // ... Now do whatever you want with thisList - it contains one structure.
}

如果字段的数量可以改变,那么你将不得不做一些更动态的事情,并循环寻找RoleID字段,例如。

答案 1 :(得分:1)

您可以尝试使用HashMap

    private static class MyItemHashMap extends HashMap {  
    public Item add(Item item) {  
        get(item).add(item);  
        return item;  
    }  

    public List get(Item key) {  
        List list = (List) get(createItemKey((Item) key));  
        return list == null ? createItemEntry((Item) key) : list;  
    }  

    private List createItemEntry(Item item) {  
        List list = new ArrayList();  
        put(createItemKey(item), list);  
        return list;  
    }  

    private Object createItemKey(Item item) {  
        return item.getSplitterProperty();  
    }  
    }  

    public static void main(String[] args) {  
    MyItemHashMap itemMapped = new MyItemHashMap();  
    List items = Arrays.asList(new Object[]{new Item("A"), new Item("B"),
    new  Item("C")});  
    for (Iterator iter = items.iterator(); iter.hasNext();) {  
    Item item = (Item) iter.next();  
    itemMapped.add(item);  
     }  
     }  

答案 2 :(得分:0)

我使用HashMap来分隔数据而不是一个长的ArrayList(你不应该在第一个实例中存储这样的数据)

HashMap<String,ArrayList<String>> hm = new HashMap<String,ArrayList<String>>;

// For each list:
    ArrayList<String> arr = new ArrayList<String>;
    arr.add("each element");
    hm.put("RoleID_123.0", arr);

通过这种方式,您将得到一个三维结构,其中一个键(“RoleID ...”)指向其子元素。

答案 3 :(得分:0)

试试这个

String[] str=new String[]{"RoleID_123.0", "UserHandel_tom", "Password_12345.0", "prevPassword_null", "userCaption_thomas", "Email_tom@tom.tom", "RoleID_124.0", "UserHandel_dave", "Password_ghadf", "prevPassword_sdfsd", "userCaption_david", "Email_dave@dave.dave", "RoleID_125.0", "UserHandel_trevor", "Password_tre", "prevPassword_null", "userCaption_trev", "Email_trev@trev.trev"};
 List<String> list=new ArrayList<>(Arrays.asList(str));
 List<String> subList=list.subList(0,5);

您可以尝试与此相似的内容

答案 4 :(得分:0)

如果你想采用Linq-ee Libraried方法,这就好了,它需要使用几个委托对象:

import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Collections2.transform;
//...


final List<String> yourList = //...
final int RECORD_LENGTH = 6;

Collection<String> roleIdValues = filter(yourList, new Predicate<String>() {
    public boolean apply(@Nullable String input) {
        return input != null && input.startsWith("RoleID");
    }
});
Collection<Collection<String>> splitRecords = transform(roleIdValues, new Function<String, Collection<String>>() {
    @Nullable public Collection<String> apply(@Nullable String input) {
        return yourList.subList(yourList.indexOf(input), RECORD_LENGTH);
    }
});

如果Oracle按时交付Java 8,您将能够以的方式更加光滑的庄园。具有讽刺意味的是,你不能提供番石榴图书馆的人是