0

I have an array list which when populated has a key and a value I want to know if there is a way of splitting it on repeating keys for example my current data is like this:

[RoleID_123.0, UserHandel_tom, Password_12345.0, prevPassword_null, userCaption_thomas, [email protected], RoleID_124.0, UserHandel_dave, Password_ghadf, prevPassword_sdfsd, userCaption_david, [email protected], RoleID_125.0, UserHandel_trevor, Password_tre, prevPassword_null, userCaption_trev, [email protected]]

I want it to come out more like this:

[RoleID_123.0, UserHandel_tom, Password_12345.0, prevPassword_null, userCaption_thomas, [email protected]]

[RoleID_124.0, UserHandel_dave, Password_ghadf, prevPassword_sdfsd, userCaption_david, [email protected]]

[RoleID_125.0, UserHandel_trevor, Password_tre, prevPassword_null, userCaption_trev, [email protected]]

Is there a way to split it on say role id or am I going about this the wrong way?

3
  • subList method is your friend !!. Commented Aug 14, 2013 at 10:51
  • You just have to loop through your existing list and create a new list of lists using your splitting criteria Commented Aug 14, 2013 at 10:51
  • IMHO instead of converting that list you should generate the proper list in first time. Also each of your new lists look like actually an object, which makes me think what you really want is a list of such objects. Commented Aug 14, 2013 at 10:52

5 Answers 5

1

You can try by using 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);  
     }  
     }  
Sign up to request clarification or add additional context in comments.

Comments

1

If it is an ArrayList, there is no built-in function to split data like this; you will have to do it manually. If you know the number of consecutive fields that make a single structure, this shouldn't be too hard; something like this:

// 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.
}

If the number of fields can change then you'll have to do something a little more dynamic and loop through looking for a RoleID field, for example.

Comments

0

I'd use a HashMap to seperate the data instead of one long ArrayList ( you shouldn't have stored the data like this in the first instance )

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);

This way you will end up with a three dimensional structure with a key ( "RoleID..." ) pointing to its child elements.

Comments

0

Try this

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

You can try something similar to this

Comments

0

If you feel like taking a Linq-ee Libraried approach, this is about as good as it gets, and it requires use of a couple delegate objects:

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);
    }
});

If Oracle had delivered Java 8 on time you would be able to do this in a way more slick manor. Ironically the reason you cant was provided by the same people providing the guava library

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.