I created a JSF web application with Java and use collection objects to represent One To Many relationships. Now I want to display this relationship by displaying the collection. Collections are shown in the style [item1, item2, item3]
in here. But I want them to be listed in the form item1, item2, item3
without the brackets. I am not allowed to change the toString method of AbstractCollection. Is it possible to override the toString method of AbstractCollection in another class? or is there another way to display the collection in the form I want?
Asked
Active
Viewed 242 times
0
-
1No, it's not possible. But it's possible to write your own method that takes a collection as argument, and transforms it to a string the way you want to. Not sure what this has to do with JSF or NetBeans. – JB Nizet Jun 24 '17 at 10:22
2 Answers
1
You can override the toString() method on all your AbstractCollection SubClasses.
Say you have a Class MyCollection<T> extends AbstractCollection<T>
you can write:
@Override
public String toString() {
return this.stream().map(o -> o.toString()).collect(Collectors.joining(","));
}
If you prefer you can have an util method to print all your collections, something like this:
public <T> String printAbstractCollections(AbstractCollection<T> c) {
return c.stream().map(o -> o.toString()).collect(Collectors.joining(","));
}

Joao
- 86
- 5
1
You need to create decorator for your collection, if you want not standard toString()
simply put your collection to PrettyPrintCollection
class
public final class PrettyPrintCollection<E> implements Collection<E> {
private final Collection<E> collection;
public PrettyPrintCollection(Collection<E> collection) {
this.collection = collection;
}
@Override
public int size() {
return collection.size();
}
@Override
public boolean isEmpty() {
return collection.isEmpty();
}
@Override
public boolean contains(Object o) {
return collection.contains(o);
}
@Override
public Iterator<E> iterator() {
return collection.iterator();
}
@Override
public Object[] toArray() {
return collection.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return collection.toArray(a);
}
@Override
public boolean add(E e) {
return collection.add(e);
}
@Override
public boolean remove(Object o) {
return collection.remove(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return collection.contains(c);
}
@Override
public boolean addAll(Collection<? extends E> c) {
return collection.addAll(c);
}
@Override
public boolean removeAll(Collection<?> c) {
return collection.removeAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return collection.retainAll(c);
}
@Override
public void clear() {
collection.clear();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Iterator<E> elements = collection.iterator();
while (elements.hasNext()) {
builder.append(elements.next());
if (elements.hasNext()) {
builder.append(", ");
}
}
return builder.toString();
}
}
and test for it
public class Main {
public static void main(String[] args) {
Collection<String> collection = Arrays.asList("a", "b", "c");
System.out.println(collection);
System.out.println(new PrettyPrintCollection<>(collection));
}
}
output will be:
[a, b, c]
a, b, c

fxrbfg
- 1,756
- 1
- 11
- 17