[] - Moved artices / codes from from the core-java related modules
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.hashtable;
|
||||
|
||||
public class Word {
|
||||
private String name;
|
||||
|
||||
public Word(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof Word))
|
||||
return false;
|
||||
|
||||
Word word = (Word) o;
|
||||
return word.getName().equals(this.name) ? true : false;
|
||||
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
package com.baeldung.iterators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Source code https://github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Santosh Thakur
|
||||
*/
|
||||
|
||||
public class Iterators {
|
||||
|
||||
public static int failFast1() {
|
||||
ArrayList<Integer> numbers = new ArrayList<>();
|
||||
|
||||
numbers.add(10);
|
||||
numbers.add(20);
|
||||
numbers.add(30);
|
||||
numbers.add(40);
|
||||
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Integer number = iterator.next();
|
||||
numbers.add(50);
|
||||
}
|
||||
|
||||
return numbers.size();
|
||||
}
|
||||
|
||||
public static int failFast2() {
|
||||
ArrayList<Integer> numbers = new ArrayList<>();
|
||||
numbers.add(10);
|
||||
numbers.add(20);
|
||||
numbers.add(30);
|
||||
numbers.add(40);
|
||||
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
if (iterator.next() == 30) {
|
||||
// will not throw Exception
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("using iterator's remove method = " + numbers);
|
||||
|
||||
iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
if (iterator.next() == 40) {
|
||||
// will throw Exception on
|
||||
// next call of next() method
|
||||
numbers.remove(2);
|
||||
}
|
||||
}
|
||||
|
||||
return numbers.size();
|
||||
}
|
||||
|
||||
public static int failSafe1() {
|
||||
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
|
||||
|
||||
map.put("First", 10);
|
||||
map.put("Second", 20);
|
||||
map.put("Third", 30);
|
||||
map.put("Fourth", 40);
|
||||
|
||||
Iterator<String> iterator = map.keySet()
|
||||
.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
String key = iterator.next();
|
||||
map.put("Fifth", 50);
|
||||
}
|
||||
|
||||
return map.size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package com.baeldung.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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user