chapter progress
This commit is contained in:
parent
d8a46db6f3
commit
37c5413501
@ -10,129 +10,206 @@ import java.util.*;
|
||||
public class Countries {
|
||||
public static final String[][] DATA = {
|
||||
// Africa
|
||||
{"ALGERIA","Algiers"}, {"ANGOLA","Luanda"},
|
||||
{"BENIN","Porto-Novo"}, {"BOTSWANA","Gaberone"},
|
||||
{"ALGERIA","Algiers"},
|
||||
{"ANGOLA","Luanda"},
|
||||
{"BENIN","Porto-Novo"},
|
||||
{"BOTSWANA","Gaberone"},
|
||||
{"BURKINA FASO","Ouagadougou"},
|
||||
{"BURUNDI","Bujumbura"},
|
||||
{"CAMEROON","Yaounde"}, {"CAPE VERDE","Praia"},
|
||||
{"CAMEROON","Yaounde"},
|
||||
{"CAPE VERDE","Praia"},
|
||||
{"CENTRAL AFRICAN REPUBLIC","Bangui"},
|
||||
{"CHAD","N'djamena"}, {"COMOROS","Moroni"},
|
||||
{"CONGO","Brazzaville"}, {"DJIBOUTI","Dijibouti"},
|
||||
{"EGYPT","Cairo"}, {"EQUATORIAL GUINEA","Malabo"},
|
||||
{"ERITREA","Asmara"}, {"ETHIOPIA","Addis Ababa"},
|
||||
{"GABON","Libreville"}, {"THE GAMBIA","Banjul"},
|
||||
{"GHANA","Accra"}, {"GUINEA","Conakry"},
|
||||
{"CHAD","N'djamena"},
|
||||
{"COMOROS","Moroni"},
|
||||
{"CONGO","Brazzaville"},
|
||||
{"DJIBOUTI","Dijibouti"},
|
||||
{"EGYPT","Cairo"},
|
||||
{"EQUATORIAL GUINEA","Malabo"},
|
||||
{"ERITREA","Asmara"},
|
||||
{"ETHIOPIA","Addis Ababa"},
|
||||
{"GABON","Libreville"},
|
||||
{"THE GAMBIA","Banjul"},
|
||||
{"GHANA","Accra"},
|
||||
{"GUINEA","Conakry"},
|
||||
{"BISSAU","Bissau"},
|
||||
{"COTE D'IVOIR (IVORY COAST)","Yamoussoukro"},
|
||||
{"KENYA","Nairobi"}, {"LESOTHO","Maseru"},
|
||||
{"LIBERIA","Monrovia"}, {"LIBYA","Tripoli"},
|
||||
{"MADAGASCAR","Antananarivo"}, {"MALAWI","Lilongwe"},
|
||||
{"MALI","Bamako"}, {"MAURITANIA","Nouakchott"},
|
||||
{"MAURITIUS","Port Louis"}, {"MOROCCO","Rabat"},
|
||||
{"MOZAMBIQUE","Maputo"}, {"NAMIBIA","Windhoek"},
|
||||
{"NIGER","Niamey"}, {"NIGERIA","Abuja"},
|
||||
{"KENYA","Nairobi"},
|
||||
{"LESOTHO","Maseru"},
|
||||
{"LIBERIA","Monrovia"},
|
||||
{"LIBYA","Tripoli"},
|
||||
{"MADAGASCAR","Antananarivo"},
|
||||
{"MALAWI","Lilongwe"},
|
||||
{"MALI","Bamako"},
|
||||
{"MAURITANIA","Nouakchott"},
|
||||
{"MAURITIUS","Port Louis"},
|
||||
{"MOROCCO","Rabat"},
|
||||
{"MOZAMBIQUE","Maputo"},
|
||||
{"NAMIBIA","Windhoek"},
|
||||
{"NIGER","Niamey"},
|
||||
{"NIGERIA","Abuja"},
|
||||
{"RWANDA","Kigali"},
|
||||
{"SAO TOME E PRINCIPE","Sao Tome"},
|
||||
{"SENEGAL","Dakar"}, {"SEYCHELLES","Victoria"},
|
||||
{"SIERRA LEONE","Freetown"}, {"SOMALIA","Mogadishu"},
|
||||
{"SENEGAL","Dakar"},
|
||||
{"SEYCHELLES","Victoria"},
|
||||
{"SIERRA LEONE","Freetown"},
|
||||
{"SOMALIA","Mogadishu"},
|
||||
{"SOUTH AFRICA","Pretoria/Cape Town"},
|
||||
{"SUDAN","Khartoum"},
|
||||
{"SWAZILAND","Mbabane"}, {"TANZANIA","Dodoma"},
|
||||
{"TOGO","Lome"}, {"TUNISIA","Tunis"},
|
||||
{"SWAZILAND","Mbabane"},
|
||||
{"TANZANIA","Dodoma"},
|
||||
{"TOGO","Lome"},
|
||||
{"TUNISIA","Tunis"},
|
||||
{"UGANDA","Kampala"},
|
||||
{"DEMOCRATIC REPUBLIC OF THE CONGO (ZAIRE)",
|
||||
"Kinshasa"},
|
||||
{"ZAMBIA","Lusaka"}, {"ZIMBABWE","Harare"},
|
||||
{"ZAMBIA","Lusaka"},
|
||||
{"ZIMBABWE","Harare"},
|
||||
// Asia
|
||||
{"AFGHANISTAN","Kabul"}, {"BAHRAIN","Manama"},
|
||||
{"BANGLADESH","Dhaka"}, {"BHUTAN","Thimphu"},
|
||||
{"AFGHANISTAN","Kabul"},
|
||||
{"BAHRAIN","Manama"},
|
||||
{"BANGLADESH","Dhaka"},
|
||||
{"BHUTAN","Thimphu"},
|
||||
{"BRUNEI","Bandar Seri Begawan"},
|
||||
{"CAMBODIA","Phnom Penh"},
|
||||
{"CHINA","Beijing"}, {"CYPRUS","Nicosia"},
|
||||
{"INDIA","New Delhi"}, {"INDONESIA","Jakarta"},
|
||||
{"IRAN","Tehran"}, {"IRAQ","Baghdad"},
|
||||
{"ISRAEL","Jerusalem"}, {"JAPAN","Tokyo"},
|
||||
{"JORDAN","Amman"}, {"KUWAIT","Kuwait City"},
|
||||
{"LAOS","Vientiane"}, {"LEBANON","Beirut"},
|
||||
{"MALAYSIA","Kuala Lumpur"}, {"THE MALDIVES","Male"},
|
||||
{"CHINA","Beijing"},
|
||||
{"CYPRUS","Nicosia"},
|
||||
{"INDIA","New Delhi"},
|
||||
{"INDONESIA","Jakarta"},
|
||||
{"IRAN","Tehran"},
|
||||
{"IRAQ","Baghdad"},
|
||||
{"ISRAEL","Jerusalem"},
|
||||
{"JAPAN","Tokyo"},
|
||||
{"JORDAN","Amman"},
|
||||
{"KUWAIT","Kuwait City"},
|
||||
{"LAOS","Vientiane"},
|
||||
{"LEBANON","Beirut"},
|
||||
{"MALAYSIA","Kuala Lumpur"},
|
||||
{"THE MALDIVES","Male"},
|
||||
{"MONGOLIA","Ulan Bator"},
|
||||
{"MYANMAR (BURMA)","Rangoon"},
|
||||
{"NEPAL","Katmandu"}, {"NORTH KOREA","P'yongyang"},
|
||||
{"OMAN","Muscat"}, {"PAKISTAN","Islamabad"},
|
||||
{"PHILIPPINES","Manila"}, {"QATAR","Doha"},
|
||||
{"SAUDI ARABIA","Riyadh"}, {"SINGAPORE","Singapore"},
|
||||
{"SOUTH KOREA","Seoul"}, {"SRI LANKA","Colombo"},
|
||||
{"NEPAL","Katmandu"},
|
||||
{"NORTH KOREA","P'yongyang"},
|
||||
{"OMAN","Muscat"},
|
||||
{"PAKISTAN","Islamabad"},
|
||||
{"PHILIPPINES","Manila"},
|
||||
{"QATAR","Doha"},
|
||||
{"SAUDI ARABIA","Riyadh"},
|
||||
{"SINGAPORE","Singapore"},
|
||||
{"SOUTH KOREA","Seoul"},
|
||||
{"SRI LANKA","Colombo"},
|
||||
{"SYRIA","Damascus"},
|
||||
{"TAIWAN (REPUBLIC OF CHINA)","Taipei"},
|
||||
{"THAILAND","Bangkok"}, {"TURKEY","Ankara"},
|
||||
{"THAILAND","Bangkok"},
|
||||
{"TURKEY","Ankara"},
|
||||
{"UNITED ARAB EMIRATES","Abu Dhabi"},
|
||||
{"VIETNAM","Hanoi"}, {"YEMEN","Sana'a"},
|
||||
{"VIETNAM","Hanoi"},
|
||||
{"YEMEN","Sana'a"},
|
||||
// Australia and Oceania
|
||||
{"AUSTRALIA","Canberra"}, {"FIJI","Suva"},
|
||||
{"AUSTRALIA","Canberra"},
|
||||
{"FIJI","Suva"},
|
||||
{"KIRIBATI","Bairiki"},
|
||||
{"MARSHALL ISLANDS","Dalap-Uliga-Darrit"},
|
||||
{"MICRONESIA","Palikir"}, {"NAURU","Yaren"},
|
||||
{"NEW ZEALAND","Wellington"}, {"PALAU","Koror"},
|
||||
{"MICRONESIA","Palikir"},
|
||||
{"NAURU","Yaren"},
|
||||
{"NEW ZEALAND","Wellington"},
|
||||
{"PALAU","Koror"},
|
||||
{"PAPUA NEW GUINEA","Port Moresby"},
|
||||
{"SOLOMON ISLANDS","Honaira"}, {"TONGA","Nuku'alofa"},
|
||||
{"TUVALU","Fongafale"}, {"VANUATU","< Port-Vila"},
|
||||
{"SOLOMON ISLANDS","Honaira"},
|
||||
{"TONGA","Nuku'alofa"},
|
||||
{"TUVALU","Fongafale"},
|
||||
{"VANUATU","Port Vila"},
|
||||
{"WESTERN SAMOA","Apia"},
|
||||
// Eastern Europe and former USSR
|
||||
{"ARMENIA","Yerevan"}, {"AZERBAIJAN","Baku"},
|
||||
{"ARMENIA","Yerevan"},
|
||||
{"AZERBAIJAN","Baku"},
|
||||
{"BELARUS (BYELORUSSIA)","Minsk"},
|
||||
{"BULGARIA","Sofia"}, {"GEORGIA","Tbilisi"},
|
||||
{"KAZAKSTAN","Almaty"}, {"KYRGYZSTAN","Alma-Ata"},
|
||||
{"MOLDOVA","Chisinau"}, {"RUSSIA","Moscow"},
|
||||
{"BULGARIA","Sofia"},
|
||||
{"GEORGIA","Tbilisi"},
|
||||
{"KAZAKSTAN","Almaty"},
|
||||
{"KYRGYZSTAN","Alma-Ata"},
|
||||
{"MOLDOVA","Chisinau"},
|
||||
{"RUSSIA","Moscow"},
|
||||
{"TAJIKISTAN","Dushanbe"},
|
||||
{"TURKMENISTAN","Ashkabad"},
|
||||
{"UKRAINE","Kyiv"}, {"UZBEKISTAN","Tashkent"},
|
||||
{"UKRAINE","Kyiv"},
|
||||
{"UZBEKISTAN","Tashkent"},
|
||||
// Europe
|
||||
{"ALBANIA","Tirana"}, {"ANDORRA","Andorra la Vella"},
|
||||
{"AUSTRIA","Vienna"}, {"BELGIUM","Brussels"},
|
||||
{"BOSNIA","---"}, {"HERZEGOVINA","Sarajevo"},
|
||||
{"CROATIA","Zagreb"}, {"CZECH REPUBLIC","Prague"},
|
||||
{"DENMARK","Copenhagen"}, {"ESTONIA","Tallinn"},
|
||||
{"FINLAND","Helsinki"}, {"FRANCE","Paris"},
|
||||
{"GERMANY","Berlin"}, {"GREECE","Athens"},
|
||||
{"HUNGARY","Budapest"}, {"ICELAND","Reykjavik"},
|
||||
{"IRELAND","Dublin"}, {"ITALY","Rome"},
|
||||
{"LATVIA","Riga"}, {"LIECHTENSTEIN","Vaduz"},
|
||||
{"LITHUANIA","Vilnius"}, {"LUXEMBOURG","Luxembourg"},
|
||||
{"MACEDONIA","Skopje"}, {"MALTA","Valletta"},
|
||||
{"MONACO","Monaco"}, {"MONTENEGRO","Podgorica"},
|
||||
{"THE NETHERLANDS","Amsterdam"}, {"NORWAY","Oslo"},
|
||||
{"POLAND","Warsaw"}, {"PORTUGAL","Lisbon"},
|
||||
{"ROMANIA","Bucharest"}, {"SAN MARINO","San Marino"},
|
||||
{"SERBIA","Belgrade"}, {"SLOVAKIA","Bratislava"},
|
||||
{"SLOVENIA","Ljuijana"}, {"SPAIN","Madrid"},
|
||||
{"SWEDEN","Stockholm"}, {"SWITZERLAND","Berne"},
|
||||
{"UNITED KINGDOM","London"}, {"VATICAN CITY","---"},
|
||||
{"ALBANIA","Tirana"},
|
||||
{"ANDORRA","Andorra la Vella"},
|
||||
{"AUSTRIA","Vienna"},
|
||||
{"BELGIUM","Brussels"},
|
||||
{"BOSNIA-HERZEGOVINA","Sarajevo"},
|
||||
{"CROATIA","Zagreb"},
|
||||
{"CZECH REPUBLIC","Prague"},
|
||||
{"DENMARK","Copenhagen"},
|
||||
{"ESTONIA","Tallinn"},
|
||||
{"FINLAND","Helsinki"},
|
||||
{"FRANCE","Paris"},
|
||||
{"GERMANY","Berlin"},
|
||||
{"GREECE","Athens"},
|
||||
{"HUNGARY","Budapest"},
|
||||
{"ICELAND","Reykjavik"},
|
||||
{"IRELAND","Dublin"},
|
||||
{"ITALY","Rome"},
|
||||
{"LATVIA","Riga"},
|
||||
{"LIECHTENSTEIN","Vaduz"},
|
||||
{"LITHUANIA","Vilnius"},
|
||||
{"LUXEMBOURG","Luxembourg"},
|
||||
{"MACEDONIA","Skopje"},
|
||||
{"MALTA","Valletta"},
|
||||
{"MONACO","Monaco"},
|
||||
{"MONTENEGRO","Podgorica"},
|
||||
{"THE NETHERLANDS","Amsterdam"},
|
||||
{"NORWAY","Oslo"},
|
||||
{"POLAND","Warsaw"},
|
||||
{"PORTUGAL","Lisbon"},
|
||||
{"ROMANIA","Bucharest"},
|
||||
{"SAN MARINO","San Marino"},
|
||||
{"SERBIA","Belgrade"},
|
||||
{"SLOVAKIA","Bratislava"},
|
||||
{"SLOVENIA","Ljuijana"},
|
||||
{"SPAIN","Madrid"},
|
||||
{"SWEDEN","Stockholm"},
|
||||
{"SWITZERLAND","Berne"},
|
||||
{"UNITED KINGDOM","London"},
|
||||
{"VATICAN CITY","Vatican City"},
|
||||
// North and Central America
|
||||
{"ANTIGUA AND BARBUDA","Saint John's"},
|
||||
{"BAHAMAS","Nassau"},
|
||||
{"BARBADOS","Bridgetown"}, {"BELIZE","Belmopan"},
|
||||
{"CANADA","Ottawa"}, {"COSTA RICA","San Jose"},
|
||||
{"CUBA","Havana"}, {"DOMINICA","Roseau"},
|
||||
{"BARBADOS","Bridgetown"},
|
||||
{"BELIZE","Belmopan"},
|
||||
{"CANADA","Ottawa"},
|
||||
{"COSTA RICA","San Jose"},
|
||||
{"CUBA","Havana"},
|
||||
{"DOMINICA","Roseau"},
|
||||
{"DOMINICAN REPUBLIC","Santo Domingo"},
|
||||
{"EL SALVADOR","San Salvador"},
|
||||
{"GRENADA","Saint George's"},
|
||||
{"GUATEMALA","Guatemala City"},
|
||||
{"HAITI","Port-au-Prince"},
|
||||
{"HONDURAS","Tegucigalpa"}, {"JAMAICA","Kingston"},
|
||||
{"MEXICO","Mexico City"}, {"NICARAGUA","Managua"},
|
||||
{"PANAMA","Panama City"}, {"ST. KITTS","---"},
|
||||
{"NEVIS","Basseterre"}, {"ST. LUCIA","Castries"},
|
||||
{"HONDURAS","Tegucigalpa"},
|
||||
{"JAMAICA","Kingston"},
|
||||
{"MEXICO","Mexico City"},
|
||||
{"NICARAGUA","Managua"},
|
||||
{"PANAMA","Panama City"},
|
||||
{"ST. KITTS AND NEVIS","Basseterre"},
|
||||
{"ST. LUCIA","Castries"},
|
||||
{"ST. VINCENT AND THE GRENADINES","Kingstown"},
|
||||
{"UNITED STATES OF AMERICA","Washington, D.C."},
|
||||
// South America
|
||||
{"ARGENTINA","Buenos Aires"},
|
||||
{"BOLIVIA","Sucre (legal)/La Paz(administrative)"},
|
||||
{"BRAZIL","Brasilia"}, {"CHILE","Santiago"},
|
||||
{"COLOMBIA","Bogota"}, {"ECUADOR","Quito"},
|
||||
{"GUYANA","Georgetown"}, {"PARAGUAY","Asuncion"},
|
||||
{"PERU","Lima"}, {"SURINAME","Paramaribo"},
|
||||
{"BRAZIL","Brasilia"},
|
||||
{"CHILE","Santiago"},
|
||||
{"COLOMBIA","Bogota"},
|
||||
{"ECUADOR","Quito"},
|
||||
{"GUYANA","Georgetown"},
|
||||
{"PARAGUAY","Asuncion"},
|
||||
{"PERU","Lima"},
|
||||
{"SURINAME","Paramaribo"},
|
||||
{"TRINIDAD AND TOBAGO","Port of Spain"},
|
||||
{"URUGUAY","Montevideo"}, {"VENEZUELA","Caracas"},
|
||||
{"URUGUAY","Montevideo"},
|
||||
{"VENEZUELA","Caracas"},
|
||||
};
|
||||
// Use AbstractMap by implementing entrySet()
|
||||
private static class FlyweightMap
|
||||
|
234
onjava/HTMLColors.java
Normal file
234
onjava/HTMLColors.java
Normal file
@ -0,0 +1,234 @@
|
||||
// onjava/HTMLColors.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Sample data for collection examples
|
||||
package onjava;
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class HTMLColors {
|
||||
public static final Object[][] ARRAY = {
|
||||
{ 0xF0F8FF, "AliceBlue" },
|
||||
{ 0xFAEBD7, "AntiqueWhite" },
|
||||
{ 0x7FFFD4, "Aquamarine" },
|
||||
{ 0xF0FFFF, "Azure" },
|
||||
{ 0xF5F5DC, "Beige" },
|
||||
{ 0xFFE4C4, "Bisque" },
|
||||
{ 0x000000, "Black" },
|
||||
{ 0xFFEBCD, "BlanchedAlmond" },
|
||||
{ 0x0000FF, "Blue" },
|
||||
{ 0x8A2BE2, "BlueViolet" },
|
||||
{ 0xA52A2A, "Brown" },
|
||||
{ 0xDEB887, "BurlyWood" },
|
||||
{ 0x5F9EA0, "CadetBlue" },
|
||||
{ 0x7FFF00, "Chartreuse" },
|
||||
{ 0xD2691E, "Chocolate" },
|
||||
{ 0xFF7F50, "Coral" },
|
||||
{ 0x6495ED, "CornflowerBlue" },
|
||||
{ 0xFFF8DC, "Cornsilk" },
|
||||
{ 0xDC143C, "Crimson" },
|
||||
{ 0x00FFFF, "Cyan" },
|
||||
{ 0x00008B, "DarkBlue" },
|
||||
{ 0x008B8B, "DarkCyan" },
|
||||
{ 0xB8860B, "DarkGoldenRod" },
|
||||
{ 0xA9A9A9, "DarkGray" },
|
||||
{ 0x006400, "DarkGreen" },
|
||||
{ 0xBDB76B, "DarkKhaki" },
|
||||
{ 0x8B008B, "DarkMagenta" },
|
||||
{ 0x556B2F, "DarkOliveGreen" },
|
||||
{ 0xFF8C00, "DarkOrange" },
|
||||
{ 0x9932CC, "DarkOrchid" },
|
||||
{ 0x8B0000, "DarkRed" },
|
||||
{ 0xE9967A, "DarkSalmon" },
|
||||
{ 0x8FBC8F, "DarkSeaGreen" },
|
||||
{ 0x483D8B, "DarkSlateBlue" },
|
||||
{ 0x2F4F4F, "DarkSlateGray" },
|
||||
{ 0x00CED1, "DarkTurquoise" },
|
||||
{ 0x9400D3, "DarkViolet" },
|
||||
{ 0xFF1493, "DeepPink" },
|
||||
{ 0x00BFFF, "DeepSkyBlue" },
|
||||
{ 0x696969, "DimGray" },
|
||||
{ 0x1E90FF, "DodgerBlue" },
|
||||
{ 0xB22222, "FireBrick" },
|
||||
{ 0xFFFAF0, "FloralWhite" },
|
||||
{ 0x228B22, "ForestGreen" },
|
||||
{ 0xDCDCDC, "Gainsboro" },
|
||||
{ 0xF8F8FF, "GhostWhite" },
|
||||
{ 0xFFD700, "Gold" },
|
||||
{ 0xDAA520, "GoldenRod" },
|
||||
{ 0x808080, "Gray" },
|
||||
{ 0x008000, "Green" },
|
||||
{ 0xADFF2F, "GreenYellow" },
|
||||
{ 0xF0FFF0, "HoneyDew" },
|
||||
{ 0xFF69B4, "HotPink" },
|
||||
{ 0xCD5C5C, "IndianRed" },
|
||||
{ 0x4B0082, "Indigo" },
|
||||
{ 0xFFFFF0, "Ivory" },
|
||||
{ 0xF0E68C, "Khaki" },
|
||||
{ 0xE6E6FA, "Lavender" },
|
||||
{ 0xFFF0F5, "LavenderBlush" },
|
||||
{ 0x7CFC00, "LawnGreen" },
|
||||
{ 0xFFFACD, "LemonChiffon" },
|
||||
{ 0xADD8E6, "LightBlue" },
|
||||
{ 0xF08080, "LightCoral" },
|
||||
{ 0xE0FFFF, "LightCyan" },
|
||||
{ 0xFAFAD2, "LightGoldenRodYellow" },
|
||||
{ 0xD3D3D3, "LightGray" },
|
||||
{ 0x90EE90, "LightGreen" },
|
||||
{ 0xFFB6C1, "LightPink" },
|
||||
{ 0xFFA07A, "LightSalmon" },
|
||||
{ 0x20B2AA, "LightSeaGreen" },
|
||||
{ 0x87CEFA, "LightSkyBlue" },
|
||||
{ 0x778899, "LightSlateGray" },
|
||||
{ 0xB0C4DE, "LightSteelBlue" },
|
||||
{ 0xFFFFE0, "LightYellow" },
|
||||
{ 0x00FF00, "Lime" },
|
||||
{ 0x32CD32, "LimeGreen" },
|
||||
{ 0xFAF0E6, "Linen" },
|
||||
{ 0xFF00FF, "Magenta" },
|
||||
{ 0x800000, "Maroon" },
|
||||
{ 0x66CDAA, "MediumAquaMarine" },
|
||||
{ 0x0000CD, "MediumBlue" },
|
||||
{ 0xBA55D3, "MediumOrchid" },
|
||||
{ 0x9370DB, "MediumPurple" },
|
||||
{ 0x3CB371, "MediumSeaGreen" },
|
||||
{ 0x7B68EE, "MediumSlateBlue" },
|
||||
{ 0x00FA9A, "MediumSpringGreen" },
|
||||
{ 0x48D1CC, "MediumTurquoise" },
|
||||
{ 0xC71585, "MediumVioletRed" },
|
||||
{ 0x191970, "MidnightBlue" },
|
||||
{ 0xF5FFFA, "MintCream" },
|
||||
{ 0xFFE4E1, "MistyRose" },
|
||||
{ 0xFFE4B5, "Moccasin" },
|
||||
{ 0xFFDEAD, "NavajoWhite" },
|
||||
{ 0x000080, "Navy" },
|
||||
{ 0xFDF5E6, "OldLace" },
|
||||
{ 0x808000, "Olive" },
|
||||
{ 0x6B8E23, "OliveDrab" },
|
||||
{ 0xFFA500, "Orange" },
|
||||
{ 0xFF4500, "OrangeRed" },
|
||||
{ 0xDA70D6, "Orchid" },
|
||||
{ 0xEEE8AA, "PaleGoldenRod" },
|
||||
{ 0x98FB98, "PaleGreen" },
|
||||
{ 0xAFEEEE, "PaleTurquoise" },
|
||||
{ 0xDB7093, "PaleVioletRed" },
|
||||
{ 0xFFEFD5, "PapayaWhip" },
|
||||
{ 0xFFDAB9, "PeachPuff" },
|
||||
{ 0xCD853F, "Peru" },
|
||||
{ 0xFFC0CB, "Pink" },
|
||||
{ 0xDDA0DD, "Plum" },
|
||||
{ 0xB0E0E6, "PowderBlue" },
|
||||
{ 0x800080, "Purple" },
|
||||
{ 0xFF0000, "Red" },
|
||||
{ 0xBC8F8F, "RosyBrown" },
|
||||
{ 0x4169E1, "RoyalBlue" },
|
||||
{ 0x8B4513, "SaddleBrown" },
|
||||
{ 0xFA8072, "Salmon" },
|
||||
{ 0xF4A460, "SandyBrown" },
|
||||
{ 0x2E8B57, "SeaGreen" },
|
||||
{ 0xFFF5EE, "SeaShell" },
|
||||
{ 0xA0522D, "Sienna" },
|
||||
{ 0xC0C0C0, "Silver" },
|
||||
{ 0x87CEEB, "SkyBlue" },
|
||||
{ 0x6A5ACD, "SlateBlue" },
|
||||
{ 0x708090, "SlateGray" },
|
||||
{ 0xFFFAFA, "Snow" },
|
||||
{ 0x00FF7F, "SpringGreen" },
|
||||
{ 0x4682B4, "SteelBlue" },
|
||||
{ 0xD2B48C, "Tan" },
|
||||
{ 0x008080, "Teal" },
|
||||
{ 0xD8BFD8, "Thistle" },
|
||||
{ 0xFF6347, "Tomato" },
|
||||
{ 0x40E0D0, "Turquoise" },
|
||||
{ 0xEE82EE, "Violet" },
|
||||
{ 0xF5DEB3, "Wheat" },
|
||||
{ 0xFFFFFF, "White" },
|
||||
{ 0xF5F5F5, "WhiteSmoke" },
|
||||
{ 0xFFFF00, "Yellow" },
|
||||
{ 0x9ACD32, "YellowGreen" },
|
||||
};
|
||||
public static final Map<Integer,String> MAP =
|
||||
Arrays.stream(ARRAY)
|
||||
.collect(Collectors.toMap(
|
||||
element -> (Integer)element[0],
|
||||
element -> (String)element[1],
|
||||
(v1, v2) -> { // Merge function
|
||||
throw new IllegalStateException();
|
||||
},
|
||||
LinkedHashMap::new
|
||||
));
|
||||
// Inversion only works if values are unique:
|
||||
public static <V, K> Map<V, K>
|
||||
invert(Map<K, V> map) {
|
||||
return map.entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getValue,
|
||||
Map.Entry::getKey,
|
||||
(v1, v2) -> {
|
||||
throw new IllegalStateException();
|
||||
},
|
||||
LinkedHashMap::new
|
||||
));
|
||||
}
|
||||
public static final Map<String,Integer>
|
||||
INVMAP = invert(MAP);
|
||||
// Look up RGB value given a name:
|
||||
public static Integer rgb(String colorName) {
|
||||
return INVMAP.get(colorName);
|
||||
}
|
||||
public static final List<String> LIST =
|
||||
Arrays.stream(ARRAY)
|
||||
.map(item -> (String)item[1])
|
||||
.collect(Collectors.toList());
|
||||
public static final List<Integer> RGBLIST =
|
||||
Arrays.stream(ARRAY)
|
||||
.map(item -> (Integer)item[0])
|
||||
.collect(Collectors.toList());
|
||||
public static void show(Map.Entry<Integer,String> e) {
|
||||
System.out.format(
|
||||
"0x%06X: %s\n", e.getKey(), e.getValue());
|
||||
}
|
||||
public static void
|
||||
show(Map<Integer,String> m, int count) {
|
||||
m.entrySet().stream()
|
||||
.limit(count)
|
||||
.forEach(e -> show(e));
|
||||
}
|
||||
public static void show(Map<Integer,String> m) {
|
||||
show(m, m.size());
|
||||
}
|
||||
public static
|
||||
void show(List<String> lst, int count) {
|
||||
lst.stream()
|
||||
.limit(count)
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
public static void show(List<String> lst) {
|
||||
show(lst, lst.size());
|
||||
}
|
||||
public static
|
||||
void showrgb(List<Integer> lst, int count) {
|
||||
lst.stream()
|
||||
.limit(count)
|
||||
.forEach(n -> System.out.format("0x%06X\n", n));
|
||||
}
|
||||
public static void showrgb(List<Integer> lst) {
|
||||
showrgb(lst, lst.size());
|
||||
}
|
||||
public static
|
||||
void show_inv(Map<String,Integer> m, int count) {
|
||||
m.entrySet().stream()
|
||||
.limit(count)
|
||||
.forEach(e ->
|
||||
System.out.format(
|
||||
"%-20s 0x%06X\n", e.getKey(), e.getValue()));
|
||||
}
|
||||
public static void show_inv(Map<String,Integer> m) {
|
||||
show_inv(m, m.size());
|
||||
}
|
||||
public static void border() {
|
||||
System.out.println("------------------------------");
|
||||
}
|
||||
}
|
38
understandingcollections/FunctionalMap.java
Normal file
38
understandingcollections/FunctionalMap.java
Normal file
@ -0,0 +1,38 @@
|
||||
// understandingcollections/FunctionalMap.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Functional operations on a Map
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
import java.util.concurrent.*;
|
||||
import static onjava.HTMLColors.*;
|
||||
|
||||
public class FunctionalMap {
|
||||
public static void main(String[] args) {
|
||||
MAP.entrySet().stream()
|
||||
.map(Map.Entry::getValue)
|
||||
.filter(v -> v.startsWith("Dark"))
|
||||
.map(v -> v.replaceFirst("Dark", "Hot"))
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
HotBlue
|
||||
HotCyan
|
||||
HotGoldenRod
|
||||
HotGray
|
||||
HotGreen
|
||||
HotKhaki
|
||||
HotMagenta
|
||||
HotOliveGreen
|
||||
HotOrange
|
||||
HotOrchid
|
||||
HotRed
|
||||
HotSalmon
|
||||
HotSeaGreen
|
||||
HotSlateBlue
|
||||
HotSlateGray
|
||||
HotTurquoise
|
||||
HotViolet
|
||||
*/
|
103
understandingcollections/HTMLColorTest.java
Normal file
103
understandingcollections/HTMLColorTest.java
Normal file
@ -0,0 +1,103 @@
|
||||
// understandingcollections/HTMLColorTest.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import static onjava.HTMLColors.*;
|
||||
|
||||
public class HTMLColorTest {
|
||||
static final int DISPLAYSIZE = 20;
|
||||
public static void main(String[] args) {
|
||||
show(MAP, DISPLAYSIZE);
|
||||
border();
|
||||
show_inv(INVMAP, DISPLAYSIZE);
|
||||
border();
|
||||
show(LIST, DISPLAYSIZE);
|
||||
border();
|
||||
showrgb(RGBLIST, DISPLAYSIZE);
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
0xF0F8FF: AliceBlue
|
||||
0xFAEBD7: AntiqueWhite
|
||||
0x7FFFD4: Aquamarine
|
||||
0xF0FFFF: Azure
|
||||
0xF5F5DC: Beige
|
||||
0xFFE4C4: Bisque
|
||||
0x000000: Black
|
||||
0xFFEBCD: BlanchedAlmond
|
||||
0x0000FF: Blue
|
||||
0x8A2BE2: BlueViolet
|
||||
0xA52A2A: Brown
|
||||
0xDEB887: BurlyWood
|
||||
0x5F9EA0: CadetBlue
|
||||
0x7FFF00: Chartreuse
|
||||
0xD2691E: Chocolate
|
||||
0xFF7F50: Coral
|
||||
0x6495ED: CornflowerBlue
|
||||
0xFFF8DC: Cornsilk
|
||||
0xDC143C: Crimson
|
||||
0x00FFFF: Cyan
|
||||
------------------------------
|
||||
AliceBlue 0xF0F8FF
|
||||
AntiqueWhite 0xFAEBD7
|
||||
Aquamarine 0x7FFFD4
|
||||
Azure 0xF0FFFF
|
||||
Beige 0xF5F5DC
|
||||
Bisque 0xFFE4C4
|
||||
Black 0x000000
|
||||
BlanchedAlmond 0xFFEBCD
|
||||
Blue 0x0000FF
|
||||
BlueViolet 0x8A2BE2
|
||||
Brown 0xA52A2A
|
||||
BurlyWood 0xDEB887
|
||||
CadetBlue 0x5F9EA0
|
||||
Chartreuse 0x7FFF00
|
||||
Chocolate 0xD2691E
|
||||
Coral 0xFF7F50
|
||||
CornflowerBlue 0x6495ED
|
||||
Cornsilk 0xFFF8DC
|
||||
Crimson 0xDC143C
|
||||
Cyan 0x00FFFF
|
||||
------------------------------
|
||||
AliceBlue
|
||||
AntiqueWhite
|
||||
Aquamarine
|
||||
Azure
|
||||
Beige
|
||||
Bisque
|
||||
Black
|
||||
BlanchedAlmond
|
||||
Blue
|
||||
BlueViolet
|
||||
Brown
|
||||
BurlyWood
|
||||
CadetBlue
|
||||
Chartreuse
|
||||
Chocolate
|
||||
Coral
|
||||
CornflowerBlue
|
||||
Cornsilk
|
||||
Crimson
|
||||
Cyan
|
||||
------------------------------
|
||||
0xF0F8FF
|
||||
0xFAEBD7
|
||||
0x7FFFD4
|
||||
0xF0FFFF
|
||||
0xF5F5DC
|
||||
0xFFE4C4
|
||||
0x000000
|
||||
0xFFEBCD
|
||||
0x0000FF
|
||||
0x8A2BE2
|
||||
0xA52A2A
|
||||
0xDEB887
|
||||
0x5F9EA0
|
||||
0x7FFF00
|
||||
0xD2691E
|
||||
0xFF7F50
|
||||
0x6495ED
|
||||
0xFFF8DC
|
||||
0xDC143C
|
||||
0x00FFFF
|
||||
*/
|
@ -4,9 +4,12 @@
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Things you can do with Lists
|
||||
import java.util.*;
|
||||
import onjava.*;
|
||||
import onjava.HTMLColors;
|
||||
|
||||
public class ListOps {
|
||||
// Create a short list for testing:
|
||||
static final List<String> LIST =
|
||||
HTMLColors.LIST.subList(0, 10);
|
||||
private static boolean b;
|
||||
private static String s;
|
||||
private static int i;
|
||||
@ -16,12 +19,12 @@ public class ListOps {
|
||||
a.add(1, "x"); // Add at location 1
|
||||
a.add("x"); // Add at end
|
||||
// Add a collection:
|
||||
a.addAll(Countries.names(25));
|
||||
a.addAll(LIST);
|
||||
// Add a collection starting at location 3:
|
||||
a.addAll(3, Countries.names(25));
|
||||
a.addAll(3, LIST);
|
||||
b = a.contains("1"); // Is it in there?
|
||||
// Is the entire collection in there?
|
||||
b = a.containsAll(Countries.names(25));
|
||||
b = a.containsAll(LIST);
|
||||
// Lists allow random access, which is cheap
|
||||
// for ArrayList, expensive for LinkedList:
|
||||
s = a.get(1); // Get (typed) object at location 1
|
||||
@ -36,9 +39,9 @@ public class ListOps {
|
||||
a.set(1, "y"); // Set location 1 to "y"
|
||||
// Keep everything that's in the argument
|
||||
// (the intersection of the two sets):
|
||||
a.retainAll(Countries.names(25));
|
||||
a.retainAll(LIST);
|
||||
// Remove everything that's in the argument:
|
||||
a.removeAll(Countries.names(25));
|
||||
a.removeAll(LIST);
|
||||
i = a.size(); // How big is it?
|
||||
a.clear(); // Remove all elements
|
||||
}
|
||||
@ -65,7 +68,7 @@ public class ListOps {
|
||||
}
|
||||
public static void testVisual(List<String> a) {
|
||||
System.out.println(a);
|
||||
List<String> b = Countries.names(25);
|
||||
List<String> b = LIST;
|
||||
System.out.println("b = " + b);
|
||||
a.addAll(b);
|
||||
a.addAll(b);
|
||||
@ -90,7 +93,7 @@ public class ListOps {
|
||||
// There are some things that only LinkedLists can do:
|
||||
public static void testLinkedList() {
|
||||
LinkedList<String> ll = new LinkedList<>();
|
||||
ll.addAll(Countries.names(25));
|
||||
ll.addAll(LIST);
|
||||
System.out.println(ll);
|
||||
// Treat it like a stack, pushing:
|
||||
ll.addFirst("one");
|
||||
@ -108,39 +111,15 @@ public class ListOps {
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
// Make and fill a new list each time:
|
||||
basicTest(
|
||||
new LinkedList<>(Countries.names(25)));
|
||||
basicTest(
|
||||
new ArrayList<>(Countries.names(25)));
|
||||
iterMotion(
|
||||
new LinkedList<>(Countries.names(25)));
|
||||
iterMotion(
|
||||
new ArrayList<>(Countries.names(25)));
|
||||
iterManipulation(
|
||||
new LinkedList<>(Countries.names(25)));
|
||||
iterManipulation(
|
||||
new ArrayList<>(Countries.names(25)));
|
||||
testVisual(
|
||||
new LinkedList<>(Countries.names(25)));
|
||||
basicTest(new LinkedList<>(LIST));
|
||||
basicTest(new ArrayList<>(LIST));
|
||||
iterMotion(new LinkedList<>(LIST));
|
||||
iterMotion(new ArrayList<>(LIST));
|
||||
iterManipulation(new LinkedList<>(LIST));
|
||||
iterManipulation(new ArrayList<>(LIST));
|
||||
testVisual(new LinkedList<>(LIST));
|
||||
testLinkedList();
|
||||
}
|
||||
}
|
||||
/* Output: (First and Last 2 Lines)
|
||||
[ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI,
|
||||
CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD,
|
||||
COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA,
|
||||
ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA,
|
||||
BISSAU, COTE D'IVOIR (IVORY COAST), KENYA, LESOTHO]
|
||||
b = [ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO,
|
||||
BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC,
|
||||
CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA,
|
||||
ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA,
|
||||
BISSAU, COTE D'IVOIR (IVORY COAST), KENYA, LESOTHO]
|
||||
...________...________...________...________...
|
||||
LESOTHO
|
||||
[ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI,
|
||||
CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD,
|
||||
COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA,
|
||||
ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA,
|
||||
BISSAU, COTE D'IVOIR (IVORY COAST), KENYA]
|
||||
/* Output:
|
||||
*/
|
||||
|
96
understandingcollections/NavMap.java
Normal file
96
understandingcollections/NavMap.java
Normal file
@ -0,0 +1,96 @@
|
||||
// understandingcollections/NavMap.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// NavigableMap produces pieces of a Map
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import static onjava.HTMLColors.*;
|
||||
|
||||
public class NavMap {
|
||||
public static final
|
||||
NavigableMap<Integer,String> COLORS =
|
||||
new ConcurrentSkipListMap<>(MAP);
|
||||
public static void main(String[] args) {
|
||||
show(COLORS.firstEntry());
|
||||
border();
|
||||
show(COLORS.lastEntry());
|
||||
border();
|
||||
NavigableMap<Integer, String> toLime =
|
||||
COLORS.headMap(rgb("Lime"), true);
|
||||
show(toLime);
|
||||
border();
|
||||
show(COLORS.ceilingEntry(rgb("DeepSkyBlue") - 1));
|
||||
border();
|
||||
show(COLORS.floorEntry(rgb("DeepSkyBlue") - 1));
|
||||
border();
|
||||
show(toLime.descendingMap());
|
||||
border();
|
||||
show(COLORS.tailMap(rgb("MistyRose"), true));
|
||||
border();
|
||||
show(COLORS.subMap(
|
||||
rgb("Orchid"), true,
|
||||
rgb("DarkSalmon"), false));
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
0x000000: Black
|
||||
------------------------------
|
||||
0xFFFFFF: White
|
||||
------------------------------
|
||||
0x000000: Black
|
||||
0x000080: Navy
|
||||
0x00008B: DarkBlue
|
||||
0x0000CD: MediumBlue
|
||||
0x0000FF: Blue
|
||||
0x006400: DarkGreen
|
||||
0x008000: Green
|
||||
0x008080: Teal
|
||||
0x008B8B: DarkCyan
|
||||
0x00BFFF: DeepSkyBlue
|
||||
0x00CED1: DarkTurquoise
|
||||
0x00FA9A: MediumSpringGreen
|
||||
0x00FF00: Lime
|
||||
------------------------------
|
||||
0x00BFFF: DeepSkyBlue
|
||||
------------------------------
|
||||
0x008B8B: DarkCyan
|
||||
------------------------------
|
||||
0x00FF00: Lime
|
||||
0x00FA9A: MediumSpringGreen
|
||||
0x00CED1: DarkTurquoise
|
||||
0x00BFFF: DeepSkyBlue
|
||||
0x008B8B: DarkCyan
|
||||
0x008080: Teal
|
||||
0x008000: Green
|
||||
0x006400: DarkGreen
|
||||
0x0000FF: Blue
|
||||
0x0000CD: MediumBlue
|
||||
0x00008B: DarkBlue
|
||||
0x000080: Navy
|
||||
0x000000: Black
|
||||
------------------------------
|
||||
0xFFE4E1: MistyRose
|
||||
0xFFEBCD: BlanchedAlmond
|
||||
0xFFEFD5: PapayaWhip
|
||||
0xFFF0F5: LavenderBlush
|
||||
0xFFF5EE: SeaShell
|
||||
0xFFF8DC: Cornsilk
|
||||
0xFFFACD: LemonChiffon
|
||||
0xFFFAF0: FloralWhite
|
||||
0xFFFAFA: Snow
|
||||
0xFFFF00: Yellow
|
||||
0xFFFFE0: LightYellow
|
||||
0xFFFFF0: Ivory
|
||||
0xFFFFFF: White
|
||||
------------------------------
|
||||
0xDA70D6: Orchid
|
||||
0xDAA520: GoldenRod
|
||||
0xDB7093: PaleVioletRed
|
||||
0xDC143C: Crimson
|
||||
0xDCDCDC: Gainsboro
|
||||
0xDDA0DD: Plum
|
||||
0xDEB887: BurlyWood
|
||||
0xE0FFFF: LightCyan
|
||||
0xE6E6FA: Lavender
|
||||
*/
|
92
understandingcollections/SetOrder.java
Normal file
92
understandingcollections/SetOrder.java
Normal file
@ -0,0 +1,92 @@
|
||||
// understandingcollections/SetOrder.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import java.util.*;
|
||||
import onjava.HTMLColors;
|
||||
|
||||
public class SetOrder {
|
||||
static String[] sets = {
|
||||
"java.util.HashSet",
|
||||
"java.util.TreeSet",
|
||||
"java.util.concurrent.ConcurrentSkipListSet",
|
||||
"java.util.LinkedHashSet",
|
||||
"java.util.concurrent.CopyOnWriteArraySet",
|
||||
};
|
||||
static List<String> RLIST =
|
||||
new ArrayList<>(HTMLColors.LIST);
|
||||
static {
|
||||
Collections.reverse(RLIST);
|
||||
}
|
||||
public static void
|
||||
main(String[] args) throws Exception {
|
||||
for(String type: sets) {
|
||||
System.out.format("[-> %s <-]\n",
|
||||
type.substring(type.lastIndexOf('.') + 1));
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<String> set = (Set<String>)
|
||||
Class.forName(type).newInstance();
|
||||
set.addAll(RLIST);
|
||||
set.stream()
|
||||
.limit(10)
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
[-> HashSet <-]
|
||||
MediumOrchid
|
||||
PaleGoldenRod
|
||||
Sienna
|
||||
LightSlateGray
|
||||
DarkSeaGreen
|
||||
Black
|
||||
Gainsboro
|
||||
Orange
|
||||
LightCoral
|
||||
DodgerBlue
|
||||
[-> TreeSet <-]
|
||||
AliceBlue
|
||||
AntiqueWhite
|
||||
Aquamarine
|
||||
Azure
|
||||
Beige
|
||||
Bisque
|
||||
Black
|
||||
BlanchedAlmond
|
||||
Blue
|
||||
BlueViolet
|
||||
[-> ConcurrentSkipListSet <-]
|
||||
AliceBlue
|
||||
AntiqueWhite
|
||||
Aquamarine
|
||||
Azure
|
||||
Beige
|
||||
Bisque
|
||||
Black
|
||||
BlanchedAlmond
|
||||
Blue
|
||||
BlueViolet
|
||||
[-> LinkedHashSet <-]
|
||||
YellowGreen
|
||||
Yellow
|
||||
WhiteSmoke
|
||||
White
|
||||
Wheat
|
||||
Violet
|
||||
Turquoise
|
||||
Tomato
|
||||
Thistle
|
||||
Teal
|
||||
[-> CopyOnWriteArraySet <-]
|
||||
YellowGreen
|
||||
Yellow
|
||||
WhiteSmoke
|
||||
White
|
||||
Wheat
|
||||
Violet
|
||||
Turquoise
|
||||
Tomato
|
||||
Thistle
|
||||
Teal
|
||||
*/
|
@ -5,7 +5,6 @@
|
||||
// Performance differences between Deques
|
||||
package understandingcollections.jmh;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.*;
|
||||
|
||||
@ -37,15 +36,9 @@ public class Deques {
|
||||
private int size;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
try {
|
||||
public void setup() throws Exception {
|
||||
deque = (Deque<String>)
|
||||
Class.forName(type).newInstance();
|
||||
} catch(Exception e) {
|
||||
System.err.println(
|
||||
"-> Cannot create: " + type);
|
||||
System.exit(99);
|
||||
}
|
||||
for(int i = 0; i < size; i++)
|
||||
deque.add(Integer.toString(i));
|
||||
}
|
||||
@ -60,11 +53,11 @@ public class Deques {
|
||||
return deque;
|
||||
}
|
||||
@Benchmark
|
||||
public void pollFirst(Blackhole bh) {
|
||||
bh.consume(deque.pollFirst());
|
||||
public String pollFirst() {
|
||||
return deque.pollFirst();
|
||||
}
|
||||
@Benchmark
|
||||
public void pollLast(Blackhole bh) {
|
||||
bh.consume(deque.pollLast());
|
||||
public String pollLast() {
|
||||
return deque.pollLast();
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@
|
||||
// Performance differences between Lists
|
||||
package understandingcollections.jmh;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@ -36,37 +35,39 @@ public class Lists {
|
||||
})
|
||||
private int size;
|
||||
|
||||
private int middle;
|
||||
private ListIterator<String> it;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
try {
|
||||
public void setup() throws Exception {
|
||||
list = (List<String>)
|
||||
Class.forName(type).newInstance();
|
||||
} catch(Exception e) {
|
||||
System.err.println(
|
||||
"-> Cannot create: " + type);
|
||||
System.exit(99);
|
||||
}
|
||||
for(int i = 0; i < size; i++)
|
||||
list.add(Integer.toString(i));
|
||||
middle = size / 2;
|
||||
it = list.listIterator(middle);
|
||||
}
|
||||
@Benchmark
|
||||
public List<String> add() {
|
||||
list.add(list.size() / 2, "test");
|
||||
public List<String> append() {
|
||||
list.add("test");
|
||||
return list;
|
||||
}
|
||||
@Benchmark
|
||||
public void get(Blackhole bh) {
|
||||
bh.consume(list.get(list.size() / 2));
|
||||
public List<String> insert() {
|
||||
list.add(middle, "test");
|
||||
return list;
|
||||
}
|
||||
@Benchmark
|
||||
public String get() {
|
||||
return list.get(middle);
|
||||
}
|
||||
@Benchmark
|
||||
public List<String> set() {
|
||||
list.set(list.size() / 2, "test");
|
||||
list.set(middle, "test");
|
||||
return list;
|
||||
}
|
||||
@Benchmark
|
||||
public List<String> iteradd() {
|
||||
ListIterator<String> it =
|
||||
list.listIterator(list.size() / 2);
|
||||
try {
|
||||
it.add("test");
|
||||
} catch(UnsupportedOperationException e) {
|
||||
@ -77,15 +78,16 @@ public class Lists {
|
||||
return list;
|
||||
}
|
||||
@Benchmark
|
||||
public List<String> insert() {
|
||||
list.add(list.size() / 2, "test");
|
||||
return list;
|
||||
}
|
||||
@Benchmark
|
||||
public List<String> remove() {
|
||||
int index = list.size() / 2;
|
||||
if(index > 0)
|
||||
list.remove(index);
|
||||
middle = list.size() / 2;
|
||||
if(middle - 1 > 0)
|
||||
try {
|
||||
list.remove(middle - 1);
|
||||
} catch(ArrayIndexOutOfBoundsException e) {
|
||||
System.out.println("Out of bounds -> size: "
|
||||
+ list.size() + " middle - 1: " + (middle - 1) +
|
||||
" for " + list.getClass().getSimpleName());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
@ -39,23 +39,23 @@ public class Maps {
|
||||
})
|
||||
private int size;
|
||||
|
||||
private String key;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
try {
|
||||
public void setup() throws Exception {
|
||||
map = (Map<String,String>)
|
||||
Class.forName(type).newInstance();
|
||||
} catch(Exception e) {
|
||||
System.err.println(
|
||||
"-> Cannot create: " + type);
|
||||
System.exit(99);
|
||||
}
|
||||
for(int i = 0; i < size; i++)
|
||||
map.put(Integer.toString(i), Integer.toString(i));
|
||||
key = Integer.toString(size / 2);
|
||||
}
|
||||
@Benchmark
|
||||
public void get(Blackhole bh) {
|
||||
String key = Integer.toString(size / 2);
|
||||
bh.consume(map.get(key));
|
||||
public boolean containsKey() {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
@Benchmark
|
||||
public String get() {
|
||||
return map.get(key);
|
||||
}
|
||||
@Benchmark
|
||||
public Map<String,String> put() {
|
||||
|
@ -5,7 +5,6 @@
|
||||
// Performance differences between Queues
|
||||
package understandingcollections.jmh;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@ -45,15 +44,9 @@ public class Queues {
|
||||
private int size;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
try {
|
||||
public void setup() throws Exception {
|
||||
queue = (Queue<String>)
|
||||
Class.forName(type).newInstance();
|
||||
} catch(Exception e) {
|
||||
System.err.println(
|
||||
"-> Cannot create: " + type);
|
||||
System.exit(99);
|
||||
}
|
||||
for(int i = 0; i < size; i++)
|
||||
queue.add(Integer.toString(i));
|
||||
}
|
||||
@ -63,7 +56,7 @@ public class Queues {
|
||||
return queue;
|
||||
}
|
||||
@Benchmark
|
||||
public void poll(Blackhole bh) {
|
||||
bh.consume(queue.poll());
|
||||
public String poll() {
|
||||
return queue.poll();
|
||||
}
|
||||
}
|
||||
|
@ -36,18 +36,15 @@ public class Sets {
|
||||
})
|
||||
private int size;
|
||||
|
||||
private String key;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
try {
|
||||
public void setup() throws Exception {
|
||||
set = (Set<String>)
|
||||
Class.forName(type).newInstance();
|
||||
} catch(Exception e) {
|
||||
System.err.println(
|
||||
"-> Cannot create: " + type);
|
||||
System.exit(99);
|
||||
}
|
||||
for(int i = 0; i < size; i++)
|
||||
set.add(Integer.toString(i));
|
||||
key = Integer.toString(size/2);
|
||||
}
|
||||
@Benchmark
|
||||
public Set<String> add() {
|
||||
@ -55,9 +52,8 @@ public class Sets {
|
||||
return set;
|
||||
}
|
||||
@Benchmark
|
||||
public void contains(Blackhole bh) {
|
||||
String key = Integer.toString(size/2);
|
||||
bh.consume(set.contains(key));
|
||||
public boolean contains() {
|
||||
return set.contains(key);
|
||||
}
|
||||
@Benchmark
|
||||
public void iterate(Blackhole bh) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user