[Bael 6056] - Move collection-related logic from core-java to core-java-collections (#4114)
* Added parent module on poms that have no parent defined * Removed dependency reduced pom from undertow module * [BAEL-6056] - Move collection-related logic from core-java to core-java-collections * [BAEL-6056] - Move collection-related logic from core-java to core-java-collections * [BAEL-6056] - Move collection-related logic from core-java to core-java-collections * [BAEL-6056] - Move collection-related logic from core-java to core-java-collections
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
12cdd5357d
commit
cfd98d2fe3
@@ -1,210 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MyKey {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MyKey.class);
|
||||
|
||||
private String name;
|
||||
private int id;
|
||||
|
||||
public MyKey(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
LOG.debug("Calling hashCode()");
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MyKey [name=" + name + ", id=" + id + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
LOG.debug("Calling equals() for key: " + obj);
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
MyKey other = (MyKey) obj;
|
||||
if (id != other.id)
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MyLinkedHashMap<K, V> extends LinkedHashMap<K, V> {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final int MAX_ENTRIES = 5;
|
||||
|
||||
public MyLinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) {
|
||||
super(initialCapacity, loadFactor, accessOrder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry eldest) {
|
||||
return size() > MAX_ENTRIES;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.list.listoflist;
|
||||
|
||||
public interface Stationery {
|
||||
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package com.baeldung.map.iteration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapIteration {
|
||||
|
||||
public static void main(String[] args) {
|
||||
MapIteration mapIteration = new MapIteration();
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
|
||||
map.put("One", 1);
|
||||
map.put("Three", 3);
|
||||
map.put("Two", 2);
|
||||
|
||||
System.out.println("Iterating Keys of Map Using KeySet");
|
||||
mapIteration.iterateKeys(map);
|
||||
|
||||
System.out.println("Iterating Map Using Entry Set");
|
||||
mapIteration.iterateUsingEntrySet(map);
|
||||
|
||||
System.out.println("Iterating Using Iterator and Map Entry");
|
||||
mapIteration.iterateUsingIteratorAndEntry(map);
|
||||
|
||||
System.out.println("Iterating Using KeySet and For Each");
|
||||
mapIteration.iterateUsingKeySetAndForeach(map);
|
||||
|
||||
System.out.println("Iterating Map Using Lambda Expression");
|
||||
mapIteration.iterateUsingLambda(map);
|
||||
|
||||
System.out.println("Iterating Using Stream API");
|
||||
mapIteration.iterateUsingStreamAPI(map);
|
||||
}
|
||||
|
||||
public void iterateUsingEntrySet(Map<String, Integer> map) {
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
System.out.println(entry.getKey() + ":" + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateUsingLambda(Map<String, Integer> map) {
|
||||
map.forEach((k, v) -> System.out.println((k + ":" + v)));
|
||||
}
|
||||
|
||||
public void iterateUsingIteratorAndEntry(Map<String, Integer> map) {
|
||||
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet()
|
||||
.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, Integer> pair = iterator.next();
|
||||
System.out.println(pair.getKey() + ":" + pair.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateUsingKeySetAndForeach(Map<String, Integer> map) {
|
||||
for (String key : map.keySet()) {
|
||||
System.out.println(key + ":" + map.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateUsingStreamAPI(Map<String, Integer> map) {
|
||||
map.entrySet()
|
||||
.stream()
|
||||
.forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
|
||||
}
|
||||
|
||||
public void iterateKeys(Map<String, Integer> map) {
|
||||
for (String key : map.keySet()) {
|
||||
System.out.println(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user