Return inverse generic list type in Java

I am doing some universal programming exercises; is there any way to take a class that implements List and returns the reverse version of the same class? This seems to be feasible, since at least taking the term at face value is "typical programming" in large writing.

Perhaps completing a U-turn on the spot? I also looked into Collections.reverse (), but this is the void method.

Here is my attempt and demonstration:

import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Arrays; public class ReverseDemo { public static <T> List<T> reverse(List<T> list) { List<T> reversed = new ArrayList<T>(); for (int i = list.size() - 1; i >= 0; i--) { reversed.add(list.get(i)); } return reversed; } public static void main(String[] args) { LinkedList<Integer> linkedInt = new LinkedList<Integer>(); ArrayList<Double> arrayDouble = new ArrayList<Double>(); for (int k = 0; k < 10; k++) { double doubleNum = 10*Math.random(); int intNum = (int) (10*Math.random()); linkedInt.add(intNum); arrayDouble.add(doubleNum); } // LinkedList<Integer> demo System.out.println(Arrays.toString(linkedInt.toArray())); System.out.println(Arrays.toString(reverse(linkedInt).toArray())); System.out.println(reverse(linkedInt) instanceof LinkedList<?>); // false // ArrayList<Double> demo System.out.println(Arrays.toString(arrayDouble.toArray())); System.out.println(Arrays.toString(reverse(arrayDouble).toArray())); System.out.println(reverse(arrayDouble) instanceof ArrayList<?>); // true } } 

By the way, this is my first post here, does anyone know how best to send code directly from Eclipse, while preserving the gap and indentation? I used the four space method specified here, but it is a bit incompatible.

+4
source share
7 answers

All java.util implementations of List are clonable, so you can use this, but unfortunately without resorting to reflection. In the reflection chapter, you can also use the copy constructor, which can also be used in all Java collections.

Unfortunately, there is no complete general approach to non-destructive handling.

The destructive converse, on the other hand, is too trivial to be interesting.

+1
source

If you want to keep the original list, you can try:

 originalList.getClass().newInstance() 

This is not a 100% correct solution, which can cause it if the source class does not have a default constructor. However, most collections have default constructors that create empty instances.

+2
source

The Guava library has a good, non-destructive solution to the problem. See Lists.reverse (List) .

They define a set of ReverseList classes that complete the List input. From there, it's just a matter of transferring all the calls (although "just" can underestimate some things a little).

+2
source

try to execute

 public static <T> List<T> reverse(List<T> list) { List<T> reversed=null; try { reversed = list.getClass().newInstance(); Collections.reverse(list); reversed.addAll(list); } catch (InstantiationException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); }; return reversed; } 
+2
source

Collections.reverse() may not be valid, but this is only because you have to go through the list you want to change.

 List<T> myList = ...; Collections.reverse(myList); 

You now have a reverse list.

+1
source

It works:

 import java.util.*; public class ReverseListDemo { public static void main(String[] args) { List<String> original = Arrays.asList("A", "B", "C"); List<String> reversal = reverse(original); System.out.println("Original: " + original); System.out.println("Reversal: " + reversal); } public static <T> List<T> reverse(List<T> list) { T[] objects = (T[]) list.toArray(); List<T> copy = Arrays.asList(objects); Collections.reverse(copy); return copy; } } 
0
source

Thanks for all the answers. I wrote a reverse2 method that works even for implementing the ArrayList and LinkedList classes. Not sure how effective it is.

 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Arrays; // See http://stackoverflow.com/questions/16799066/return-reversed-generic-list-type-in-java. public class ReverseDemo { public static <T> List<T> reverse1(List<T> list) { List<T> reversed = new ArrayList<T>(); for (int i = list.size() - 1; i > -1; i--) { reversed.add(list.get(i)); } return reversed; } public static <T> List<T> reverse2(List<T> list) { int size = list.size(); ArrayList<T> obArray = new ArrayList<T>(); obArray.addAll(list); ListIterator<T> iter = list.listIterator(); for (int i = 0; i < size; i++) { iter.next(); iter.set(obArray.get(size - 1 - i)); } return list; } public static void main(String[] args) { LinkedList<Integer> linkedInt = new LinkedList<Integer>(); ArrayList<Double> arrayDouble = new ArrayList<Double>(); for (int k = 0; k < 10; k++) { double doubleNum = 10*Math.random(); int intNum = (int) (10*Math.random()); linkedInt.add(intNum); arrayDouble.add(doubleNum); } TextIO.putln("Testing reverse1."); // LinkedList<Integer> demo System.out.println(Arrays.toString(linkedInt.toArray())); System.out.println(Arrays.toString(reverse1(linkedInt).toArray())); TextIO.putln("LinkedList structure preserved?"); System.out.println(reverse1(linkedInt) instanceof LinkedList<?>); // ArrayList<Double> demo System.out.println(Arrays.toString(arrayDouble.toArray())); System.out.println(Arrays.toString(reverse1(arrayDouble).toArray())); TextIO.putln("ArrayList structure preserved?"); System.out.println(reverse1(arrayDouble) instanceof ArrayList<?>); TextIO.putln("\nTesting reverse2."); // LinkedList<Integer> demo System.out.println(Arrays.toString(linkedInt.toArray())); System.out.println(Arrays.toString(reverse2(linkedInt).toArray())); TextIO.putln("LinkedList structure preserved?"); System.out.println(reverse2(linkedInt) instanceof LinkedList<?>); // ArrayList<Double> demo System.out.println(Arrays.toString(arrayDouble.toArray())); System.out.println(Arrays.toString(reverse2(arrayDouble).toArray())); TextIO.putln("ArrayList structure preserved?"); System.out.println(reverse2(arrayDouble) instanceof ArrayList<?>); } } 

console output:

Testing reverse1. [8, 0, 1, 9, 3, 4, 3, 7, 6, 3] [3, 6, 7, 3, 4, 3, 9, 1, 0, 8] Is the LinkedList structure saved? false [8.301783107294664, 5.434068303620735, 9.095396759542615, .41823972682620836, 9.56659902304762, 3.2560723280079085, 4.037362000077436, 9.731919590391389, 0.5243645318825874, 5.9432185528462975] [5.9432185528462975, 0.5243645318825874, 9.731919590391389, 4.037362000077436, 3.2560723280079085, 9.56659902304762, 0.41823972682620836, 9.095396759542615, 5.434068303620735, 8.301783107294664] Retained ArrayList structure ? true

Testing reverse2. [8, 0, 1, 9, 3, 4, 3, 7, 6, 3] [3, 6, 7, 3, 4, 3, 9, 1, 0, 8] Is the LinkedList structure saved? true [8.301783107294664, 5.434068303620735, 9.095396759542615, .41823972682620836, 9.56659902304762, 3.2560723280079085, 4.037362000077436, 9.731919590391389, 0.5243645318825874, 5.9432185528462975] [5.9432185528462975, 0.5243645318825874, 9.731919590391389, 4.037362000077436, 3.2560723280079085, 9.56659902304762, 0.41823972682620836, 9.095396759542615, 5.434068303620735, 8.301783107294664] Retained ArrayList structure ? true

0
source

Source: https://habr.com/ru/post/1483209/


All Articles