Printing out items in any Collection in reverse order?

I have the following problem in my Data Structures and Problem Solving using Java book:

Write a routine that uses the Collections API to print out the items in any Collection in reverse order. Do not use a ListIterator.

I'm not putting it up here because I want somebody to do my homework, I just can't seem to understand exactly what it is asking for me to code!

When it asks me to write a 'routine', is it looking for a single method? I don't really understand how I can make a single method work for all of the various types of Collections (linked list, queue, stack).

If anybody could guide me in the right direction, I would greatly appreciate it.


Asked by: Brad863 | Posted: 28-01-2022






Answer 1

Regardless from the question not making much sense as half of the collections have no gstable ordering of have fixed-ordering (i.e. TreeSet or PriorityQueue), you can use the following statement for printing the contents of a collection in reverse-natural order:

List temp = new ArrayList(src);
Collections.reverse(temp);
System.out.println(temp);

I essence you create an array list as lists are the only structure that can be arbitrarily reordered. You pass the src collection to the constructor which initializes the list withj the contents of the src in the collection natural order. Then you pass the list to the Collections.reverse() method which reverses the list and finally you print it.

Answered by: Brad621 | Posted: 01-03-2022



Answer 2

First, I believe it is asking you to write a method. Like:

void printReverseList(Collection col) {}

Then there are many ways to do this. For example, only using the Collection API, use the toArray method and use a for loop to print out all the items from the end. Make sense?

As for the various classes using the Collection interface, it will automatically work for all of those since they must implement the interface (provided they implement it in a sane way;).

Answered by: First Name906 | Posted: 01-03-2022



Answer 3

Well you could have a routine that delegates to other routines based on the input type, however I'm not sure there is a generic enough collection type that can be encompassed into one argument. I guess you could just use method overloading (having multiple methods with the same name, but accept different args).

That could technically count as 1 routine (all have the same name).

Answered by: Elian588 | Posted: 01-03-2022



Answer 4

I don't know much Java, but considering the "Collections API" i imagine all those objects implement an interface you could iterate through someway. i suppose they all could have an itemAtIndex( int index ) and length() or similar method you could use.

You might want to read this.

Answered by: Victoria859 | Posted: 01-03-2022



Answer 5

Isn't there a base Collection class?

Probably worth looking here as a starting point: Collections.

Answered by: Ryan723 | Posted: 01-03-2022



Similar questions

java - Printing Collection of Strings on Different Pages using iReport

I am trying to print a Collection parameter on different pages using iReport by passing a String array into iReport from Java. However, when I tried to print the Collection parameter, the report reflected [String1, String2, String3] after I generated the report. I would like the report to print String 1 on Page 1, String 2 on Page 2 and so on. Is there anyway to do this?


java - Illegal attempt to associate a collection with two open sessions

I'm trying to add a pojo to a collection in another pojo. I'm sure I'm making a really stupid mistake somewhere along the lines but I can't figure out how to solve it. I have a pojo LookupTable which contains a list of Columns: public class LookupTable { private long id; // More properties go here... private List<Column> columns; public void addColumn(Column column) { this.columns...


garbage collection - Java Concurrent and Parallel GC

This article here suggests to use -XX:+UseParNewGC "To enable a parallel young generation GC with the concurrent GC". My confusion is that in order to enable both parallel and concurrent GC, should I use -XX:+UseParNewGC or use both -XX:+UseParNewGC and -XX:+Use...


java - How do I bind collection attributes to a form in Spring MVC

I'm trying to bind one of my model objects to the fields of a form, using Spring-MVC. Everything works fine, except that one of the attributes of the model object is an unordered collection. Doing something like <c:forEach items="${m.items}" var="i" varStatus="itemsRow"> <form:input path="items[${itemsRow.index}]"/> </c:forEach> <form:errors path="items" />


java - Best Collection To Use?


Does using final for variables in Java improve garbage collection?

Today my colleagues and me have a discussion about the usage of the final keyword in Java to improve the garbage collection. For example, if you write a method like: public Double doCalc(final Double value) { final Double maxWeight = 1000.0; final Double totalWeight = maxWeight * value; return totalWeight; } Declaring the variables in the method final


generics - Java: How to create a collection of a specific parent type and not its subtypes?

I am learning Java for a test (tomorrow) and am wondering about a question that will probably never come up but has got me curious. Is it possible to create a new collection class such as a list or something that can hold only a specific type and not its sub-types? Would I use generics to achieve this goal?


java - Can Hibernate return a collection of result objects OTHER than a List?

Does the Hibernate API support object result sets in the form of a collection other than a List? For example, I have process that runs hundreds of thousands of iterations in order to create some data for a client. This process uses records from a Value table (for example) in order to create this output for each iteration. With a List I would have to iterate through the entire list in order to find a certa...


java - JPA map collection of Enums

Is there a way in JPA to map a collection of Enums within the Entity class? Or the only solution is to wrap Enum with another domain class and use it to map the collection? @Entity public class Person { public enum InterestsEnum {Books, Sport, etc... } //@??? Collection<InterestsEnum> interests; } I am using Hibernate JPA implementation, but of course would prefer implem...


c# - garbage collection Operation

Can someone please explain me how garbage collection is working? (I'm using C# and Java).


Java: Trouble with Generics & Collection type detection

I have a class called DataSet with various constructors, each specifying a different type of variable. It might look a bit like this: public class DataSet { private HashSet Data; public DataSet( DataObject obj ) { Data = new <DataObject>HashSet(); Data.add( obj ); } public DataSet( ObjectRelationship rel ) { Data = new <ObjectRelationship>HashS...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top