For example, Map<String*, Integer> map = new TreeMap<String, Integer>(); , where * can I put < <String,String>, Integer > for example?
2. What are singleton collections for? The first time I hear. Google gives little information.
For example, Map<String*, Integer> map = new TreeMap<String, Integer>(); , where * can I put < <String,String>, Integer > for example?
2. What are singleton collections for? The first time I hear. Google gives little information.
Depends on the specific implementation of the Map . For TreeMap , the key requirement is the implementation of the Comparable interface. Accordingly, you can define your own class, which has two fields of type String and implements Comparable , and then use objects of this class as a key. It is even easier to use HashMap instead of TreeMap . The HashMap key can be any hash object, so you can use, for example, a List containing a couple of lines.
List<String> key1 = new ArrayList<>(); key1.add("qqq"); key1.add("www"); List<String> key2 = new ArrayList<>(); key2.add("eee"); key2.add("rrr"); Map<List, Integer> map = new HashMap<>(); map.put(key1, 1); map.put(key2, 2); The second part of the question is too general. And as the definition of "single-element collection" is too vague, and the scope of its application is very extensive. For example, some method waits for a collection as an argument, and you only need to pass one element.
List<String> list = new ArrayList<>(); list.add("abc"); list.add(null); list.add("def"); list.removeAll(Collections.singletonList(null)); Source: https://ru.stackoverflow.com/questions/591517/
All Articles