Calling setter method of object in List also changes properties of other objects in List. List is value in HashMap
By : Sanco de Voogd
Date : March 29 2020, 07:55 AM
This might help you The problem is in your randomlyExpand function. You are iterating trough your list of 5 distinguished objects that you have previously created with this: code :
for(int i = 0; i < 5; i++){
list2.add(new TestObject());
}
int randomInt = ran.nextInt(values.size());
tempList.add(values.get(randomInt));
for (int i = 0; i < 10; i++){
int randomInt = ran.nextInt(values.size());
TestObject obj = new TestObject();
obj.setName(values.get(randomInt).getName());
tempList.add(obj);
}
|
C# How to create a List of objects with read only properties by passing in a list of similar objects via constructor
By : Patrick
Date : March 29 2020, 07:55 AM
hope this fix your issue My class is defined as follows. Without going too much into detail I don't want anyone to be able to alter this instantiated object, or the object that is passed in to this classes constructor. , Simply use LINQ and the current ctor: code :
List<ItemDeliveryInfo> itemDeliveryInfos =
itemDeliveryAudits.Select(i => new ItemDeliveryInfo(i, true, false)).ToList();
|
Java 8 streaming: How to convert list of objects to a list of its selected properties
By : bipopbipooop
Date : March 29 2020, 07:55 AM
this will help I have a class , How about something like this code :
stream.stream().flatMap(p -> Stream.of(p.firstName, p.lastName)).collect(Collectors.toList());
|
Java - map object properties in one list to another list of objects
By : user764611
Date : March 29 2020, 07:55 AM
it fixes the issue As I said in the comments, I'd suggest that you rethink your model a bit. First introduce a Shop class: code :
public class Shop {
private final String id; // Id of this shop.
private final ShopOwner owner; // Owner of this shop.
public Shop(String id, ShopOwner owner) {
this.id = id;
this.owner = owner;
}
public String getId() { return id; }
// Overwrite equals and hashcode to allow using Shop as a key in a HashMap (only necessary if you want to enforce uniqueness of a ShopOwner's shops by maintaining a ShopOwner's owned shops using a Map).
// We simply define equality based on id equality and rely on String.hashCode() (note that this will have to be changed if you want to add additional properties and define equality based on those):
@Override
public boolean equals(Object o) {
return o instanceof Shop && o.toString().equals(this.toString());
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public String toString() {
return id;
}
// Other properties etc.
}
public class ShopOwner {
String owner;
List<Shop> shops = new ArrayList<>(); // Shops owned by this ShopOwner.
int age;
String dateOfBirth;
public void addShop(Shop s) {
// Note that this does NOT guarantee uniqueness of shops if your DB query returns multiple instances of the same shop.
// If you want this, you could use a Map instead.
this.shops.add(s);
}
// Your other getters etc.
}
List<ShopOwnerMap> soms = ...; // the list you produced from the DB query.
List<ShopOwner> sos = ...; // your list of ShopOwners, each of which needs to be initialized with the shops they own, i.e. their initial lists of shops are empty.
// Note that the below could be reduced from O(N^2) to O(N) if you manage your ShopOwners using a Map (using an equals+hashCode scheme as suggested for Shop).
for (ShopOwnerMap som : soms) {
for (ShopOwner so : sos) {
// We assume that the 'owner' property (perhaps 'name' is more suitable?) is unique among ShopOwners.
if (som.getOwner().equals(so.getOwner()) {
// Found the corresponding shop owner; create shop instance and add to list of owned shops.
so.addShop(new Shop(som.getShopId(), so));
}
}
}
|
How to Compare a List of Objects and Their Properties, then Update the Original List in Java
By : eDube
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have an object that has a list as its property, e.g. , This works: code :
package collection.delta;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import collection.delta.model.A;
import collection.delta.model.B;
public class App {
public static void main( String[] args ) {
List<A> originalA = new ArrayList<A>();
List<A> newA = new ArrayList<A>();
List<B> bListOriginalA1 = new ArrayList<B>();
bListOriginalA1.add(new B("originalA1_B1"));
bListOriginalA1.add(new B("originalA1_B2"));
bListOriginalA1.add(new B("originalA1_B3"));
bListOriginalA1.add(new B("originalA1_B4"));
A originalA1 = new A("originalA1", bListOriginalA1);
List<B> bListOriginalA2 = new ArrayList<B>();
bListOriginalA2.add(new B("originalA2_B1"));
bListOriginalA2.add(new B("originalA2_B2"));
bListOriginalA2.add(new B("originalA2_B3"));
bListOriginalA2.add(new B("originalA2_B4"));
A originalA2 = new A("originalA2", bListOriginalA2);
List<B> bListOriginalA3 = new ArrayList<B>();
bListOriginalA3.add(new B("originalA3_B1"));
bListOriginalA3.add(new B("originalA3_B2"));
bListOriginalA3.add(new B("originalA3_B3"));
bListOriginalA3.add(new B("originalA3_B4"));
A originalA3 = new A("originalA3", bListOriginalA3);
originalA.add(originalA1);
originalA.add(originalA2);
originalA.add(originalA3);
List<B> bListNewA1 = new ArrayList<B>();
bListNewA1.add(new B("originalA1_B1"));
bListNewA1.add(new B("originalA1_B2"));
bListNewA1.add(new B("originalA1_B3"));
bListNewA1.add(new B("originalA1_B4"));
A newA1 = new A("originalA1", bListNewA1);
List<B> bListNewA2 = new ArrayList<B>();
bListNewA2.add(new B("originalA2_B1"));
bListNewA2.add(new B("originalA2_B3"));
bListNewA2.add(new B("originalA2_B4"));
bListNewA2.add(new B("originalA2_B2"));
A newA2 = new A("originalA2", bListNewA2);
List<B> bListNewA3 = new ArrayList<B>();
bListNewA3.add(new B("originalA3_B1"));
bListNewA3.add(new B("originalA3_B2"));
bListNewA3.add(new B("originalA3_B5"));
bListNewA3.add(new B("originalA3_B4"));
A newA3 = new A("originalA3", bListNewA3);
List<B> bListNewA4 = new ArrayList<B>();
bListNewA4.add(new B("A4_B1"));
bListNewA4.add(new B("A4_B2"));
bListNewA4.add(new B("A4_B3"));
bListNewA4.add(new B("A4_B4"));
A newA4 = new A("originalA4", bListNewA4);
newA.add(newA1);
newA.add(newA2);
newA.add(newA3);
newA.add(newA4);
List<A> result = newA.stream()
.filter(not(new HashSet<A>(originalA)::contains))
.collect(Collectors.toList());
A tempA = null;
B tempB = null;
List<B> bList = null;
for (A a : result) {
if (!containsName(originalA, a.getaName())) {
originalA.add(a);
} else {
tempA = getAIfPresent(originalA, a.getaName());
if (tempA != null) {
bList = a.getbList().stream()
.filter(not(new HashSet<B>(tempA.getbList())::contains))
.collect(Collectors.toList());
if (bList != null) {
tempA.getbList().addAll(bList);
}
}
}
}
System.out.println("");
}
public static <T> Predicate<T> not(Predicate<T> predicate) {
return predicate.negate();
}
public static boolean containsName(final List<A> list, final String name){
return list.stream().map(A::getaName).filter(name::equals).findFirst().isPresent();
}
public static A getAIfPresent(final List<A> list, final String name) {
return list.stream().filter(x -> x.getaName().equalsIgnoreCase(name)).findFirst().orElse(null);
}
}
|