A AttributeList method addAll() appends all the elements in the AttributeList specified to the end of the list, in the order in which they are returned by the Iterator of the AttributeList specified. It returns boolean value true, if this AttributeList changed, otherwise false.
1 public boolean addAll(AttributeList list)
list : It is a AttributeList containing an elements to be appended in this list.
return : It returns boolean value true, if this AttributeList changed, otherwise false.
AttributeList method addAll()
1 package com.java;
2
3 import javax.management.Attribute;
4 import javax.management.AttributeList;
5 import java.util.Arrays;
6
7 public class Main {
8
9 public static void main(String[] args) {
10
11 AttributeList attributeList = new AttributeList();
12 Attribute a1 = new Attribute("A1", 100);
13 Attribute a2 = new Attribute("A2", 200);
14
15 attributeList.addAll(Arrays.asList(a1, a2));
16 System.out.println("Attributes list : " + attributeList);
17
18 Attribute a3 = new Attribute("A3", 300);
19 AttributeList list = new AttributeList(Arrays.asList(a3));
20
21
22 attributeList.addAll(list);
23 System.out.println("Attributes after adding elements : " + attributeList);
24
25 attributeList.forEach(object -> {
26 Attribute attr = (Attribute)object;
27 System.out.println(attr.getName() + " -> " + attr.getValue());
28 });
29 }
30 }
In the above example, a AttributeList created and Attribute with name and value as Integer, added using addAll() method by passing a collection. A addAll() method called by passing a list that appends specified elements at the end of this list and iterated using forEach() method and print name and value by casting to Attribute type.
1 Attributes list : [A1 = 100, A2 = 200]
2 Attributes after adding elements : [A1 = 100, A2 = 200, A3 = 300]
3 A1 -> 100
4 A2 -> 200
5 A3 -> 300
AttributeList method addAll() with String
AttributeList method addAll() with String
1 package com.java;
2
3 import javax.management.Attribute;
4 import javax.management.AttributeList;
5 import java.util.Arrays;
6
7 public class StringExp {
8
9 public static void main(String[] args) {
10
11 Attribute a1 = new Attribute("A1", "Welcome");
12 Attribute a2 = new Attribute("A2", "to");
13
14 AttributeList attributeList = new AttributeList(Arrays.asList(a1, a2));
15 System.out.println("Attributes list : " + attributeList);
16
17 Attribute a3 = new Attribute("A3", "IOGyan");
18 AttributeList list = new AttributeList(Arrays.asList(a3));
19
20 attributeList.addAll(list);
21 System.out.println("Attributes after adding elements : " + attributeList);
22
23 attributeList.forEach(object -> {
24 Attribute attr = (Attribute)object;
25 System.out.println(attr.getName() + " -> " + attr.getValue());
26 });
27 }
28 }
In the above example, a AttributeList created and Attribute with name and value as String, added using addAll() method by passing a collection. A addAll() method called by passing a list that appends specified elements at the end of this list and iterated using forEach() method and print name and value by casting to Attribute type.
1 Attributes list : [A1 = Welcome, A2 = to]
2 Attributes after adding elements : [A1 = Welcome, A2 = to, A3 = IOGyan]
3 A1 -> Welcome
4 A2 -> to
5 A3 -> IOGyan
AttributeList method addAll() with custom object
AttributeList method addAll() with custom object
1 package com.java;
2
3 import javax.management.Attribute;
4 import javax.management.AttributeList;
5 import java.util.Arrays;
6
7 public class Example {
8
9 public static void main(String[] args) {
10
11 Employee emp1 = new Employee(100, "Emp1", "Tech", 12000);
12 Employee emp2 = new Employee(200, "Emp2", "Tech", 31000);
13 Employee emp3 = new Employee(300,"Emp3", "Admin", 21000);
14
15 Attribute a1 = new Attribute("E1", emp1);
16 Attribute a2 = new Attribute("E2", emp2);
17
18
19 AttributeList attributeList = new AttributeList(Arrays.asList(a1, a2));
20 System.out.println("Attributes list : " + attributeList);
21
22 Attribute a3 = new Attribute("E3", emp3);
23 AttributeList list = new AttributeList(Arrays.asList(a3));
24
25 attributeList.addAll(list);
26 System.out.println("Attributes list after set element : " + attributeList);
27
28 attributeList.forEach(object -> {
29 Attribute attr = (Attribute)object;
30 System.out.println(attr.getName() + " -> " + attr.getValue());
31 });
32 }
33 }
In the above example, a AttributeList created and Attribute with name and value as Employee, added using addAll() method by passing a collection. A addAll() method called by passing a list that appends specified elements at the end of this list and iterated using forEach() method and print name and value by casting to Attribute type. It uses Employee class toString() method to print Employee object.
1 Attributes list : [E1 = Emp1 Tech 12000, E2 = Emp2 Tech 31000]
2 Attributes list after set element : [E1 = Emp1 Tech 12000, E2 = Emp2 Tech 31000, E3 = Emp3 Admin 21000]
3 E1 -> Emp1 Tech 12000
4 E2 -> Emp2 Tech 31000
5 E3 -> Emp3 Admin 21000
AttributeList method addAll() with index position
A AttributeList method addAll() inserts all of the elements in the AttributeList specified into this list, starting at the specified index position, in the order in which they are returned by the Iterator of the AttributeList specified. It returns boolean value true, if this AttributeList changed, otherwise false.
1 public boolean addAll(int index, AttributeList list)
index : It is an index position to start inserting specified element in this list.
list : It is a list of element to be inserted in this list.
Exception:
1. | IndexOutOfBoundsException : If the index is out of range (index < 0 || index > size()) a RuntimeOperationsException should be raised, wrapping the IndexOutOfBoundsException thrown. |
AttributeList method addAll()
1 package com.java;
2
3 import javax.management.Attribute;
4 import javax.management.AttributeList;
5 import java.util.Arrays;
6
7 public class Main {
8
9 public static void main(String[] args) {
10
11 AttributeList attributeList = new AttributeList();
12 Attribute a1 = new Attribute("A1", 100);
13 Attribute a2 = new Attribute("A2", 200);
14
15 attributeList.addAll(Arrays.asList(a1, a2));
16 System.out.println("Attributes list : " + attributeList);
17
18 Attribute a3 = new Attribute("A3", 300);
19 AttributeList list = new AttributeList(Arrays.asList(a3));
20
21
22 attributeList.addAll(1, list);
23 System.out.println("Attributes after adding elements : " + attributeList);
24
25 attributeList.forEach(object -> {
26 Attribute attr = (Attribute)object;
27 System.out.println(attr.getName() + " -> " + attr.getValue());
28 });
29 }
30 }
In the above example, a AttributeList created and Attribute with name and value as Integer, added using addAll() method by passing a collection. A addAll() method called by passing an index position and list that inserts specified list elements at index position 1. A AttributeList elements iterated using forEach() method and print name and value by casting to Attribute.
1 Attributes list : [A1 = 100, A2 = 200]
2 Attributes after adding elements : [A1 = 100, A3 = 300, A2 = 200]
3 A1 -> 100
4 A3 -> 300
5 A2 -> 200
AttributeList method addAll() with index position and String
AttributeList method addAll() with index position and String
1 package com.java;
2
3 import javax.management.Attribute;
4 import javax.management.AttributeList;
5 import java.util.Arrays;
6
7 public class StringExp {
8
9 public static void main(String[] args) {
10
11 Attribute a1 = new Attribute("A1", "Welcome");
12 Attribute a2 = new Attribute("A2", "to");
13
14 AttributeList attributeList = new AttributeList(Arrays.asList(a1, a2));
15 System.out.println("Attributes list : " + attributeList);
16
17 Attribute a3 = new Attribute("A3", "IOGyan");
18 AttributeList list = new AttributeList(Arrays.asList(a3));
19
20 attributeList.addAll(0, list);
21 System.out.println("Attributes after adding elements : " + attributeList);
22
23 attributeList.forEach(object -> {
24 Attribute attr = (Attribute)object;
25 System.out.println(attr.getName() + " -> " + attr.getValue());
26 });
27 }
28 }
In the above example, a AttributeList created and Attribute with name and value as String, added using addAll() method by passing a collection. A addAll() method called by passing an index position and list that inserts specified list elements at index position 1. A AttributeList elements iterated using forEach() method and print name and value by casting to Attribute.
1 Attributes list : [A1 = Welcome, A2 = to]
2 Attributes after adding elements : [A3 = IOGyan, A1 = Welcome, A2 = to]
3 A3 -> IOGyan
4 A1 -> Welcome
5 A2 -> to
Employee class
1 package com.java;
2
3 public class Employee {
4
5 private final int id;
6 private final String name;
7 private final String dept;
8 private final Long salary;
9
10 public Employee(int id, String name, String dept, long salary) {
11 this.id = id;
12 this.name = name;
13 this.dept = dept;
14 this.salary = salary;
15 }
16 public int getId() {
17 return this.id;
18 }
19
20 public String getDept() {
21 return this.dept;
22 }
23
24 public String getName() {
25 return this.name;
26 }
27
28 public Long getSalary() {
29 return this.salary;
30 }
31
32 @Override
33 public int hashCode() {
34 return this.getId() + this.getName().hashCode();
35 }
36
37 @Override
38 public boolean equals(Object obj) {
39 return this.id == ((Employee)obj).id;
40 }
41
42 public String toString() {
43 return this.name + " " + this.dept + " " + this.salary;
44 }
45 }
Related options for your search