.


:




:

































 

 

 

 


SortedSet tailSet(E fromElement);




. headSet(E element) tailSet(E element, boolean inclusive) , element . inclusive true, .

NavigableSet.

/* # 13: c NavigableSet: NavigableSetTest.java */

package by.bsu.chapt10;

import java.util.*;

public class NavigableSetTest {

public static void main(String[] args) {

HashSet<String> city = new HashSet<String>();

city.add("Minsk");

city.add("Mosow");

city.add("Polotsk");

city.add("Brest");

NavigableSet<String> ns = new TreeSet<String>(city);

System. out. println("All: " + ns);

System. out. println("Between Minsk and Polotsk: "

+ ns.subSet("Minsk","Polotsk"));

System. out. println("Before Minsk: "

+ ns.headSet("Minsk"));

System. out. println("After Minsk: "

+ ns.tailSet("Minsk", false));

}

}

:

All: [Brest, Minsk, Mosow, Polotsk]

Between Minsk and Polotsk: [Minsk, Mosow]

Before Minsk: [Brest]

After Minsk: [Mosow, Polotsk]

, -. () , . - hashCode() equals() . , null.

:

AbstractMap<K,V> Map<K,V>;

HashMap<K,V> AbstractMap<K,V>, -, -;

TreeMap<K,V> AbstractMap<K,V>, , .

WeakHashMap<K,V> , .

LinkedHashMap<K,V> . , - .

. 3.

IdentityHashMap<K,V> - - System.identityHashCode() , hashCode(), .

:

Map<K,V> ;

Map.Entry<K,V> -;

SortedMap<K,V> ;

NavigableMap<K,V> .

Map<K,V> :

void clear() ;

boolean containsKey(Object key) true, key ;

boolean containsValue(Object value) true, value ;

Set<Map.Entry<K,V>> entrySet() , ;

Set<K> keySet() ;

V get(Object obj) , obj;

V put(K key, V value) key value . . ;

void putAll(Map <? extends K,? extends V> t) t ;

V remove(Object key) - key;

Collection<V> values() , .

Map.Entry<K,V> :

K getKey() ;

V getValue() ;

V setValue(V obj) obj .

-
.

/* # 14: - :

DemoHashMap.java */

package chapt10;

import java.util.*;

public class DemoHashMap {

public static void main(String[] args){

HashMap<Integer, String> hm =

new HashMap<Integer, String>(5);

for (int i = 11; i < 15; i++) {

hm.put(i, i + "EL");

}

System. out. println(hm);

hm.put(12, "14EL");

System. out. println(hm + " ");

String a = hm.get(12);

System. out. println(a + " - '12'");

/* -

Map.Entry<K,V> */

Set<Map.Entry<Integer, String>> setvalue =

hm.entrySet();

System. out. println(setvalue);

Iterator<Map.Entry<Integer, String>> i =

setvalue.iterator();

while (i.hasNext()) {

Map.Entry<Integer, String> me = i.next();

System. out. print(me.getKey()+": ");

System. out. println(me.getValue());

}

}

}

{13=13EL, 14=14EL, 12=12EL, 11=11EL}

{13=13EL, 14=14EL, 12=14EL, 11=11EL}

14EL - '12'

[13=13EL, 14=14EL, 12=14EL, 11=11EL]

EL

EL

EL

EL

, HashMap<K,V> Map.Entry<K,V> .

/* # 15: :

DemoSecurity.java */

package chapt10;

import java.util.*;

 

public class DemoSecurity {

public static void main(String[] args) {

CheckRight. startUsing (2041, "");

CheckRight. startUsing (2420, "");

/*

*

*

*/

CheckRight. startUsing (2437, "");

CheckRight. startUsing (2041, "");

}

}

/* # 16: : CheckRight.java */

package chapt10;

import java.util.*;

 

public class CheckRight {

private static HashMap<Integer, String> map =

new HashMap<Integer, String> ();

 

public static void startUsing(int id, String name) {

if (canUse(id)){

map.put(id, name);

System. out. println(" ");

} else {

System. out. println(" ");

}

}

public static boolean canUse(int id) {

final int MAX_NUM = 2; // 2 3

int currNum = 0;

if (!map.containsKey(id))

currNum = map.size();

return currNum < MAX_NUM;

}

}

:

,

. MAX_NUM , 2, .

