Change attribute of an object in ArrayList [duplicate] - java-8

This question already has answers here:
Updating an attribute of an element in list with matching attribute
(2 answers)
Closed 4 years ago.
I would like to modify an attribute of an object in an Arraylist. For example :
class A {
String text;
boolean bool;
// getters and setters....
}
If I have an ArrayList<A> in an other class, how can I modify the attribute of a given String text (which is in an object in the ArrayList)
I know it's possible on java 8 with collections and stream but I can't figure how to do it

list.forEach(a -> a.setText("some new value"));

Related

Is there a graceful approach to deserializing Go interface types? [duplicate]

This question already has answers here:
Can I unmarshal JSON into implementers of an Interface?
(2 answers)
Closed 11 months ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Original close reason(s) were not resolved
Given the following:
type Foo struct {
Td ThingDoer
// ... other stuff
}
type ThingDoer interface {
doThing()
}
type doerA struct {
AGuts string
}
func (a doerA) doThing() {}
type doerB struct {
BGuts string
}
func (b doerB) doThing() {}
is there a preferred serialization / deserialization strategy for Foo?
Attaching, eg, a MarshalJSON function onto doerA and doerB satisfies the serialization, but then Foo.UnmarshalJSON is effectively stuck: it can't know in advance whether the supplied JSON is of doerA or doerB type.
Edit: The linked "similar" question addresses the specific non-solution example outlined in this question. This question is asking about the existence of a graceful solution.
Imagine you have an structure Foo with one or more fields using interface types.
Lets say you have an interface Bar with two possible structures: Baz and Bam.
You can define auxiliary type (FooConf), without any interface. Only concrete types.
This structure may have a method Build() Foo that will choose the right type on each case.
To be possible define what is the concrete type you can define a signature. For instance an extra field “type” (baz or bam).
You just need to be sure about each type can marshal/unmarshal with consistency.

Appending to a slice in a struct in a map [duplicate]

This question already has answers here:
Append values to array inside of map golang
(2 answers)
Append a slice from a map value does not affect the map
(3 answers)
Need help understanding `map[String]type` behaviour in Go
(1 answer)
Cannot assign to struct field in a map
(5 answers)
Closed 1 year ago.
I'm trying to append to a slice within a map:
type MyOuterStruct struct{
name string
inners map[string]MyInnerStruct
}
type MyInnerStruct struct{
name string
list []int
}
func main() {
ms := MyOuterStruct{
name: "outer",
inners: map[string]MyInnerStruct{
"a": {name: "inner"},
},
}
ms.inners["a"].list = append(ms.inners["a"].list, 4, 5, 6)
fmt.Println(ms.inners["a"].list)
//cannot assign to struct field ms.inners["a"].list in map
}
I know the issue is I'm assigning to an "unaddressable" field, but I'm not sure how to structure this properly.
I've tried the following
myCurrentList := ms.inners["a"].list
myCurrentList = append(myCurrentList, 4, 5, 6)
ms.inners["a"].list = myCurrentList
But this wasn't correct either.
How should I structure the MyInnerStruct so that I can have access to a dynamic slice list than I can append to?
The problem is that ms.inners["a"] returns a copy of the value stored for that key. So when you modify a member of that copy, it is not reflected on the copy stored in the map.
The easiest solution is to define the map as:
map[string]*MyInnerStruct
This way, ms.inners["a"] returns a pointer to the value stored in the map. Another solution is:
x:=ms.inners["a"]
x.list = myCurrentList
ms.inners["a"]=x
This copies the struct stored in the map to x, modifies it, and copies it back.

How to instatiate the class by dynamically receiving the class name [duplicate]

This question already has answers here:
How do I create a class instance from a string name in ruby?
(4 answers)
Closed 4 years ago.
I need to create a class but the condition is, I will receive the class name in strings like shown below
["IndividualContact", "Legal"].each do |var|
ind = var.new
end
Now My expectation is, I need to call
IndividualContact.new and Legal.new but since var is a string variable, it's calling .new on a string like given below
"IndividualContact".new
rather than calling
IndividualContact.new
Is there any way can I call as I expected?
Use Module#const_get (assuming these classes are already defined in the global namespace):
%w|IndividualContact Legal|.map do |klazz|
Kernel.const_get(klazz).new
end
The code above will return an array containing two instances: one instance of IndividualContact and one of Legal.

Golang: I have a map of int to struct. Why can't I directly modify a field in a map value? [duplicate]

This question already has answers here:
Why do I get a "cannot assign" error when setting value to a struct as a value in a map? [duplicate]
(2 answers)
Closed 6 years ago.
Why do we have to first read the struct, modify it, and then write it back to the map? Am I missing some kind of implied hidden cost in modifying fields of structs in other data structures (like a map or a slice)?
Edit:
I realize I can use pointers, but why is this not allowed by Go?
type dummy struct {
a int
}
x := make(map[int]dummy)
x[1] = dummy{a:1}
x[1].a = 2
You are storing a struct by value which means that accession of that struct in the map gives you a copy of the value. This is why when you modify it, the struct in the map remains unmutated until you overwrite it with the new copy.
As RickyA pointed out in the comment, you can store the pointer to the struct instead and this allows direct modification of the struct being referenced by the stored struct pointer.
i.e. map[whatever]*struct instead of map[whatever]struct

What does mean about the `json:"make"` at the end of Make string in Golang? [duplicate]

This question already has answers here:
What is the usage of backtick in golang structs definition? [duplicate]
(2 answers)
Closed 6 years ago.
I am working on some Golang source code, and confusing on Program syntax as below.
What does mean about the json:"make" at the end of Make string in Golang?
type Vehicle struct {
Make string `json:"make"`
Model string `json:"model"`
Reg string `json:"reg"`
VIN int `json:"VIN"`
Owner string `json:"owner"`
Scrapped bool `json:"scrapped"`
Status int `json:"status"`
Colour string `json:"colour"`
V5cID string `json:"v5cID"`
LeaseContractID string `json:"leaseContractID"`
}
Tags are used by encoding packages like encoding/json or encoding/xml to control how fields are interpreted during encoding and decoding. Uses of tags are already discussed in this thread: What are the use(s) for tags in Go?

Resources