KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an Item which has a method List<Item> getChildren() (which returns an immutable list) and for each of the items I have, I need to create a list of the item followed by its children. What's the quickest way to " cons " (in the Lisp/Scheme sense) my item to create a new immutable list? I can certainly do the following, but it seems wrong/wasteful: public List<Item> getItemAndItsChildren(Item item) { if (item.getChildren.isEmpty()) return Collections.singletonList(item); else { // would rather just "return cons(item, item.getChildren())" // than do the following -- which, although straightforward, // seems wrong/wasteful. List<Item> items = new ArrayList<Item>(); items.add(item); items.addAll(item.getChildren()); return Collections.unmodifiableList(items); } }
Tags (comma-separated)
Save Edits
Cancel