Java 1.5 Tiger A Developers Notebook [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Java 1.5 Tiger A Developers Notebook [Electronic resources] - نسخه متنی

David Flanagan, Brett McLaughlin

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








2.2 Using Type-Safe Maps


As cool as generics make the List class, it wouldn't be much good if that
was the only collection that could be parameterized. All of the various
collection classes are now generic types, and accept type parameters.
Since most of these behave like List, I'll spare you the boring prose of
covering each one. It is worth looking at Map, though, as it takes two type
parameters, instead of just one. You use it just as you use List, but with
two types at declaration and initialization.


2.2.1 How do I do that?


java.util.Map has a
key type (which can be any type) and a value type
(which can be any type). While it's common to use a numeric or String
key, that's not built into the language, and you can't depend on itat
least, not until Tiger came along:

        Map<Integer, Integer> squares = new HashMap<Integer, Integer>( );
for (int i=0; i<100; i++) {
squares.put(i, i*i);
}
for (int i=0; i<10; i++) {
int n = i*3;
out.println("The square of " + n + " is " + squares.get(n));
}

This is a simple example of where a new Map is declared, and both its
key and value types are defined as Integer. This ensures that you don't
have to do any casting, either in putting values into the Map or pulling them out. Pretty easy stuff, isn't it? Of course, you could use any of the
following lines of code as well:

    // Key and value are Strings
Map<String, String> strings = new HashMap<String, String>( );
// Key is a String, value is an Object
Map<String, Object> map = new HashMap<String, Object>( );
// Key is a Long, value is a String
Map<Long, String> args = new HashMap<Long, String>( );


2.2.2 What just happened?


As briefly mentioned in Using Type-Safe Lists, autoboxing helps when
you want to stuff primitives into a collection. In this case, even though
the Map is defined to take Integers, it's the int counter i that is used to
create values. Without getting into the details covered in Chapter 4, Java
autoboxes the int value of i into an Integer, behind the scenes, meeting
the requirements of the squares Map.


/ 131