EnumMap<K extends Enum<K>, V> , enum, . .

/* # 17: EnumMap: UseEnumMap.java */

package chapt10;

import java.util.EnumMap;

 

enum User {

STUDENT, TUTOR, INSTRUCTOR, DEAN

}

class UserPriority {

private int priority;

 

public UserPriority(User k) {

switch (k) {

case STUDENT:

priority = 1; break;

case TUTOR:

priority = 3; break;

case INSTRUCTOR:

priority = 7; break;

case DEAN:

priority = 10; break;

default:

priority = 0;

}

}

public int getPriority() {

return priority;

}

}

public class UseEnumMap {

public static void main(String[] args) {

EnumMap<User, UserPriority> faculty =

new EnumMap<User, UserPriority> (User. class);

for (User user: User.values()) {

faculty.put(user,

new UserPriority(user));

}

for (User user: User.values()) {

System. out. println(user.name()

+ "-> Priority:" +

((UserPriority) faculty.get(user)).getPriority());

}

}

}

:

STUDENT-> Priority:1

TUTOR-> Priority:3

INSTRUCTOR-> Priority:7

DEAN-> Priority:10

NavigableMap<K, V>

NavigableSet, , , , "-":

Map.Entry<K,V> lowerEntry(K key);
Map.Entry<K,V> floorEntry(K key);
Map.Entry<K,V> higherEntry(K key);
Map.Entry<K,V> ceilingEntry(K key);
K lowerKey(K key);
K floorKey(K key);
K higherKey(K key);
K ceilingKey(K key);

pollFirstEntry pollLastEntry , . firstEntry lastEntry , .

Map.Entry<K,V> pollFirstEntry();
Map.Entry<K,V> pollLastEntry();
Map.Entry<K,V> firstEntry();
Map.Entry<K,V> lastEntry();

, :
NavigableMap<K,V> descendingMap();

, , :

NavigableSet navigableKeySet();
NavigableSet descendingKeySet();

, , , . NavigableSet, , . , , , .

NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive);
NavigableMap<K,V> headMap(K toKey, boolean inclusive);
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
SortedMap<K,V> subMap(K fromKey, K toKey);
SortedMap<K,V> headMap(K toKey);
SortedMap<K,V> tailMap(K fromKey);

, , , , , Java , Hashtable<K,V>, Vector<E> ( ) Enumeration<E>. , .

Hashtable<K,V> Map, :

Enumeration<V> elements() ;

Enumeration<K> keys() .

/* # 18: - :

HashTableDemo.java */

package by.bsu.chapt10;

import java.util.*;

import java.io.*;

 

public class HashTableDemo {

public static void main(String[] args) {

Hashtable<Integer, Double> ht =

new Hashtable<Integer, Double>();

for (int i = 0; i < 5; i++) {

ht.put(i, Math. atan (i));

}

Enumeration<Integer> ek = ht.keys();

int key;

while (ek.hasMoreElements()) {

key = ek.nextElement();

System. out. printf("%4d ", key);

}

System. out. println("");

Enumeration<Double> ev = ht.elements();

double value;

while (ev.hasMoreElements()) {

value = ev.nextElement();

System. out. printf("%.2f ", value);

}

}

}

:

4 3 2 1 0

1,33 1,25 1,11 0,79 0,00

, , .

Collections

Collections , . , , , , . Collections : public static <E> Collection <E> checkedCollection(Collection<E> c, Class<E> type)

, ,
ClassCastException:

/* # 19: : SafeSet.java */

package by.bsu.chapt10;

import java.util.*;

public class SafeSet {

public static void main(String args[]) {

//HashSet c = new HashSet();

HashSet c = Collections. checkedSet (

new HashSet<String>(), String. class);

c.add("Java");

c.add(7.0); //

}

}

, , : checkedList(),
checkedSortedMap(), checkedMap(), checkedSortedSet(),
checkedCollection(), :

<T> boolean addAll(Collection<? super T> c, T... a) ;

<T> void copy(List<? super T> dest, List<? extends T> src) ;

boolean disjoint(Collection<?> c1, Collection<?> c2) true, ;

<T> List <T> emptyList(), <K,V> Map <K,V> emptyMap(),
<T> Set <T> emptySet()
,
;

<T> void fill(List<? super T> list, T obj) ;

int frequency(Collection<?> c, Object o) ;

