I want to write a function that checks a 2-d List for duplicate elements. For example: [[key1, key2], [key1, key3]] "key1" is duplicate. I want to remove or replace it e.g with n\a. What I have tried so far.
private static List<List<String>> removeRedundantStrings(List<List<String>> list) {
List<List<String>> outList = new ArrayList<>();
List<String> oneDimension = new ArrayList<>();
for (var tempList : list) {
for (var string : tempList) {
if (!oneDimension.contains(string))
oneDimension.add(string);
else
oneDimension.add("n/a");
outList.add(oneDimension);
}
}
return outList;
}
This "solution" results in: [[key1, key2, n/a, key3]] - not a "true" 2-d list. How would I achieve the same format as the input list? The result would be: [[key1, key2], [n/a, key3]]. Am I missing something else?
You should create the inner List in the correct place - inside the outer loop.
In addition, I'd use a single HashSet to keep track of the Strings that already appeared.
List<List<String>> outList = new ArrayList<>();
Set<String> uniques = new HashSet<>();
for (var tempList : list) {
List<String> oneDimension = new ArrayList<>();
for (var string : tempList) {
if (uniques.add(string)) // add will return true if string was actually added to Set
oneDimension.add(string);
else
oneDimension.add("n/a");
}
}
outList.add(oneDimension);
}
My excercise is:
Create the method lengths that gets a list of String variables as a parameter and returns an ArrayList that contains the lengths of the Strings in the same order as the original list.
And my code:
import java.util.ArrayList;
public class Test {
public static ArrayList<Integer> lengths(ArrayList<String> strings) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (String item : strings) {
System.out.print(item.length());
}
return list;
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("Dog");
list.add("Elephant");
list.add("Aligator");
list.add("Parrot");
ArrayList<Integer> stringLenghts = lengths(list);
System.out.println(stringLenghts);
}
}
And this program outputs
3386[]
Instead of
[3, 3, 8, 6]
Any idea where I do a mistake?
In your lengths() method, you are simply printing the length values.
But you are NOT adding the lengths to the ArrayList i.e., your stringLenghts list is empty (so printing as [] empty array), so change your code as shown below:
public static ArrayList<Integer> lengths(ArrayList<String> strings) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (String item : strings) {
list.add(item.length());//add length to list
}
return list;
}
Try this.
import java.util.ArrayList;
public class Test {
public static ArrayList<Integer> lengths(ArrayList<String> strings) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (String item : strings) {
list.add(item.length());
}
return list;
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("Dog");
list.add("Elephant");
list.add("Aligator");
list.add("Parrot");
ArrayList<Integer> stringLenghts = lengths(list);
System.out.println(stringLenghts);
}
}
EDIT: In the lengths method, the list is being created but you are not adding any items to it. Now in the code abode for (String item : strings) { list.add(item.length()); } This adds the length of each string to the new ArrayList which you will return to the main method for printing.
Thanks
You need correct this body of cycle and add here the numbers
list.add(item.length());
between brackets.
Initially you are printing the length of each string from the loop
for(String item : strings) {
System.out.print(item.length());
}
which outputs : 3386 (no new line because of print)
After this empty ArrayList is printed because you are returning the empty arrayList object.
System.out.println(stringLenghts);
which outputs : [ ]
Solution :
You have to replace System.out.print(item.length());
with
list.add(item.length());
i have arraylists named sub and main,
ArrayList main = new ArrayList();
ArrayList sub=new ArrayList();
i add value to sub and then add sub to main.
example;
sub.add(1);
sub.add(2);
main.add(sub);
now i want to get all values inside sub
so i used following one but .get(j) gives me the error get >> canot find symbol
for (int i=0;i<main.size();i++) {
System.out.println();
for (int j=0;j<sub().size();j++) {
System.out.print(main.get(i).get(j));//error line
}
}
how can i get all values inside subarray of main arraylist
When you declare a variable as
ArrayList main;
This list holds Objects. This means that main.get(i) will only return an Object, even if you add ArrayLists. That's why you get a compiler error: Object doesn't have a method named get().
To fix the problem, you need to use generics:
ArrayList<List<Integer>> main = new ArrayList<>();
ArrayList<Integer> sub=new ArrayList<>();
Now get() will return a List<Integer> which has a get() method, so the compiler error will disappear.
Generics could be your friend here:
ArrayList<ArrayList<Object>> main = new ArrayList<ArrayList<Object>>(); // or new ArrayList<>(); in Java 7+
ArrayList<Object> sub = new ArrayList<Object>(); // or new ArrayList<>();
If you can't or don't want to use generics, the solution is to cast the expression main.get(i) to an ArrayList first:
System.out.println(((ArrayList) main.get(i)).get(j));
Go through the following code
public class ArrayListDemo {
public static void main(String[] args) {
List<List<Integer>> main = new ArrayList<>();
List<Integer> sub = new ArrayList<>();
sub.add(1);
sub.add(2);
main.add(sub);
//If you want to get values in sub array list
for(int i = 0; i < 1; i++){
List<Integer> arr = main.get(i);
for(Integer val : arr) System.out.println(val + "");
}
//If you want to print all values
for(List<Integer> list : main){
for(Integer val : list) System.out.println(val + "");
}
}
}
In the above code, I had declared an ArrayList (main) to keep all Array which are having Integer values. Also i had declared an another ArrayList (sub) to keep all Integer values.
I had used ArrayList data structure because of length of the List will be changing the
run time.
Good Luck !!!
at my work I've got the following source code:
import java.util.ArrayList;
import java.util.List;
public class Temporaer
{
public static void main(String[] args)
{
List stringArrayList = new java.util.ArrayList();
stringArrayList.add(fillStringArrayElement("a", "b"));
stringArrayList.add(fillStringArrayElement("c", "d"));
String[] listElement;
/*
* I'm stuck here, because I don't know what I have to do
*/
System.out.println(listElement.length);
}
//Just a method to fill a list easily
private static String[] fillStringArrayElement (String firstElem, String secondElem)
{
String[] stringArrayListElement = new String[2];
stringArrayListElement[0] = firstElem;
stringArrayListElement[1] = secondElem;
return stringArrayListElement;
}
}
My goal is it to extract each list item and work with those.
I tried to use the toArray[T[]) method as mentioned here. Though it generates an java.lang.ArrayStoreException. Note: I cannot change the type of the list because the list is filled by an extern service. Maybe I have to convert the list first...
Can someone show me a way to achive my goal? Thanks in advanced.
Iterator is an interface in java used to iterate over a Collection like ArrayList or other Collection framework classes.
Before reading ArrayList make sure values are available using the size() method.
Here a sample working snippet for your problem.
String [] myArray ;
if (stringArrayList.size()>0){
Iterator<String [] > i = stringArrayList.iterator();
while(i.hasNext()){
myArray = i.next();
for(String s : myArray)
System.out.println(s);
}
}
}
Don't use Raw ArrayList, instead use Generics
Use this:
List<String[]> stringArrayList = new java.util.ArrayList<String[]>();
stringArrayList.add(new String[]{"a", "b"});
stringArrayList.add(new String[]{"c", "d"});
//if you cant to convert the stringArrayList to an array:
String[][] listElement = stringArrayList.toArray(new String[0][]);
for (String[] inArr : listElement){
for (String e : inArr){
System.out.print(e + " ");
}
System.out.println();
}
String[] listElement;
Above statements is incorrect because you are keeping arrays in list so your listElement must contain String[] .
String [][] listElement
Something like below.
listElement=stringArrayList.toArray(new String[stringArrayList.size()][]);
I'm migrating a piece of code to make use of generics. One argument for doing so is that the for loop is much cleaner than keeping track of indexes, or using an explicit iterator.
In about half the cases, the list (an ArrayList) is being iterated in reverse order by using an index today.
Can someone suggest a cleaner way of doing this (since I dislike the indexed for loop when working with collections), though it does work?
for (int i = nodes.size() - 1; i >= 0; i--) {
final Node each = (Node) nodes.get(i);
...
}
Note: I can't add any new dependencies outside the JDK.
Try this:
// Substitute appropriate type.
ArrayList<...> a = new ArrayList<...>();
// Add elements to list.
// Generate an iterator. Start just after the last element.
ListIterator li = a.listIterator(a.size());
// Iterate in reverse.
while(li.hasPrevious()) {
System.out.println(li.previous());
}
Guava offers Lists#reverse(List) and ImmutableList#reverse(). As in most cases for Guava, the former delegates to the latter if the argument is an ImmutableList, so you can use the former in all cases. These do not create new copies of the list but just "reversed views" of it.
Example
List reversed = ImmutableList.copyOf(myList).reverse();
I don't think it's possible using the for loop syntax. The only thing I can suggest is to do something like:
Collections.reverse(list);
for (Object o : list) {
...
}
... but I wouldn't say this is "cleaner" given that it's going to be less efficient.
Option 1: Have you thought about reversing the List with Collections#reverse() and then using foreach?
Of course, you may also want to refactor your code such that the list is ordered correctly so you don't have to reverse it, which uses extra space/time.
EDIT:
Option 2: Alternatively, could you use a Deque instead of an ArrayList? It will allow you to iterate forwards and backwards
EDIT:
Option 3: As others have suggested, you could write an Iterator that will go through the list in reverse, here is an example:
import java.util.Iterator;
import java.util.List;
public class ReverseIterator<T> implements Iterator<T>, Iterable<T> {
private final List<T> list;
private int position;
public ReverseIterator(List<T> list) {
this.list = list;
this.position = list.size() - 1;
}
#Override
public Iterator<T> iterator() {
return this;
}
#Override
public boolean hasNext() {
return position >= 0;
}
#Override
public T next() {
return list.get(position--);
}
#Override
public void remove() {
throw new UnsupportedOperationException();
}
}
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
for (String s : new ReverseIterator<String>(list)) {
System.out.println(s);
}
You could use the concrete class LinkedList instead of the general interface List. Then you have a descendingIterator for iterating with the reverse direction.
LinkedList<String > linkedList;
for( Iterator<String > it = linkedList.descendingIterator(); it.hasNext(); ) {
String text = it.next();
}
Don't know why there is no descendingIterator with ArrayList...
This is an old question, but it's lacking a java8-friendly answer. Here are some ways of reverse-iterating the list, with the help of the Streaming API:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 3, 3, 7, 5));
list.stream().forEach(System.out::println); // 1 3 3 7 5
int size = list.size();
ListIterator<Integer> it = list.listIterator(size);
Stream.generate(it::previous).limit(size)
.forEach(System.out::println); // 5 7 3 3 1
ListIterator<Integer> it2 = list.listIterator(size);
Stream.iterate(it2.previous(), i -> it2.previous()).limit(size)
.forEach(System.out::println); // 5 7 3 3 1
// If list is RandomAccess (i.e. an ArrayList)
IntStream.range(0, size).map(i -> size - i - 1).map(list::get)
.forEach(System.out::println); // 5 7 3 3 1
// If list is RandomAccess (i.e. an ArrayList), less efficient due to sorting
IntStream.range(0, size).boxed().sorted(Comparator.reverseOrder())
.map(list::get).forEach(System.out::println); // 5 7 3 3 1
Here is an (untested) implementation of a ReverseIterable. When iterator() is called it creates and returns a private ReverseIterator implementation, which simply maps calls to hasNext() to hasPrevious() and calls to next() are mapped to previous(). It means you could iterate over an ArrayList in reverse as follows:
ArrayList<String> l = ...
for (String s : new ReverseIterable(l)) {
System.err.println(s);
}
Class Definition
public class ReverseIterable<T> implements Iterable<T> {
private static class ReverseIterator<T> implements Iterator {
private final ListIterator<T> it;
public boolean hasNext() {
return it.hasPrevious();
}
public T next() {
return it.previous();
}
public void remove() {
it.remove();
}
}
private final ArrayList<T> l;
public ReverseIterable(ArrayList<T> l) {
this.l = l;
}
public Iterator<T> iterator() {
return new ReverseIterator(l.listIterator(l.size()));
}
}
If the lists are fairly small so that performance is not a real issue, one can use the reverse-metod of the Lists-class in Google Guava. Yields pretty for-each-code, and the original list stays the same. Also, the reversed list is backed by the original list, so any change to the original list will be reflected in the reversed one.
import com.google.common.collect.Lists;
[...]
final List<String> myList = Lists.newArrayList("one", "two", "three");
final List<String> myReverseList = Lists.reverse(myList);
System.out.println(myList);
System.out.println(myReverseList);
myList.add("four");
System.out.println(myList);
System.out.println(myReverseList);
Yields the following result:
[one, two, three]
[three, two, one]
[one, two, three, four]
[four, three, two, one]
Which means that reverse iteration of myList can be written as:
for (final String someString : Lists.reverse(myList)) {
//do something
}
You could use ReverseListIterator from Apache Commons-Collections:
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/iterators/ReverseListIterator.html
Very simple Example:
List<String> list = new ArrayList<String>();
list.add("ravi");
list.add("kant");
list.add("soni");
// Iterate to disply : result will be as --- ravi kant soni
for (String name : list) {
...
}
//Now call this method
Collections.reverse(list);
// iterate and print index wise : result will be as --- soni kant ravi
for (String name : list) {
...
}
To have code which looks like this:
List<Item> items;
...
for (Item item : In.reverse(items))
{
...
}
Put this code into a file called "In.java":
import java.util.*;
public enum In {;
public static final <T> Iterable<T> reverse(final List<T> list) {
return new ListReverseIterable<T>(list);
}
class ListReverseIterable<T> implements Iterable<T> {
private final List<T> mList;
public ListReverseIterable(final List<T> list) {
mList = list;
}
public Iterator<T> iterator() {
return new Iterator<T>() {
final ListIterator<T> it = mList.listIterator(mList.size());
public boolean hasNext() {
return it.hasPrevious();
}
public T next() {
return it.previous();
}
public void remove() {
it.remove();
}
};
}
}
}
Create a custom reverseIterable.
Also found google collections reverse method.
How about using DeQue:
var queue = new ArrayDeque<>(list);
while (!queue.isEmpty()) {
var first = reversed ? queue.removeLast() : queue.removeFirst();
var second = reversed ? queue.peekLast() : queue.peekFirst();
if (second != null) {
//your code goes here
}
}
As has been suggested at least twice, you can use descendingIterator with a Deque, in particular with a LinkedList. If you want to use the for-each loop (i.e., have an Iterable), you can construct and use a wraper like this:
import java.util.*;
public class Main {
public static class ReverseIterating<T> implements Iterable<T> {
private final LinkedList<T> list;
public ReverseIterating(LinkedList<T> list) {
this.list = list;
}
#Override
public Iterator<T> iterator() {
return list.descendingIterator();
}
}
public static void main(String... args) {
LinkedList<String> list = new LinkedList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
for (String s : new ReverseIterating<String>(list)) {
System.out.println(s);
}
}
}
Valid for Java 9+
List<String> strList = List.of("a", "b", "c", "d", "e");
IntStream.iterate(strList.size() - 1, i -> i >= 0, i -> --i)
.mapToObj(strList::get)
.forEach(System.out::println);
Reason : "Don't know why there is no descendingIterator with ArrayList..."
Since array list doesnot keep the list in the same order as data has been added to list. So, never use Arraylist .
Linked list will keep the data in same order of ADD to list.
So , above in my example, i used ArrayList() in order to make user to twist their mind and make them to workout something from their side.
Instead of this
List<String> list = new ArrayList<String>();
USE:
List<String> list = new LinkedList<String>();
list.add("ravi");
list.add("kant");
list.add("soni");
// Iterate to disply : result will be as --- ravi kant soni
for (String name : list) {
...
}
//Now call this method
Collections.reverse(list);
// iterate and print index wise : result will be as --- soni kant ravi
for (String name : list) {
...
}