Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

java - Filter an ArrayList class into another based on one of the items

I'm trying to sort my commentList and result with an itemList with only the items where the last value, itemPosition equals the current position. I have a class for the List items with 4 values:

class CommentItem {
    int profile; 
    String user;
    String message; 
    int itemPosition;
}

I am trying to use this code to sort it, but it is only adding the correct number of values instead of the actual values that I want.

for (CommentItem item : commentList) {
    if (item.getItemPosition() == position) {
        itemList.add(new CommentItem(
            commentList.get(position).getProfile(),
            commentList.get(position).getUser(),
            commentList.get(position).getMessage(),
            commentList.get(position).getItemPosition()
        ));
    }
}

This is the commentList:

commentList = new ArrayList<>();
commentList.add(new CommentItem(R.drawable.circleicon, "User0", "Message0", 1));
commentList.add(new CommentItem(R.drawable.circleicon, "User1", "Message1", 2));
commentList.add(new CommentItem(R.drawable.circleicon, "User2", "Message2", 3));
commentList.add(new CommentItem(R.drawable.circleicon, "User3", "Message3", 4));
commentList.add(new CommentItem(R.drawable.circleicon, "User4", "Message4", 1));
commentList.add(new CommentItem(R.drawable.circleicon, "User5", "Message5", 2));
commentList.add(new CommentItem(R.drawable.circleicon, "User6", "Message6", 3));
commentList.add(new CommentItem(R.drawable.circleicon, "User7", "Message7", 4));
commentList.add(new CommentItem(R.drawable.circleicon, "User8", "Message8", 0));

The resulting itemList with the position has these items displayed:

commentList.add(new CommentItem(R.drawable.circleicon, "User0", "Message0", 1));
commentList.add(new CommentItem(R.drawable.circleicon, "User1", "Message1", 2));

I have also tried all of these codes:

for (CommentItem item : commentList) {
    if (item.getItemPosition() == position) {
        itemList.add(new CommentItem(
            item.getProfile(), item.getUser(), 
            item.getMessage(), item.getItemPosition()
    ));
    }
}
for (CommentItem item : commentList) {
    if (item.getItemPosition() == position) {
        itemList.add(item);
    }
}
for (CommentItem item : commentList) {
    if (item.getItemPosition() == position) {
        itemList.add(commentList.get(commentList.indexOf(item)));
    }
}

With the same result.

Thank you in advance!

question from:https://stackoverflow.com/questions/66048587/filter-an-arraylist-class-into-another-based-on-one-of-the-items

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, declare your commentList correctly to avoid compile errors:

ArrayList<CommentItem>commentList = new ArrayList<CommentItem>();

Also, it is more efficient to write:

...new ArrayList<CommentItem>(Arrays.asList(/*insert contents here*/));

Instead of .add() .add() .add()...

Alternatively, you could use a for loop to create all those new CommentItems. Since you are making user1, user2, etc..., just write:

for (int i = 0, j = 1; i < 9 && j < 5; i++, j++){
    commentList.add(
    new CommentItem(
    R.drawable.circleicon, ("User" + i), ("Message" + i), j));
}

Finally, to your question:

ArrayList<CommentItem> resultItems = new ArrayList<CommentItem>();
for (int index = 0; index < commentList.size(); index++){
    CommentItem item = commentList.get(index);
    if (item == commentList.get(commentList.size()-1))//last index{
    resultItems.add(item);
    }
}

The most important part of that is commentList.get(commentList.size()-1)), which is an easy way to get the last element in a list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...