0

How would you go about maintaining an order of collection business objects

<BO1, BO2, BO3, BO4>

so that when you remove BO2, amd BO4 you get

<BO1, BO3>

and then when you add BO2

<BO1, BO2, BO3>
Colin D
  • 5,641
  • 1
  • 23
  • 35
Bober02
  • 15,034
  • 31
  • 92
  • 178
  • How is the order of the objects determined? Is it possible to know BO2 should be between 1,3 without knowing where it was before it was removed? – not all wrong Aug 10 '12 at 15:25

6 Answers6

3

Unless you use a sorted order, I don't see how the collection is supposed to know that BO2 should go in the middle.

This will do what you want if your Business object implement Comparable

SortedSet<BusObj> bos = new TreeSet<>();
bos.addAll(Arrays.asList(bo1, bo2, bo3, bo4));
bos.removeAll(Arrays.asList(bo2, bo4));
bos.add(bo2);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

You have several ways of doing that but it depends of the type of collection you want to use. Obviously, you don't want to maintain the order of insertions but an order based on the type of elements in the list.

So, before saying use this or that, ask yourself the following question:

Can my collection hold duplicate elements?

1) If YES: then you could use an implementation of a List object (ArrayList, LinkedList, etc). But you will need to sort the list after each insertion:

List<MyObj> list = ...
list.add(myObjInstance);
Collections.sort(list);

To avoid having to sort the list on each insertion you could use the TreeList implementation from Apache Commons Collections.

2) If the answer to the previous question is NO. Then use a TreeSet, you won't need sort the collection on each insertion with that implementation.

Be aware that your object elements have to implement the Comparable interface in order to be sortable.

Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
2

Make your business object Comparable and use a sorted collection (like TreeSet which is a SortedSet).

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • This assumes that BO1, BO2, BO3 can be sorted and they are not relying on the order in which they were first added to the list. If they are then havign a second list where elements are never removed would allow it to compare appropriatley. – Jon Taylor Aug 10 '12 at 15:27
  • @JonTaylor: I think the first line in Peter Lawrey's answer explains it. – Bhesh Gurung Aug 10 '12 at 15:33
  • I don't think it does. It states the same as your answer that it must be comparable. If however the future ordering of a list depends on the original order in which they were added rather than any specific comparable ordering, a comparator on its own will be useless. – Jon Taylor Aug 10 '12 at 15:36
  • I think in that case, it would be a fixed sized collection, with same objects and he would have to keep track of location of the objects manually. – Bhesh Gurung Aug 10 '12 at 15:47
1

Use a SortedSet

http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html

Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
0

There are 2 options: use a List and do the sorting yourself by inserting at the proper location or use a sorted collection.

The sorted collection I think you want is SortedSet http://docs.oracle.com/javase/6/docs/api/java/util/SortedSet.html.

The SortedSet requires entries to implement the Comparable interface.

There is also another question that you should look at: Sorted collection in Java

Community
  • 1
  • 1
Colin D
  • 5,641
  • 1
  • 23
  • 35
0

Aswering my question:

I guess also PriorityQueue would be a solution, if one were not interested in the random access.

Bober02
  • 15,034
  • 31
  • 92
  • 178