<T extends Object & Comparable <? super T>> T max(Collection<? extends T> coll),

<T extends Object & Comparable <? super T>> T min(Collection<? extends T> coll)
;

<T> T max(Collection <? extends T> coll,

Comparator<? super T> comp),

<T> T min(Collection<? extends T> coll,

Comparator<? super T> comp) , Comparator ;

<T> List <T> nCopies(int n, T o) n ;

<T> boolean replaceAll(List<T> list, T oldVal, T newVal) ;

void reverse(List<?> list) ;

void rotate(List<?> list, int distance) ;

void shuffle(List<?> list) ;

<T> Set <T> singleton(T o), singletonList(T o), singletonMap(K key, V value) , , ;

<T extends Comparable<? super T>> void sort(List<T> list),

<T> void sort(List<T> list, Comparator<? super T> c) , Comparator ;

void swap(List<?> list, int i, int j) .

/* # 20: Collections: CollectionsDemo.java:

MyComparator.java */

package by.bsu.chapt10;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class CollectionsDemo {

public static void main(String[] args) {

MyComparator<Integer> comp =

new MyComparator<Integer>();

ArrayList<Integer> list =

new ArrayList<Integer>();

 

Collections. addAll (list, 1, 2, 3, 4, 5);

Collections. shuffle (list);

print(list);

Collections. sort (list, comp);

print(list);

Collections. reverse (list);

print(list);

Collections. rotate (list, 3);

print(list);

System. out. println("min: "

+ Collections.min(list, comp));

System. out. println("max: "

+ Collections.max(list, comp));

 

List<Integer> singl =

Collections.singletonList(71);

print(singl);

//singl.add(21);//

}

static void print(List<Integer> c) {

for (int i: c)

System.out.print(i + " ");

System.out.println();

}

}

package chapt10;

import java.util.Comparator;

 

public class MyComparator<T> implements Comparator<Integer> {

public int compare(Integer n, Integer m) {

return m.intValue() - n.intValue();

}

}

:

4 3 5 1 2

5 4 3 2 1

1 2 3 4 5

3 4 5 1 2

min: 5

max: 1

Arrays

java.util Arrays, , , , , :

static int binarySearch( ) . ;

static void fill( ) ;

static void sort( ) Comparator ;

static <T> T[] copyOf(T[] original, int newLength) , null ;

static <T> T[] copyOfRange(T[] original, int from, int to) ;

static boolean equals( 1, 2 ) ;

static boolean deepEquals( Object[] a1, Object[] a2 ) ;

static String deepToString( Object[] a ) ;

static int deepHashCode( Object[] a ) - ;

static <T> List<T> asList(T... a) , List<T>.

.

/* # 21: Arrays: ArraysEqualDemo.java */

package chapt10;

import java.util.*;

public class ArraysEqualDemo {

public static void main(String[] args) {

char m1[] = new char [3];

char m2[] = { 'a', 'b', 'c' };

char i;

Arrays. fill (m1, 'a');

System. out. print(" m1:");

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

System. out. print(" " + m1[i]);

}

m1[1] = 'b';

m1[2] = 'c';

//m1[2]='x'; //

if (Arrays. equals (m1, m2))

System. out. print("\nm1 m2 ");

Else

System. out. print("\nm1 m2 ");

 

m1[0] = 'z';

Arrays.sort(m1);

System. out. print("\n m1:");

for (i = 0; i < 3; i++)

System. out. print(" " + m1[i]);

System. out. print(

"\n 'c' -"

+ Arrays. binarySearch (m1, 'c'));

Integer arr[] = {35, 71, 92};

//

System. out. println(Arrays. deepToString (arr));

// -

System. out. println(Arrays. deepHashCode (arr));

Integer arr2[] = {35, 71, 92};

//

System. out. println(Arrays. deepEquals (arr, arr2));

char m3[] = new char [5];

//

m3 = Arrays. copyOf (m1, 5);

System. out. print(" m3:");

for (i = 0; i < 5; i++) {

System. out. print(" " + m3[i]);

}

}

}

:

m1: a a a

M1 m2

m1: b c z

'c' 1

[35, 71, 92]

True

m3: b c z □ □

 





:


: 2017-02-24; !; : 343 |


:

:

- , - .
==> ...

1684 - | 1607 -


© 2015-2024 lektsii.org - -

: 0.227 .