Index: /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java	(revision 9767)
+++ /trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java	(revision 9768)
@@ -13,6 +13,8 @@
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -80,5 +82,5 @@
     }
 
-    private static class TileMatrixSet {
+    private static class TileMatrixSetBuilder {
         SortedSet<TileMatrix> tileMatrix = new TreeSet<>(new Comparator<TileMatrix>() {
             @Override
@@ -91,13 +93,31 @@
         private String identifier;
 
+        TileMatrixSet build() {
+            return new TileMatrixSet(this);
+        }
+    }
+
+    private static class TileMatrixSet {
+
+        private final List<TileMatrix> tileMatrix;
+        private final String crs;
+        private final String identifier;
+
         TileMatrixSet(TileMatrixSet tileMatrixSet) {
             if (tileMatrixSet != null) {
-                tileMatrix = new TreeSet<>(tileMatrixSet.tileMatrix);
+                tileMatrix = new ArrayList<>(tileMatrixSet.tileMatrix);
                 crs = tileMatrixSet.crs;
                 identifier = tileMatrixSet.identifier;
-            }
-        }
-
-        TileMatrixSet() {
+            } else {
+                tileMatrix = Collections.emptyList();
+                crs = null;
+                identifier = null;
+            }
+        }
+
+        TileMatrixSet(TileMatrixSetBuilder builder) {
+            tileMatrix = new ArrayList<>(builder.tileMatrix);
+            crs = builder.crs;
+            identifier = builder.identifier;
         }
 
@@ -405,5 +425,5 @@
      */
     private static TileMatrixSet parseTileMatrixSet(XMLStreamReader reader) throws XMLStreamException {
-        TileMatrixSet matrixSet = new TileMatrixSet();
+        TileMatrixSetBuilder matrixSet = new TileMatrixSetBuilder();
         for (int event = reader.getEventType();
                 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && new QName(WMTS_NS_URL, "TileMatrixSet").equals(reader.getName()));
@@ -421,5 +441,5 @@
                     }
         }
-        return matrixSet;
+        return matrixSet.build();
     }
 
@@ -637,5 +657,5 @@
         Collection<Layer> layers = getLayers(null, Main.getProjection().toCode());
         if (!layers.isEmpty()) {
-            return layers.iterator().next().tileMatrixSet.tileMatrix.first().tileHeight;
+            return layers.iterator().next().tileMatrixSet.tileMatrix.get(0).tileHeight;
         }
         // if no layers is found, fallback to default mercator tile size. Maybe it will work
@@ -694,5 +714,5 @@
             return null;
         }
-        return this.currentTileMatrixSet.tileMatrix.toArray(new TileMatrix[]{})[zoom - 1];
+        return this.currentTileMatrixSet.tileMatrix.get(zoom - 1);
     }
 
