boolean |
add(Object o) Ensures that this collection contains the specified element (optional operation). |
boolean |
contains(Object o) Returns true if this collection contains the specified element. |
boolean |
isEmpty()
Returns true if this collection contains no elements. |
Iterator |
iterator()
Returns an iterator over the elements in this collection. |
boolean |
remove(Object o) Removes a single instance of the specified element from this collection, if it is present. |
int |
size()
Returns the number of elements in this collection. |
Object[] |
toArray()
Returns an array containing all of the elements in this collection. |
void |
add(int index,
Object element)
Inserts the specified element at the specified position in this list (optional operation). |
Object |
get(int index)
Returns the element at the specified position in this list. |
int |
indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. |
int |
lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. |
ListIterator |
listIterator()
Returns a list iterator of the elements in this list (in proper sequence). |
ListIterator |
listIterator(int index)
Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. |
Object |
remove(int index)
Removes the element at the specified position in this list (optional operation). |
Object |
set(int index,
Object element)
Replaces the element at the specified position in this list with the specified element (optional operation). |
List |
subList(int fromIndex,
int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. |
boolean |
containsKey(Object key) Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue(Object value)
Returns true if this map maps one or more keys to the specified value. |
Object |
get(Object key) Returns the value to which this map maps the specified key. |
boolean |
isEmpty()
Returns true if this map contains no key-value mappings. |
Set |
keySet()
Returns a set view of the keys contained in this map. |
Object |
put(Object key, Object value)
Associates the specified value with the specified key in this map (optional operation). |
Object |
remove(Object key) Removes the mapping for this key from this map if it is present (optional operation). |
int |
size()
Returns the number of key-value mappings in this map. |
Collection |
values()
Returns a collection view of the values contained in this map. |
Näiteks klass HashMap realiseerib selle liidese. Sisuliselt on Java varasemates versioonides defineeritud klass Hashtable samade omadustega.
boolean |
hasNext()
Returns true if the iteration has more elements. |
Object |
next()
Returns the next element in the iteration. |
public int compareTo(Object o)
ClassCastException
- if the specified object's type prevents it from being
compared to this Object.static int |
binarySearch(List list, Object key) Searches the specified list for the specified object using the binary search algorithm. |
static void |
copy(List dest, List src)
Copies all of the elements from one list into another. |
static void |
fill(List list, Object obj) Replaces all of the elements of the specified list with the specified element. |
static int |
indexOfSubList(List source, List target)
Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. |
static int |
lastIndexOfSubList(List source, List target)
Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. |
static Object |
max(Collection coll)
Returns the maximum element of the given collection, according to the natural ordering of its elements. |
static Object |
min(Collection coll)
Returns the minimum element of the given collection, according to the natural ordering of its elements. |
static List |
nCopies(int n,
Object o) Returns an immutable list consisting of n copies of the specified object. |
static boolean |
replaceAll(List list, Object oldVal, Object newVal)
Replaces all occurrences of one specified value in a list with another. |
static void |
reverse(List list)
Reverses the order of the elements in the specified list. |
static void |
rotate(List list,
int distance) Rotates the elements in the specified list by the specified distance. |
static void |
shuffle(List list, Random rnd) Randomly permute the specified list using the specified source of randomness. |
static void |
sort(List list)
Sorts the specified list into ascending order, according to the natural ordering of its elements. |
static void |
swap(List list,
int i, int j) Swaps the elements at the specified positions in the specified list. |
final int SUURUS = 100;
ArrayList juhuarvud = new ArrayList (SUURUS);
Random generaator = new Random();
for (int i=0; i<SUURUS; i++) {
juhuarvud.add (new Integer (generaator.nextInt(1000)));
}
HashMap ained = new HashMap();
ained.put ("I200", "Java");
ained.put ("I209", "Algoritmid");
ained.put ("MTAT.03.001", "Programmeerimine II");
ained.put ("MTAT.03.022", "Rakendustarkvara: Internet");
Iterator koodid = ained.keySet().iterator();
while (koodid.hasNext()) {
System.out.println (koodid.next());
}
String aine = (String)ained.remove ("MTAT.03.022");
System.out.println (aine);
koodid = ained.keySet().iterator();
String kood;
while (koodid.hasNext()) {
kood = (String)koodid.next();
System.out.println (kood + " " + (String)ained.get (kood));
}
/**
* Maksimaalse elemendi leidmine järjendist.
* @param a järjend
* (rahuldab liidest java.util.List,
* elemendid rahuldavad liidest java.lang.Comparable).
* @return järjendi maksimaalne element.
* @throws IndexOutOfBoundsException kui järjend on tühi.
*/
static public Comparable maksimum (List a) {
Comparable maks;
if (a.size() < 1)
throw new IndexOutOfBoundsException (" maksimumi leidmisel");
maks = (Comparable)a.get (0);
for (int i=0; i<a.size(); i++) {
if (maks.compareTo ((Comparable)a.get (i)) < 0)
maks = (Comparable)a.get (i);
}
return maks;
} // maksimum lopp
/**
* Maksimaalse elemendi leidmine.
* @param a Collection, mille
* elemendid rahuldavad liidest Comparable.
* @return maksimaalne element.
* @throws NoSuchElementException kui <code> a </code> on tühi.
*/
static public Comparable maksimum2 (Collection a) {
Comparable maks;
Iterator iteraator = a.iterator();
if (iteraator.hasNext())
maks = (Comparable)iteraator.next();
else throw new NoSuchElementException (" maksimumi leidmisel");
Comparable c;
while (iteraator.hasNext()) {
c = (Comparable)iteraator.next();
if (maks.compareTo (c)<0)
maks = c;
}
return maks;
} // maksimum2 lopp
Java 5 jaoks:
/**
* Finding a maximal element of a collection.
* @param a Collection, elements satisfy interface Comparable.
* @return maximal element.
* @throws NoSuchElementException if <code> a </code> is empty.
*/
static public < T extends Object & Comparable < ? super T > > T
maximum2 (Collection < ? extends T > a) {
T maxelem;
if (a == null)
throw new IndexOutOfBoundsException (" maximum2 got a null collection");
Iterator < ? extends T > iter = a.iterator();
if (iter.hasNext())
maxelem = iter.next();
else throw new NoSuchElementException (" maximum2 got an empty collection");
for (T c : a) {
if (maxelem.compareTo (c) < 0)
maxelem = c;
}
return maxelem;
} // maximum2
Implementations | ||||||
---|---|---|---|---|---|---|
Hash Table | Resizable Array | Balanced Tree | Linked List | Hash Table + Linked List | ||
Interfaces | Set | HashSet | TreeSet | LinkedHashSet | ||
List | ArrayList | LinkedList | ||||
Deque | ArrayDeque | LinkedList | ||||
Map | HashMap | TreeMap | LinkedHashMap |
import java.util.*;
/** Stream.
* @author Jaanus
* @since 1.8
*/
public class StrExample {
public static void main (String[] args) {
int sum = 0;
List<Integer> myList = Arrays.asList (1, -1, 0, 2, -4, -5);
System.out.println (myList);
sum = myList.stream()
.filter (elem -> (elem > 0))
.reduce (0, (s, e) -> s + e);
System.out.println ("SumPos is: " + sum);
int[] myArray = new int[] {1, 2, 3, 6, -6, -5};
System.out.println (Arrays.toString (myArray));
sum = Arrays.stream (myArray)
.filter (elem -> (elem < 0))
.reduce (0, (s, e) -> s + e);
System.out.println ("SumNeg is: " + sum);
}
}