chapter progress
This commit is contained in:
parent
d8a46db6f3
commit
37c5413501
@ -10,129 +10,206 @@ import java.util.*;
|
|||||||
public class Countries {
|
public class Countries {
|
||||||
public static final String[][] DATA = {
|
public static final String[][] DATA = {
|
||||||
// Africa
|
// Africa
|
||||||
{"ALGERIA","Algiers"}, {"ANGOLA","Luanda"},
|
{"ALGERIA","Algiers"},
|
||||||
{"BENIN","Porto-Novo"}, {"BOTSWANA","Gaberone"},
|
{"ANGOLA","Luanda"},
|
||||||
|
{"BENIN","Porto-Novo"},
|
||||||
|
{"BOTSWANA","Gaberone"},
|
||||||
{"BURKINA FASO","Ouagadougou"},
|
{"BURKINA FASO","Ouagadougou"},
|
||||||
{"BURUNDI","Bujumbura"},
|
{"BURUNDI","Bujumbura"},
|
||||||
{"CAMEROON","Yaounde"}, {"CAPE VERDE","Praia"},
|
{"CAMEROON","Yaounde"},
|
||||||
|
{"CAPE VERDE","Praia"},
|
||||||
{"CENTRAL AFRICAN REPUBLIC","Bangui"},
|
{"CENTRAL AFRICAN REPUBLIC","Bangui"},
|
||||||
{"CHAD","N'djamena"}, {"COMOROS","Moroni"},
|
{"CHAD","N'djamena"},
|
||||||
{"CONGO","Brazzaville"}, {"DJIBOUTI","Dijibouti"},
|
{"COMOROS","Moroni"},
|
||||||
{"EGYPT","Cairo"}, {"EQUATORIAL GUINEA","Malabo"},
|
{"CONGO","Brazzaville"},
|
||||||
{"ERITREA","Asmara"}, {"ETHIOPIA","Addis Ababa"},
|
{"DJIBOUTI","Dijibouti"},
|
||||||
{"GABON","Libreville"}, {"THE GAMBIA","Banjul"},
|
{"EGYPT","Cairo"},
|
||||||
{"GHANA","Accra"}, {"GUINEA","Conakry"},
|
{"EQUATORIAL GUINEA","Malabo"},
|
||||||
|
{"ERITREA","Asmara"},
|
||||||
|
{"ETHIOPIA","Addis Ababa"},
|
||||||
|
{"GABON","Libreville"},
|
||||||
|
{"THE GAMBIA","Banjul"},
|
||||||
|
{"GHANA","Accra"},
|
||||||
|
{"GUINEA","Conakry"},
|
||||||
{"BISSAU","Bissau"},
|
{"BISSAU","Bissau"},
|
||||||
{"COTE D'IVOIR (IVORY COAST)","Yamoussoukro"},
|
{"COTE D'IVOIR (IVORY COAST)","Yamoussoukro"},
|
||||||
{"KENYA","Nairobi"}, {"LESOTHO","Maseru"},
|
{"KENYA","Nairobi"},
|
||||||
{"LIBERIA","Monrovia"}, {"LIBYA","Tripoli"},
|
{"LESOTHO","Maseru"},
|
||||||
{"MADAGASCAR","Antananarivo"}, {"MALAWI","Lilongwe"},
|
{"LIBERIA","Monrovia"},
|
||||||
{"MALI","Bamako"}, {"MAURITANIA","Nouakchott"},
|
{"LIBYA","Tripoli"},
|
||||||
{"MAURITIUS","Port Louis"}, {"MOROCCO","Rabat"},
|
{"MADAGASCAR","Antananarivo"},
|
||||||
{"MOZAMBIQUE","Maputo"}, {"NAMIBIA","Windhoek"},
|
{"MALAWI","Lilongwe"},
|
||||||
{"NIGER","Niamey"}, {"NIGERIA","Abuja"},
|
{"MALI","Bamako"},
|
||||||
|
{"MAURITANIA","Nouakchott"},
|
||||||
|
{"MAURITIUS","Port Louis"},
|
||||||
|
{"MOROCCO","Rabat"},
|
||||||
|
{"MOZAMBIQUE","Maputo"},
|
||||||
|
{"NAMIBIA","Windhoek"},
|
||||||
|
{"NIGER","Niamey"},
|
||||||
|
{"NIGERIA","Abuja"},
|
||||||
{"RWANDA","Kigali"},
|
{"RWANDA","Kigali"},
|
||||||
{"SAO TOME E PRINCIPE","Sao Tome"},
|
{"SAO TOME E PRINCIPE","Sao Tome"},
|
||||||
{"SENEGAL","Dakar"}, {"SEYCHELLES","Victoria"},
|
{"SENEGAL","Dakar"},
|
||||||
{"SIERRA LEONE","Freetown"}, {"SOMALIA","Mogadishu"},
|
{"SEYCHELLES","Victoria"},
|
||||||
|
{"SIERRA LEONE","Freetown"},
|
||||||
|
{"SOMALIA","Mogadishu"},
|
||||||
{"SOUTH AFRICA","Pretoria/Cape Town"},
|
{"SOUTH AFRICA","Pretoria/Cape Town"},
|
||||||
{"SUDAN","Khartoum"},
|
{"SUDAN","Khartoum"},
|
||||||
{"SWAZILAND","Mbabane"}, {"TANZANIA","Dodoma"},
|
{"SWAZILAND","Mbabane"},
|
||||||
{"TOGO","Lome"}, {"TUNISIA","Tunis"},
|
{"TANZANIA","Dodoma"},
|
||||||
|
{"TOGO","Lome"},
|
||||||
|
{"TUNISIA","Tunis"},
|
||||||
{"UGANDA","Kampala"},
|
{"UGANDA","Kampala"},
|
||||||
{"DEMOCRATIC REPUBLIC OF THE CONGO (ZAIRE)",
|
{"DEMOCRATIC REPUBLIC OF THE CONGO (ZAIRE)",
|
||||||
"Kinshasa"},
|
"Kinshasa"},
|
||||||
{"ZAMBIA","Lusaka"}, {"ZIMBABWE","Harare"},
|
{"ZAMBIA","Lusaka"},
|
||||||
|
{"ZIMBABWE","Harare"},
|
||||||
// Asia
|
// Asia
|
||||||
{"AFGHANISTAN","Kabul"}, {"BAHRAIN","Manama"},
|
{"AFGHANISTAN","Kabul"},
|
||||||
{"BANGLADESH","Dhaka"}, {"BHUTAN","Thimphu"},
|
{"BAHRAIN","Manama"},
|
||||||
|
{"BANGLADESH","Dhaka"},
|
||||||
|
{"BHUTAN","Thimphu"},
|
||||||
{"BRUNEI","Bandar Seri Begawan"},
|
{"BRUNEI","Bandar Seri Begawan"},
|
||||||
{"CAMBODIA","Phnom Penh"},
|
{"CAMBODIA","Phnom Penh"},
|
||||||
{"CHINA","Beijing"}, {"CYPRUS","Nicosia"},
|
{"CHINA","Beijing"},
|
||||||
{"INDIA","New Delhi"}, {"INDONESIA","Jakarta"},
|
{"CYPRUS","Nicosia"},
|
||||||
{"IRAN","Tehran"}, {"IRAQ","Baghdad"},
|
{"INDIA","New Delhi"},
|
||||||
{"ISRAEL","Jerusalem"}, {"JAPAN","Tokyo"},
|
{"INDONESIA","Jakarta"},
|
||||||
{"JORDAN","Amman"}, {"KUWAIT","Kuwait City"},
|
{"IRAN","Tehran"},
|
||||||
{"LAOS","Vientiane"}, {"LEBANON","Beirut"},
|
{"IRAQ","Baghdad"},
|
||||||
{"MALAYSIA","Kuala Lumpur"}, {"THE MALDIVES","Male"},
|
{"ISRAEL","Jerusalem"},
|
||||||
|
{"JAPAN","Tokyo"},
|
||||||
|
{"JORDAN","Amman"},
|
||||||
|
{"KUWAIT","Kuwait City"},
|
||||||
|
{"LAOS","Vientiane"},
|
||||||
|
{"LEBANON","Beirut"},
|
||||||
|
{"MALAYSIA","Kuala Lumpur"},
|
||||||
|
{"THE MALDIVES","Male"},
|
||||||
{"MONGOLIA","Ulan Bator"},
|
{"MONGOLIA","Ulan Bator"},
|
||||||
{"MYANMAR (BURMA)","Rangoon"},
|
{"MYANMAR (BURMA)","Rangoon"},
|
||||||
{"NEPAL","Katmandu"}, {"NORTH KOREA","P'yongyang"},
|
{"NEPAL","Katmandu"},
|
||||||
{"OMAN","Muscat"}, {"PAKISTAN","Islamabad"},
|
{"NORTH KOREA","P'yongyang"},
|
||||||
{"PHILIPPINES","Manila"}, {"QATAR","Doha"},
|
{"OMAN","Muscat"},
|
||||||
{"SAUDI ARABIA","Riyadh"}, {"SINGAPORE","Singapore"},
|
{"PAKISTAN","Islamabad"},
|
||||||
{"SOUTH KOREA","Seoul"}, {"SRI LANKA","Colombo"},
|
{"PHILIPPINES","Manila"},
|
||||||
|
{"QATAR","Doha"},
|
||||||
|
{"SAUDI ARABIA","Riyadh"},
|
||||||
|
{"SINGAPORE","Singapore"},
|
||||||
|
{"SOUTH KOREA","Seoul"},
|
||||||
|
{"SRI LANKA","Colombo"},
|
||||||
{"SYRIA","Damascus"},
|
{"SYRIA","Damascus"},
|
||||||
{"TAIWAN (REPUBLIC OF CHINA)","Taipei"},
|
{"TAIWAN (REPUBLIC OF CHINA)","Taipei"},
|
||||||
{"THAILAND","Bangkok"}, {"TURKEY","Ankara"},
|
{"THAILAND","Bangkok"},
|
||||||
|
{"TURKEY","Ankara"},
|
||||||
{"UNITED ARAB EMIRATES","Abu Dhabi"},
|
{"UNITED ARAB EMIRATES","Abu Dhabi"},
|
||||||
{"VIETNAM","Hanoi"}, {"YEMEN","Sana'a"},
|
{"VIETNAM","Hanoi"},
|
||||||
|
{"YEMEN","Sana'a"},
|
||||||
// Australia and Oceania
|
// Australia and Oceania
|
||||||
{"AUSTRALIA","Canberra"}, {"FIJI","Suva"},
|
{"AUSTRALIA","Canberra"},
|
||||||
|
{"FIJI","Suva"},
|
||||||
{"KIRIBATI","Bairiki"},
|
{"KIRIBATI","Bairiki"},
|
||||||
{"MARSHALL ISLANDS","Dalap-Uliga-Darrit"},
|
{"MARSHALL ISLANDS","Dalap-Uliga-Darrit"},
|
||||||
{"MICRONESIA","Palikir"}, {"NAURU","Yaren"},
|
{"MICRONESIA","Palikir"},
|
||||||
{"NEW ZEALAND","Wellington"}, {"PALAU","Koror"},
|
{"NAURU","Yaren"},
|
||||||
|
{"NEW ZEALAND","Wellington"},
|
||||||
|
{"PALAU","Koror"},
|
||||||
{"PAPUA NEW GUINEA","Port Moresby"},
|
{"PAPUA NEW GUINEA","Port Moresby"},
|
||||||
{"SOLOMON ISLANDS","Honaira"}, {"TONGA","Nuku'alofa"},
|
{"SOLOMON ISLANDS","Honaira"},
|
||||||
{"TUVALU","Fongafale"}, {"VANUATU","< Port-Vila"},
|
{"TONGA","Nuku'alofa"},
|
||||||
|
{"TUVALU","Fongafale"},
|
||||||
|
{"VANUATU","Port Vila"},
|
||||||
{"WESTERN SAMOA","Apia"},
|
{"WESTERN SAMOA","Apia"},
|
||||||
// Eastern Europe and former USSR
|
// Eastern Europe and former USSR
|
||||||
{"ARMENIA","Yerevan"}, {"AZERBAIJAN","Baku"},
|
{"ARMENIA","Yerevan"},
|
||||||
|
{"AZERBAIJAN","Baku"},
|
||||||
{"BELARUS (BYELORUSSIA)","Minsk"},
|
{"BELARUS (BYELORUSSIA)","Minsk"},
|
||||||
{"BULGARIA","Sofia"}, {"GEORGIA","Tbilisi"},
|
{"BULGARIA","Sofia"},
|
||||||
{"KAZAKSTAN","Almaty"}, {"KYRGYZSTAN","Alma-Ata"},
|
{"GEORGIA","Tbilisi"},
|
||||||
{"MOLDOVA","Chisinau"}, {"RUSSIA","Moscow"},
|
{"KAZAKSTAN","Almaty"},
|
||||||
|
{"KYRGYZSTAN","Alma-Ata"},
|
||||||
|
{"MOLDOVA","Chisinau"},
|
||||||
|
{"RUSSIA","Moscow"},
|
||||||
{"TAJIKISTAN","Dushanbe"},
|
{"TAJIKISTAN","Dushanbe"},
|
||||||
{"TURKMENISTAN","Ashkabad"},
|
{"TURKMENISTAN","Ashkabad"},
|
||||||
{"UKRAINE","Kyiv"}, {"UZBEKISTAN","Tashkent"},
|
{"UKRAINE","Kyiv"},
|
||||||
|
{"UZBEKISTAN","Tashkent"},
|
||||||
// Europe
|
// Europe
|
||||||
{"ALBANIA","Tirana"}, {"ANDORRA","Andorra la Vella"},
|
{"ALBANIA","Tirana"},
|
||||||
{"AUSTRIA","Vienna"}, {"BELGIUM","Brussels"},
|
{"ANDORRA","Andorra la Vella"},
|
||||||
{"BOSNIA","---"}, {"HERZEGOVINA","Sarajevo"},
|
{"AUSTRIA","Vienna"},
|
||||||
{"CROATIA","Zagreb"}, {"CZECH REPUBLIC","Prague"},
|
{"BELGIUM","Brussels"},
|
||||||
{"DENMARK","Copenhagen"}, {"ESTONIA","Tallinn"},
|
{"BOSNIA-HERZEGOVINA","Sarajevo"},
|
||||||
{"FINLAND","Helsinki"}, {"FRANCE","Paris"},
|
{"CROATIA","Zagreb"},
|
||||||
{"GERMANY","Berlin"}, {"GREECE","Athens"},
|
{"CZECH REPUBLIC","Prague"},
|
||||||
{"HUNGARY","Budapest"}, {"ICELAND","Reykjavik"},
|
{"DENMARK","Copenhagen"},
|
||||||
{"IRELAND","Dublin"}, {"ITALY","Rome"},
|
{"ESTONIA","Tallinn"},
|
||||||
{"LATVIA","Riga"}, {"LIECHTENSTEIN","Vaduz"},
|
{"FINLAND","Helsinki"},
|
||||||
{"LITHUANIA","Vilnius"}, {"LUXEMBOURG","Luxembourg"},
|
{"FRANCE","Paris"},
|
||||||
{"MACEDONIA","Skopje"}, {"MALTA","Valletta"},
|
{"GERMANY","Berlin"},
|
||||||
{"MONACO","Monaco"}, {"MONTENEGRO","Podgorica"},
|
{"GREECE","Athens"},
|
||||||
{"THE NETHERLANDS","Amsterdam"}, {"NORWAY","Oslo"},
|
{"HUNGARY","Budapest"},
|
||||||
{"POLAND","Warsaw"}, {"PORTUGAL","Lisbon"},
|
{"ICELAND","Reykjavik"},
|
||||||
{"ROMANIA","Bucharest"}, {"SAN MARINO","San Marino"},
|
{"IRELAND","Dublin"},
|
||||||
{"SERBIA","Belgrade"}, {"SLOVAKIA","Bratislava"},
|
{"ITALY","Rome"},
|
||||||
{"SLOVENIA","Ljuijana"}, {"SPAIN","Madrid"},
|
{"LATVIA","Riga"},
|
||||||
{"SWEDEN","Stockholm"}, {"SWITZERLAND","Berne"},
|
{"LIECHTENSTEIN","Vaduz"},
|
||||||
{"UNITED KINGDOM","London"}, {"VATICAN CITY","---"},
|
{"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
|
// North and Central America
|
||||||
{"ANTIGUA AND BARBUDA","Saint John's"},
|
{"ANTIGUA AND BARBUDA","Saint John's"},
|
||||||
{"BAHAMAS","Nassau"},
|
{"BAHAMAS","Nassau"},
|
||||||
{"BARBADOS","Bridgetown"}, {"BELIZE","Belmopan"},
|
{"BARBADOS","Bridgetown"},
|
||||||
{"CANADA","Ottawa"}, {"COSTA RICA","San Jose"},
|
{"BELIZE","Belmopan"},
|
||||||
{"CUBA","Havana"}, {"DOMINICA","Roseau"},
|
{"CANADA","Ottawa"},
|
||||||
|
{"COSTA RICA","San Jose"},
|
||||||
|
{"CUBA","Havana"},
|
||||||
|
{"DOMINICA","Roseau"},
|
||||||
{"DOMINICAN REPUBLIC","Santo Domingo"},
|
{"DOMINICAN REPUBLIC","Santo Domingo"},
|
||||||
{"EL SALVADOR","San Salvador"},
|
{"EL SALVADOR","San Salvador"},
|
||||||
{"GRENADA","Saint George's"},
|
{"GRENADA","Saint George's"},
|
||||||
{"GUATEMALA","Guatemala City"},
|
{"GUATEMALA","Guatemala City"},
|
||||||
{"HAITI","Port-au-Prince"},
|
{"HAITI","Port-au-Prince"},
|
||||||
{"HONDURAS","Tegucigalpa"}, {"JAMAICA","Kingston"},
|
{"HONDURAS","Tegucigalpa"},
|
||||||
{"MEXICO","Mexico City"}, {"NICARAGUA","Managua"},
|
{"JAMAICA","Kingston"},
|
||||||
{"PANAMA","Panama City"}, {"ST. KITTS","---"},
|
{"MEXICO","Mexico City"},
|
||||||
{"NEVIS","Basseterre"}, {"ST. LUCIA","Castries"},
|
{"NICARAGUA","Managua"},
|
||||||
|
{"PANAMA","Panama City"},
|
||||||
|
{"ST. KITTS AND NEVIS","Basseterre"},
|
||||||
|
{"ST. LUCIA","Castries"},
|
||||||
{"ST. VINCENT AND THE GRENADINES","Kingstown"},
|
{"ST. VINCENT AND THE GRENADINES","Kingstown"},
|
||||||
{"UNITED STATES OF AMERICA","Washington, D.C."},
|
{"UNITED STATES OF AMERICA","Washington, D.C."},
|
||||||
// South America
|
// South America
|
||||||
{"ARGENTINA","Buenos Aires"},
|
{"ARGENTINA","Buenos Aires"},
|
||||||
{"BOLIVIA","Sucre (legal)/La Paz(administrative)"},
|
{"BOLIVIA","Sucre (legal)/La Paz(administrative)"},
|
||||||
{"BRAZIL","Brasilia"}, {"CHILE","Santiago"},
|
{"BRAZIL","Brasilia"},
|
||||||
{"COLOMBIA","Bogota"}, {"ECUADOR","Quito"},
|
{"CHILE","Santiago"},
|
||||||
{"GUYANA","Georgetown"}, {"PARAGUAY","Asuncion"},
|
{"COLOMBIA","Bogota"},
|
||||||
{"PERU","Lima"}, {"SURINAME","Paramaribo"},
|
{"ECUADOR","Quito"},
|
||||||
|
{"GUYANA","Georgetown"},
|
||||||
|
{"PARAGUAY","Asuncion"},
|
||||||
|
{"PERU","Lima"},
|
||||||
|
{"SURINAME","Paramaribo"},
|
||||||
{"TRINIDAD AND TOBAGO","Port of Spain"},
|
{"TRINIDAD AND TOBAGO","Port of Spain"},
|
||||||
{"URUGUAY","Montevideo"}, {"VENEZUELA","Caracas"},
|
{"URUGUAY","Montevideo"},
|
||||||
|
{"VENEZUELA","Caracas"},
|
||||||
};
|
};
|
||||||
// Use AbstractMap by implementing entrySet()
|
// Use AbstractMap by implementing entrySet()
|
||||||
private static class FlyweightMap
|
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.
|
// Visit http://OnJava8.com for more book information.
|
||||||
// Things you can do with Lists
|
// Things you can do with Lists
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import onjava.*;
|
import onjava.HTMLColors;
|
||||||
|
|
||||||
public class ListOps {
|
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 boolean b;
|
||||||
private static String s;
|
private static String s;
|
||||||
private static int i;
|
private static int i;
|
||||||
@ -16,12 +19,12 @@ public class ListOps {
|
|||||||
a.add(1, "x"); // Add at location 1
|
a.add(1, "x"); // Add at location 1
|
||||||
a.add("x"); // Add at end
|
a.add("x"); // Add at end
|
||||||
// Add a collection:
|
// Add a collection:
|
||||||
a.addAll(Countries.names(25));
|
a.addAll(LIST);
|
||||||
// Add a collection starting at location 3:
|
// 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?
|
b = a.contains("1"); // Is it in there?
|
||||||
// Is the entire collection 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
|
// Lists allow random access, which is cheap
|
||||||
// for ArrayList, expensive for LinkedList:
|
// for ArrayList, expensive for LinkedList:
|
||||||
s = a.get(1); // Get (typed) object at location 1
|
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"
|
a.set(1, "y"); // Set location 1 to "y"
|
||||||
// Keep everything that's in the argument
|
// Keep everything that's in the argument
|
||||||
// (the intersection of the two sets):
|
// (the intersection of the two sets):
|
||||||
a.retainAll(Countries.names(25));
|
a.retainAll(LIST);
|
||||||
// Remove everything that's in the argument:
|
// Remove everything that's in the argument:
|
||||||
a.removeAll(Countries.names(25));
|
a.removeAll(LIST);
|
||||||
i = a.size(); // How big is it?
|
i = a.size(); // How big is it?
|
||||||
a.clear(); // Remove all elements
|
a.clear(); // Remove all elements
|
||||||
}
|
}
|
||||||
@ -65,7 +68,7 @@ public class ListOps {
|
|||||||
}
|
}
|
||||||
public static void testVisual(List<String> a) {
|
public static void testVisual(List<String> a) {
|
||||||
System.out.println(a);
|
System.out.println(a);
|
||||||
List<String> b = Countries.names(25);
|
List<String> b = LIST;
|
||||||
System.out.println("b = " + b);
|
System.out.println("b = " + b);
|
||||||
a.addAll(b);
|
a.addAll(b);
|
||||||
a.addAll(b);
|
a.addAll(b);
|
||||||
@ -90,7 +93,7 @@ public class ListOps {
|
|||||||
// There are some things that only LinkedLists can do:
|
// There are some things that only LinkedLists can do:
|
||||||
public static void testLinkedList() {
|
public static void testLinkedList() {
|
||||||
LinkedList<String> ll = new LinkedList<>();
|
LinkedList<String> ll = new LinkedList<>();
|
||||||
ll.addAll(Countries.names(25));
|
ll.addAll(LIST);
|
||||||
System.out.println(ll);
|
System.out.println(ll);
|
||||||
// Treat it like a stack, pushing:
|
// Treat it like a stack, pushing:
|
||||||
ll.addFirst("one");
|
ll.addFirst("one");
|
||||||
@ -108,39 +111,15 @@ public class ListOps {
|
|||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Make and fill a new list each time:
|
// Make and fill a new list each time:
|
||||||
basicTest(
|
basicTest(new LinkedList<>(LIST));
|
||||||
new LinkedList<>(Countries.names(25)));
|
basicTest(new ArrayList<>(LIST));
|
||||||
basicTest(
|
iterMotion(new LinkedList<>(LIST));
|
||||||
new ArrayList<>(Countries.names(25)));
|
iterMotion(new ArrayList<>(LIST));
|
||||||
iterMotion(
|
iterManipulation(new LinkedList<>(LIST));
|
||||||
new LinkedList<>(Countries.names(25)));
|
iterManipulation(new ArrayList<>(LIST));
|
||||||
iterMotion(
|
testVisual(new LinkedList<>(LIST));
|
||||||
new ArrayList<>(Countries.names(25)));
|
|
||||||
iterManipulation(
|
|
||||||
new LinkedList<>(Countries.names(25)));
|
|
||||||
iterManipulation(
|
|
||||||
new ArrayList<>(Countries.names(25)));
|
|
||||||
testVisual(
|
|
||||||
new LinkedList<>(Countries.names(25)));
|
|
||||||
testLinkedList();
|
testLinkedList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Output: (First and Last 2 Lines)
|
/* Output:
|
||||||
[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]
|
|
||||||
*/
|
*/
|
||||||
|
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
|
// Performance differences between Deques
|
||||||
package understandingcollections.jmh;
|
package understandingcollections.jmh;
|
||||||
import org.openjdk.jmh.annotations.*;
|
import org.openjdk.jmh.annotations.*;
|
||||||
import org.openjdk.jmh.infra.Blackhole;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -37,15 +36,9 @@ public class Deques {
|
|||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() throws Exception {
|
||||||
try {
|
deque = (Deque<String>)
|
||||||
deque = (Deque<String>)
|
Class.forName(type).newInstance();
|
||||||
Class.forName(type).newInstance();
|
|
||||||
} catch(Exception e) {
|
|
||||||
System.err.println(
|
|
||||||
"-> Cannot create: " + type);
|
|
||||||
System.exit(99);
|
|
||||||
}
|
|
||||||
for(int i = 0; i < size; i++)
|
for(int i = 0; i < size; i++)
|
||||||
deque.add(Integer.toString(i));
|
deque.add(Integer.toString(i));
|
||||||
}
|
}
|
||||||
@ -60,11 +53,11 @@ public class Deques {
|
|||||||
return deque;
|
return deque;
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void pollFirst(Blackhole bh) {
|
public String pollFirst() {
|
||||||
bh.consume(deque.pollFirst());
|
return deque.pollFirst();
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void pollLast(Blackhole bh) {
|
public String pollLast() {
|
||||||
bh.consume(deque.pollLast());
|
return deque.pollLast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
// Performance differences between Lists
|
// Performance differences between Lists
|
||||||
package understandingcollections.jmh;
|
package understandingcollections.jmh;
|
||||||
import org.openjdk.jmh.annotations.*;
|
import org.openjdk.jmh.annotations.*;
|
||||||
import org.openjdk.jmh.infra.Blackhole;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
@ -36,37 +35,39 @@ public class Lists {
|
|||||||
})
|
})
|
||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
|
private int middle;
|
||||||
|
private ListIterator<String> it;
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() throws Exception {
|
||||||
try {
|
list = (List<String>)
|
||||||
list = (List<String>)
|
Class.forName(type).newInstance();
|
||||||
Class.forName(type).newInstance();
|
|
||||||
} catch(Exception e) {
|
|
||||||
System.err.println(
|
|
||||||
"-> Cannot create: " + type);
|
|
||||||
System.exit(99);
|
|
||||||
}
|
|
||||||
for(int i = 0; i < size; i++)
|
for(int i = 0; i < size; i++)
|
||||||
list.add(Integer.toString(i));
|
list.add(Integer.toString(i));
|
||||||
|
middle = size / 2;
|
||||||
|
it = list.listIterator(middle);
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public List<String> add() {
|
public List<String> append() {
|
||||||
list.add(list.size() / 2, "test");
|
list.add("test");
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void get(Blackhole bh) {
|
public List<String> insert() {
|
||||||
bh.consume(list.get(list.size() / 2));
|
list.add(middle, "test");
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
@Benchmark
|
||||||
|
public String get() {
|
||||||
|
return list.get(middle);
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public List<String> set() {
|
public List<String> set() {
|
||||||
list.set(list.size() / 2, "test");
|
list.set(middle, "test");
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public List<String> iteradd() {
|
public List<String> iteradd() {
|
||||||
ListIterator<String> it =
|
|
||||||
list.listIterator(list.size() / 2);
|
|
||||||
try {
|
try {
|
||||||
it.add("test");
|
it.add("test");
|
||||||
} catch(UnsupportedOperationException e) {
|
} catch(UnsupportedOperationException e) {
|
||||||
@ -77,15 +78,16 @@ public class Lists {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public List<String> insert() {
|
|
||||||
list.add(list.size() / 2, "test");
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
@Benchmark
|
|
||||||
public List<String> remove() {
|
public List<String> remove() {
|
||||||
int index = list.size() / 2;
|
middle = list.size() / 2;
|
||||||
if(index > 0)
|
if(middle - 1 > 0)
|
||||||
list.remove(index);
|
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;
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,23 +39,23 @@ public class Maps {
|
|||||||
})
|
})
|
||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() throws Exception {
|
||||||
try {
|
map = (Map<String,String>)
|
||||||
map = (Map<String,String>)
|
Class.forName(type).newInstance();
|
||||||
Class.forName(type).newInstance();
|
|
||||||
} catch(Exception e) {
|
|
||||||
System.err.println(
|
|
||||||
"-> Cannot create: " + type);
|
|
||||||
System.exit(99);
|
|
||||||
}
|
|
||||||
for(int i = 0; i < size; i++)
|
for(int i = 0; i < size; i++)
|
||||||
map.put(Integer.toString(i), Integer.toString(i));
|
map.put(Integer.toString(i), Integer.toString(i));
|
||||||
|
key = Integer.toString(size / 2);
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void get(Blackhole bh) {
|
public boolean containsKey() {
|
||||||
String key = Integer.toString(size / 2);
|
return map.containsKey(key);
|
||||||
bh.consume(map.get(key));
|
}
|
||||||
|
@Benchmark
|
||||||
|
public String get() {
|
||||||
|
return map.get(key);
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public Map<String,String> put() {
|
public Map<String,String> put() {
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
// Performance differences between Queues
|
// Performance differences between Queues
|
||||||
package understandingcollections.jmh;
|
package understandingcollections.jmh;
|
||||||
import org.openjdk.jmh.annotations.*;
|
import org.openjdk.jmh.annotations.*;
|
||||||
import org.openjdk.jmh.infra.Blackhole;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
@ -45,15 +44,9 @@ public class Queues {
|
|||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() throws Exception {
|
||||||
try {
|
queue = (Queue<String>)
|
||||||
queue = (Queue<String>)
|
Class.forName(type).newInstance();
|
||||||
Class.forName(type).newInstance();
|
|
||||||
} catch(Exception e) {
|
|
||||||
System.err.println(
|
|
||||||
"-> Cannot create: " + type);
|
|
||||||
System.exit(99);
|
|
||||||
}
|
|
||||||
for(int i = 0; i < size; i++)
|
for(int i = 0; i < size; i++)
|
||||||
queue.add(Integer.toString(i));
|
queue.add(Integer.toString(i));
|
||||||
}
|
}
|
||||||
@ -63,7 +56,7 @@ public class Queues {
|
|||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void poll(Blackhole bh) {
|
public String poll() {
|
||||||
bh.consume(queue.poll());
|
return queue.poll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,18 +36,15 @@ public class Sets {
|
|||||||
})
|
})
|
||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() throws Exception {
|
||||||
try {
|
set = (Set<String>)
|
||||||
set = (Set<String>)
|
Class.forName(type).newInstance();
|
||||||
Class.forName(type).newInstance();
|
|
||||||
} catch(Exception e) {
|
|
||||||
System.err.println(
|
|
||||||
"-> Cannot create: " + type);
|
|
||||||
System.exit(99);
|
|
||||||
}
|
|
||||||
for(int i = 0; i < size; i++)
|
for(int i = 0; i < size; i++)
|
||||||
set.add(Integer.toString(i));
|
set.add(Integer.toString(i));
|
||||||
|
key = Integer.toString(size/2);
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public Set<String> add() {
|
public Set<String> add() {
|
||||||
@ -55,9 +52,8 @@ public class Sets {
|
|||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void contains(Blackhole bh) {
|
public boolean contains() {
|
||||||
String key = Integer.toString(size/2);
|
return set.contains(key);
|
||||||
bh.consume(set.contains(key));
|
|
||||||
}
|
}
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void iterate(Blackhole bh) {
|
public void iterate(Blackhole bh) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user