[BAEL-10836] - Create core-java-collections-list module
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.classcastexception;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ClassCastException {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String p1 = new String("John");
|
||||
String p2 = new String("Snow");
|
||||
String[] strArray = new String[] { p1, p2 };
|
||||
ArrayList<String> strList = (ArrayList<String>) Arrays.asList(strArray);
|
||||
// To fix the ClassCastException at above line, modify the code as:
|
||||
// List<String> strList = Arrays.asList(strArray);
|
||||
System.out.println("String list: " + strList);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.findanelement;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Customer(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id * 20;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Customer) {
|
||||
Customer otherCustomer = (Customer) obj;
|
||||
if (id == otherCustomer.id)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.findanelement;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
public class FindACustomerInGivenList {
|
||||
|
||||
public Customer findUsingGivenIndex(int indexOfCustomer, List<Customer> customers) {
|
||||
if (indexOfCustomer >= 0 && indexOfCustomer < customers.size())
|
||||
return customers.get(indexOfCustomer);
|
||||
return null;
|
||||
}
|
||||
|
||||
public int findUsingIndexOf(Customer customer, List<Customer> customers) {
|
||||
return customers.indexOf(customer);
|
||||
}
|
||||
|
||||
public boolean findUsingContains(Customer customer, List<Customer> customers) {
|
||||
return customers.contains(customer);
|
||||
}
|
||||
|
||||
public Customer findUsingIterator(String name, List<Customer> customers) {
|
||||
Iterator<Customer> iterator = customers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Customer customer = iterator.next();
|
||||
if (customer.getName().equals(name)) {
|
||||
return customer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Customer findUsingEnhancedForLoop(String name, List<Customer> customers) {
|
||||
for (Customer customer : customers) {
|
||||
if (customer.getName().equals(name)) {
|
||||
return customer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Customer findUsingStream(String name, List<Customer> customers) {
|
||||
return customers.stream()
|
||||
.filter(customer -> customer.getName().equals(name))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public Customer findUsingParallelStream(String name, List<Customer> customers) {
|
||||
return customers.parallelStream()
|
||||
.filter(customer -> customer.getName().equals(name))
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public Customer findUsingGuava(String name, List<Customer> customers) {
|
||||
return Iterables.tryFind(customers, new Predicate<Customer>() {
|
||||
public boolean apply(Customer customer) {
|
||||
return customer.getName().equals(name);
|
||||
}
|
||||
}).orNull();
|
||||
}
|
||||
|
||||
public Customer findUsingApacheCommon(String name, List<Customer> customers) {
|
||||
return IterableUtils.find(customers, new org.apache.commons.collections4.Predicate<Customer>() {
|
||||
public boolean evaluate(Customer customer) {
|
||||
return customer.getName().equals(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.java.list;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CopyListService {
|
||||
|
||||
public List<Flower> copyListByConstructor(List<Flower> source) {
|
||||
return new ArrayList<Flower>(source);
|
||||
}
|
||||
|
||||
public List<Flower> copyListByConstructorAndEditOneFlowerInTheNewList(List<Flower> source) {
|
||||
List<Flower> flowers = new ArrayList<>(source);
|
||||
if(flowers.size() > 0) {
|
||||
flowers.get(0).setPetals(flowers.get(0).getPetals() * 3);
|
||||
}
|
||||
|
||||
return flowers;
|
||||
}
|
||||
|
||||
public List<Flower> copyListByAddAllMethod(List<Flower> source) {
|
||||
List<Flower> flowers = new ArrayList<>();
|
||||
flowers.addAll(source);
|
||||
return flowers;
|
||||
}
|
||||
|
||||
public List<Flower> copyListByAddAllMethodAndEditOneFlowerInTheNewList(List<Flower> source) {
|
||||
List<Flower> flowers = new ArrayList<>();
|
||||
flowers.addAll(source);
|
||||
|
||||
if(flowers.size() > 0) {
|
||||
flowers.get(0).setPetals(flowers.get(0).getPetals() * 3);
|
||||
}
|
||||
|
||||
return flowers;
|
||||
}
|
||||
|
||||
public List<Integer> copyListByCopyMethod(List<Integer> source, List<Integer> dest) {
|
||||
Collections.copy(dest, source);
|
||||
return dest;
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStream(List<Flower> source) {
|
||||
return source.stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStreamAndSkipFirstElement(List<Flower> source) {
|
||||
return source.stream().skip(1).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStreamWithFilter(List<Flower> source, Integer moreThanPetals) {
|
||||
return source.stream().filter(f -> f.getPetals() > moreThanPetals).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStreamWithOptional(List<Flower> source) {
|
||||
return Optional.ofNullable(source)
|
||||
.map(List::stream)
|
||||
.orElseGet(Stream::empty)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStreamWithOptionalAndSkip(List<Flower> source) {
|
||||
return Optional.ofNullable(source)
|
||||
.map(List::stream)
|
||||
.orElseGet(Stream::empty)
|
||||
.skip(1)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package com.baeldung.java.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
public class CustomList<E> implements List<E> {
|
||||
private Object[] internal = {};
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
// the first cycle
|
||||
// return true;
|
||||
|
||||
// the second cycle
|
||||
// if (internal.length != 0) {
|
||||
// return false;
|
||||
// } else {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// refactoring
|
||||
return internal.length == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
// the first cycle
|
||||
// if (isEmpty()) {
|
||||
// return 0;
|
||||
// } else {
|
||||
// return internal.length;
|
||||
// }
|
||||
|
||||
// refactoring
|
||||
return internal.length;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public E get(int index) {
|
||||
// the first cycle
|
||||
// return (E) internal[0];
|
||||
|
||||
// improvement
|
||||
return (E) internal[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E element) {
|
||||
// the first cycle
|
||||
// internal = new Object[] { element };
|
||||
// return true;
|
||||
|
||||
// the second cycle
|
||||
Object[] temp = Arrays.copyOf(internal, internal.length + 1);
|
||||
temp[internal.length] = element;
|
||||
internal = temp;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends E> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object object) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object object) {
|
||||
for (Object element : internal) {
|
||||
if (object.equals(element)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> collection) {
|
||||
for (Object element : collection)
|
||||
if (!contains(element)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
E oldElement = (E) internal[index];
|
||||
internal[index] = element;
|
||||
return oldElement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
internal = new Object[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object object) {
|
||||
for (int i = 0; i < internal.length; i++) {
|
||||
if (object.equals(internal[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object object) {
|
||||
for (int i = internal.length - 1; i >= 0; i--) {
|
||||
if (object.equals(internal[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<E> subList(int fromIndex, int toIndex) {
|
||||
Object[] temp = new Object[toIndex - fromIndex];
|
||||
System.arraycopy(internal, fromIndex, temp, 0, temp.length);
|
||||
return (List<E>) Arrays.asList(temp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return Arrays.copyOf(internal, internal.length);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T[] toArray(T[] array) {
|
||||
if (array.length < internal.length) {
|
||||
return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
|
||||
}
|
||||
|
||||
System.arraycopy(internal, 0, array, 0, internal.length);
|
||||
if (array.length > internal.length) {
|
||||
array[internal.length] = null;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new CustomIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator(int index) {
|
||||
// ignored for brevity
|
||||
return null;
|
||||
}
|
||||
|
||||
private class CustomIterator implements Iterator<E> {
|
||||
int index;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return index != internal.length;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public E next() {
|
||||
E element = (E) CustomList.this.internal[index];
|
||||
index++;
|
||||
return element;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.java.list;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
private int petals;
|
||||
|
||||
public Flower(String name, int petals) {
|
||||
this.name = name;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(int petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.java.list;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import org.apache.commons.collections4.iterators.ReverseListIterator;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
/**
|
||||
* Provides methods for iterating backward over a list.
|
||||
*/
|
||||
public class ReverseIterator {
|
||||
|
||||
/**
|
||||
* Iterate using the for loop.
|
||||
*
|
||||
* @param list the list
|
||||
*/
|
||||
public void iterateUsingForLoop(final List<String> list) {
|
||||
|
||||
for (int i = list.size(); i-- > 0; ) {
|
||||
System.out.println(list.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate using the Java {@link ListIterator}.
|
||||
*
|
||||
* @param list the list
|
||||
*/
|
||||
public void iterateUsingListIterator(final List<String> list) {
|
||||
|
||||
final ListIterator listIterator = list.listIterator(list.size());
|
||||
while (listIterator.hasPrevious()) {
|
||||
System.out.println(listIterator.previous());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate using Java {@link Collections} API.
|
||||
*
|
||||
* @param list the list
|
||||
*/
|
||||
public void iterateUsingCollections(final List<String> list) {
|
||||
|
||||
Collections.reverse(list);
|
||||
for (final String item : list) {
|
||||
System.out.println(item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate using Apache Commons {@link ReverseListIterator}.
|
||||
*
|
||||
* @param list the list
|
||||
*/
|
||||
public void iterateUsingApacheReverseListIterator(final List<String> list) {
|
||||
|
||||
final ReverseListIterator listIterator = new ReverseListIterator(list);
|
||||
while (listIterator.hasNext()) {
|
||||
System.out.println(listIterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate using Guava {@link Lists} API.
|
||||
*
|
||||
* @param list the list
|
||||
*/
|
||||
public void iterateUsingGuava(final List<String> list) {
|
||||
|
||||
final List<String> reversedList = Lists.reverse(list);
|
||||
for (final String item : reversedList) {
|
||||
System.out.println(item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.java.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
/**
|
||||
* Demonstrates the different ways to loop over
|
||||
* the elements of a list.
|
||||
*/
|
||||
public class WaysToIterate {
|
||||
|
||||
List<String> countries = Arrays.asList("Germany", "Panama", "Australia");
|
||||
|
||||
/**
|
||||
* Iterate over a list using a basic for loop
|
||||
*/
|
||||
public void iterateWithForLoop() {
|
||||
for (int i = 0; i < countries.size(); i++) {
|
||||
System.out.println(countries.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using the enhanced for loop
|
||||
*/
|
||||
public void iterateWithEnhancedForLoop() {
|
||||
for (String country : countries) {
|
||||
System.out.println(country);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using an Iterator
|
||||
*/
|
||||
public void iterateWithIterator() {
|
||||
Iterator<String> countriesIterator = countries.iterator();
|
||||
while(countriesIterator.hasNext()) {
|
||||
System.out.println(countriesIterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using a ListIterator
|
||||
*/
|
||||
public void iterateWithListIterator() {
|
||||
ListIterator<String> listIterator = countries.listIterator();
|
||||
while(listIterator.hasNext()) {
|
||||
System.out.println(listIterator.next());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using the Iterable.forEach() method
|
||||
*/
|
||||
public void iterateWithForEach() {
|
||||
countries.forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over a list using the Stream.forEach() method
|
||||
*/
|
||||
public void iterateWithStreamForEach() {
|
||||
countries.stream().forEach((c) -> System.out.println(c));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.java_8_features;
|
||||
|
||||
public class Car {
|
||||
|
||||
private String model;
|
||||
private int topSpeed;
|
||||
|
||||
public Car(String model, int topSpeed) {
|
||||
super();
|
||||
this.model = model;
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public int getTopSpeed() {
|
||||
return topSpeed;
|
||||
}
|
||||
|
||||
public void setTopSpeed(int topSpeed) {
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.java_8_features;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private Integer age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.list.listoflist;
|
||||
|
||||
public class Pen implements Stationery {
|
||||
|
||||
public String name;
|
||||
|
||||
public Pen(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.list.listoflist;
|
||||
|
||||
public class Pencil implements Stationery{
|
||||
|
||||
public String name;
|
||||
|
||||
public Pencil(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.list.listoflist;
|
||||
|
||||
public class Rubber implements Stationery {
|
||||
|
||||
public String name;
|
||||
|
||||
public Rubber(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.list.listoflist;
|
||||
|
||||
public interface Stationery {
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.list.multidimensional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ArrayListOfArrayList {
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
int vertex = 5;
|
||||
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(vertex);
|
||||
|
||||
//Initializing each element of ArrayList with ArrayList
|
||||
for(int i=0; i< vertex; i++) {
|
||||
graph.add(new ArrayList<Integer>());
|
||||
}
|
||||
|
||||
//We can add any number of columns to each row
|
||||
graph.get(0).add(1);
|
||||
graph.get(0).add(5);
|
||||
graph.get(1).add(0);
|
||||
graph.get(1).add(2);
|
||||
graph.get(2).add(1);
|
||||
graph.get(2).add(3);
|
||||
graph.get(3).add(2);
|
||||
graph.get(3).add(4);
|
||||
graph.get(4).add(3);
|
||||
graph.get(4).add(5);
|
||||
|
||||
//Printing all the edges
|
||||
for(int i=0; i<vertex; i++) {
|
||||
for(int j=0; j<graph.get(i).size(); j++) {
|
||||
System.out.println("Edge between vertex "+i+"and "+graph.get(i).get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.list.multidimensional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ThreeDimensionalArrayList {
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
int x_axis_length = 2;
|
||||
int y_axis_length = 2;
|
||||
int z_axis_length = 2;
|
||||
ArrayList< ArrayList< ArrayList<String> > > space = new ArrayList<>(x_axis_length);
|
||||
|
||||
//Initializing each element of ArrayList with ArrayList< ArrayList<String> >
|
||||
for(int i=0; i< x_axis_length; i++) {
|
||||
space.add(new ArrayList< ArrayList<String> >(y_axis_length));
|
||||
for(int j =0; j< y_axis_length; j++) {
|
||||
space.get(i).add(new ArrayList<String>(z_axis_length));
|
||||
}
|
||||
}
|
||||
|
||||
//Set Red color for points (0,0,0) and (0,0,1)
|
||||
space.get(0).get(0).add("Red");
|
||||
space.get(0).get(0).add("Red");
|
||||
//Set Blue color for points (0,1,0) and (0,1,1)
|
||||
space.get(0).get(1).add("Blue");
|
||||
space.get(0).get(1).add("Blue");
|
||||
//Set Green color for points (1,0,0) and (1,0,1)
|
||||
space.get(1).get(0).add("Green");
|
||||
space.get(1).get(0).add("Green");
|
||||
//Set Yellow color for points (1,1,0) and (1,1,1)
|
||||
space.get(1).get(1).add("Yellow");
|
||||
space.get(1).get(1).add("Yellow");
|
||||
|
||||
//Printing colors for all the points
|
||||
for(int i=0; i<x_axis_length; i++) {
|
||||
for(int j=0; j<y_axis_length; j++) {
|
||||
for(int k=0; k<z_axis_length; k++) {
|
||||
System.out.println("Color of point ("+i+","+j+","+k+") is :"+space.get(i).get(j).get(k));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.baeldung.list.removeall;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RemoveAll {
|
||||
|
||||
static void removeWithWhileLoopPrimitiveElement(List<Integer> list, int element) {
|
||||
while (list.contains(element)) {
|
||||
list.remove(element);
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithWhileLoopNonPrimitiveElement(List<Integer> list, Integer element) {
|
||||
while (list.contains(element)) {
|
||||
list.remove(element);
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithWhileLoopStoringFirstOccurrenceIndex(List<Integer> list, Integer element) {
|
||||
int index;
|
||||
while ((index = list.indexOf(element)) >= 0) {
|
||||
list.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithCallingRemoveUntilModifies(List<Integer> list, Integer element) {
|
||||
while (list.remove(element))
|
||||
;
|
||||
}
|
||||
|
||||
static void removeWithStandardForLoopUsingIndex(List<Integer> list, int element) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (Objects.equals(element, list.get(i))) {
|
||||
list.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithForLoopDecrementOnRemove(List<Integer> list, int element) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (Objects.equals(element, list.get(i))) {
|
||||
list.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithForLoopIncrementIfRemains(List<Integer> list, int element) {
|
||||
for (int i = 0; i < list.size();) {
|
||||
if (Objects.equals(element, list.get(i))) {
|
||||
list.remove(i);
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithForEachLoop(List<Integer> list, int element) {
|
||||
for (Integer number : list) {
|
||||
if (Objects.equals(number, element)) {
|
||||
list.remove(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void removeWithIterator(List<Integer> list, int element) {
|
||||
for (Iterator<Integer> i = list.iterator(); i.hasNext();) {
|
||||
Integer number = i.next();
|
||||
if (Objects.equals(number, element)) {
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static List<Integer> removeWithCollectingAndReturningRemainingElements(List<Integer> list, int element) {
|
||||
List<Integer> remainingElements = new ArrayList<>();
|
||||
for (Integer number : list) {
|
||||
if (!Objects.equals(number, element)) {
|
||||
remainingElements.add(number);
|
||||
}
|
||||
}
|
||||
return remainingElements;
|
||||
}
|
||||
|
||||
static void removeWithCollectingRemainingElementsAndAddingToOriginalList(List<Integer> list, int element) {
|
||||
List<Integer> remainingElements = new ArrayList<>();
|
||||
for (Integer number : list) {
|
||||
if (!Objects.equals(number, element)) {
|
||||
remainingElements.add(number);
|
||||
}
|
||||
}
|
||||
|
||||
list.clear();
|
||||
list.addAll(remainingElements);
|
||||
}
|
||||
|
||||
static List<Integer> removeWithStreamFilter(List<Integer> list, Integer element) {
|
||||
return list.stream()
|
||||
.filter(e -> !Objects.equals(e, element))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
static void removeWithRemoveIf(List<Integer> list, Integer element) {
|
||||
list.removeIf(n -> Objects.equals(n, element));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user