Java colllections FrameWork Flashcards
(19 cards)
What is the JCF?
It is a set of classes and interfaces that come by default with java and provide ready-to-use data structures and algorithms for storing, retriving, and manipulating groups of objects efficiently
What are 3 collections you know from Java?
List, Sets, and Maps
What is a List?
It is an orderd collection of elments, where elements can be duplicated, and elements can be accesed by their index the collection.
What is a set?
It is a collection of elements in which duplicate elements are not allowed.
What is a map?
It is a collection of key-value pairs.
How do you create and use an Array in Java?
```java
String[] fruits = {“Apple”, “Banana”, “Orange”};
System.out.println(fruits[0]);
~~~
How do you access elements in an Array in Java?
```java
String firstFruit = fruits[0];
System.out.println(firstFruit);
~~~
How do you create and add elements to an ArrayList in Java?
```java
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Blue");
~~~</String>
How do you access elements in an ArrayList in Java?
```java
String color = colors.get(0);
System.out.println(color);
~~~
How do you remove an element from an ArrayList in Java?
```java
colors.remove(“Red”);
colors.remove(0); // remove by index
~~~
How do you create a LinkedList and add elements in Java?
```java
LinkedList<String> names = new LinkedList<>();
names.add("Alice");
names.add("Bob");
~~~</String>
How do you access elements in a LinkedList in Java?
```java
String name = names.get(0);
System.out.println(name);
~~~
How do you remove an element from a LinkedList in Java?
```java
names.remove(“Alice”);
names.remove(0); // remove by index
~~~
How do you create a HashMap and add key-value pairs in Java?
```java
HashMap<String, Integer> scores = new HashMap<>();
scores.put(“Math”, 90);
scores.put(“English”, 85);
~~~
How do you access values in a HashMap in Java?
```java
int mathScore = scores.get(“Math”);
System.out.println(mathScore);
~~~
How do you remove a key-value pair from a HashMap in Java?
```java
scores.remove(“Math”);
~~~
How do you create a Set or HashSet and add elements in Java?
```java
HashSet<String> cities = new HashSet<>();
cities.add("Nairobi");
cities.add("Lagos");
~~~</String>
How do you access elements in a HashSet in Java?
```java
for(String city : cities) {
System.out.println(city);
}
~~~
How do you remove an element from a HashSet in Java?
```java
cities.remove(“Lagos”);
~~~