0% found this document useful (0 votes)
7 views6 pages

Java Collections: ArrayList to PriorityQueue

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Java Collections: ArrayList to PriorityQueue

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Array List:
Import [Link];
Import [Link];
Import [Link];

Public class Arrays {


Public static void main(String[] args) {
List<String> l = new ArrayList<>();
Scanner s = new Scanner([Link]);
[Link](“Placeholder”);

for (int I = 0; I < 3; i++) {


[Link]([Link]());
}

[Link](“Placeholder”);
[Link](“Example”);

for (String I : l) {
[Link](i);
}
}
}

OUTPUT:
Alpha
Beta
Gamma

Alpha
Beta
Gamma

2. Hashset
Import [Link];

Import [Link];
Import [Link];

Public class Sets {

Public static void main(String[] args) {

Scanner s = new Scanner([Link]);

Set<Integer> sets = new HashSet<>();

Int n = [Link]();

For (int I = 0; I < n; i++) {

[Link]([Link]());

[Link](sets);

Output:

10

20

10

30

40

[20, 40, 10, 30]

3. HashMaps
Import [Link];
Import [Link];

Public class Hashmaps {

Public static void main(String[] args) {

Map<String, Integer> maps = new HashMap<>();

[Link](“John”, 25);

[Link](“Alice”, 30);

[Link](“John’s age: “ + [Link](“John”));

Output:

John’s age: 25

4. Linked List
Import [Link].*;

Public class Linkedlist {

Public static void main(String[] args) {

LinkedList<String> l = new LinkedList<>();

[Link](“John”);

[Link](“Doe”);

[Link](“X”);

[Link](“Y”);

[Link](“Z”);
Iterator<String> I = [Link]();

While ([Link]()) {

[Link]([Link]());

Output:

John

Doe

5. Stack
Import [Link].*;

Public class Stacks {

Public static void main(String[] args) {

Stack<String> s = new Stack<>();

[Link](“John”);

[Link](“Doe”);

[Link](“Smith”);

[Link]();

Iterator<String> t = [Link]();

While ([Link]()) {
[Link]([Link]());

Output:

John

Doe

6. Vectors
Import [Link].*;

Public class Vectores {

Public static void main(String[] args) {

Scanner I = new Scanner([Link]);

Int n = [Link]();

Vector<Integer> v = new Vector<>();

For (int j = 0; j < n; j++) {

[Link]([Link]());

[Link](v);

Output:

Input:

4
10

20

30

40

Output:

[10, 20, 30, 40]

7. Priority Queue

Import [Link].*;

Public class PrioriQueue {


Public static void main(String[] args) {
PriorityQueue<String> q = new PriorityQueue<>();
[Link](“John”);
[Link](“Alice”);
[Link]();
[Link](“Bob”);
while (![Link]()) {
[Link]([Link]());
}
}
}
Output:
Alice
Bob

You might also like