å 容ïŒ
- List.subList
- PriorityQueue
- EnumSetããã³EnumMap
- Set.addïŒEïŒããã³Set.removeïŒEïŒã¯ããŒã«å€ãè¿ããŸã
- Map.putïŒKãVïŒãMap.removeïŒKïŒãList.setïŒidxãEïŒãList.removeïŒidxïŒã¯åã®ã¢ã€ãã ãè¿ããŸã
- Map.keySetïŒïŒããã³Map.valuesïŒïŒ
- Arrays.asListãããŒã«ãªããŸã
- Collections.max
- LinkedListãStackãVectorãHashtable
List.subList
圌ãã¯ãã§ã«ããã«ã€ããŠæžããŠããŸãããç¹°ãè¿ã䟡å€ã¯ãããŸãã ãããããCollections APIã§æãéå°è©äŸ¡ãããŠããã¡ãœããã§ãã ãªã¹ãã®äžéšãäœããã®æ¹æ³ã§åŠçããå¿ èŠãããå ŽåããããŸãïŒããšãã°ããåå²çµ±æ²»ããã¡ããªãŒã®ã¢ã«ãŽãªãºã ã§ããŸãã¯ã¿ã¹ã¯ã䞊ååããå ŽåïŒã å€ãã®å Žåã3ã€ã®ãã©ã¡ãŒã¿ãŒïŒListãfromããã³toïŒã«é¢é£ä»ããããã¡ãœãããŸãã¯ã¯ã©ã¹ãäœæãããŸãã
void processListPart(List<Item> list, int from, int to) { for(int idx = from; idx < to; idx++) { Item item = list.get(idx); ... } }
ãã®ããããããè¡ãå¿ èŠã¯ãããŸããã ã¢ã«ãŽãªãºã ã®å®è£ ã¯ããªã¹ãã®äžéšãåŠçããããšãæ°ã«ããã¹ãã§ã¯ãããŸããã æžãïŒ
void processList(List<Item> list) { for(Item item : list) { ... } }
ãããŠé»è©±ãã
processList(list.subList(from, to));
1ã€ã®ã¡ãœããã«ãã¹ãŠãå«ãŸããŠããå Žåã§ããã€ã³ããã¯ã¹ã䜿çšãããããæ¡åŒµforã«ãŒãã䜿çšããæ¹ã䟿å©ã§ãã
for(Item item : list.subList(from, to)) {...}
ããã«ãsubListã¯å®å šã«æ©èœãããªã¹ãã§ãããæžã蟌ã¿æã«ãæ©èœãã芪ãªã¹ãã«é©åãªå€æŽãå ããŸãã ãªã¹ãã®äžå€®ããå€ãã®ã¢ã€ãã ãåé€ããå¿ èŠããããŸããïŒ ãã以äžç°¡åãªããšã¯ãããŸããïŒ
list.subList(from, to).clear();
ArrayListã®ãããªäžè¬çãªå®è£ ã§ã¯ãããã¯éåžžã«é«éã§ãã
ãªã¹ããç¹å®ã®èŠçŽ ã§å§ãŸããã©ããã調ã¹ãå¿ èŠããããŸããïŒ ãããŠãsubListãæã«å ¥ããŸãïŒ
List<String> prefix = Arrays.asList("a", "prefix", "values"); if(myList.size() >= prefix.size() && myList.subList(0, prefix.size()).equals(prefix)) {...}
ãããªã¹ãã«ãæåã®ãªã¹ããé€ãå¥ã®ãªã¹ãã®ãã¹ãŠã®èŠçŽ ãè¿œå ããå¿ èŠããããŸããïŒ ãããŠããã§subListãå©ãã«ãªããŸãïŒ
list1.addAll(list2.subList(1, list2.size()));
Arrays.asList(array).subList(from, to)
ãèšè¿°ã§ããããšãå¿ããªãã§ãã ããããããã£ãŠãäžèšã¯éããªããã£ãé åã«ãé©çšãããŸãã ããããæ§é çã«å€æŽããããšã¯ã§ããŸããããé åã®äžéšãèªã¿åããªã¹ããåãå ¥ããã¡ãœããã«æž¡ãã®ã¯ç°¡åã§ãã
PriorityQueue
subListãæãéå°è©äŸ¡ãããŠããã¡ãœããã§ããå ŽåãPriorityQueueã¯æãéå°è©äŸ¡ãããŠããã¯ã©ã¹ã§ãã å€ãã®å ŽåããœãŒããããŠããªã倧ããªãªã¹ãã®æå°å€ã10åèŠã€ããã¿ã¹ã¯ã«çŽé¢ããŠããŸãã ã»ãšãã©ã®å Žåããªã¹ãã¯ãœãŒããããŠãããæåã®10åã®å€ãååŸãããŸãã å ã®ãªã¹ããå€æŽã§ããªãå Žåã¯ã䞊ã¹æ¿ãã®ããã«ã³ããŒããå¿ èŠããããŸãã ãã ããåªå 床ãã¥ãŒã¯ãã®ã¿ã¹ã¯ã«ç°¡åã«å¯ŸåŠã§ããŸãã
public static <T extends Comparable<T>> List<T> leastN(Collection<T> input, int n) { assert n > 0; PriorityQueue<T> pq = new PriorityQueue<>(Collections.reverseOrder()); for (T t : input) { if (pq.size() < n) { pq.add(t); } else if (pq.peek().compareTo(t) > 0) { pq.poll(); pq.add(t); } } List<T> list = new ArrayList<>(pq); Collections.sort(list); return list; }
ãã®ãããªã³ãŒãã¯ãããŒã¿ã«å¿ããŠããœãŒããããã¯ããã«é«éã«åäœããŸãã ããšãã°ãn = 10ã§ãã©ã³ãã ã«å ¥åããã100äžåã®èŠçŽ ã®ãªã¹ãã®å Žåãåªå 床ãã¥ãŒã¯ã»ãŒ100åãœãŒãæ¹æ³ãè¿œãè¶ããŸãã ãã®å Žåãè¿œå ã®ã¡ã¢ãªã«ã¯OïŒnïŒãå¿ èŠã§ãå ¥åèŠçŽ ã¯ã¹ããªãŒãã³ã°ã¢ãŒãã§åŠçã§ããŸãïŒããšãã°ãå ¥åãã¡ã€ã«ãã10åã®æå°ã®æ°åãéžæããŸãïŒã
äžè¬ã«ã人ã ã¯ããã€ãã®ããŒã¿æ§é ãç 究ããã©ãã§ããããã䜿çšããåŸåããããŸãã æ ããŠã¯ãããŸãããç°ãªãæ§é ã«ç²ŸéããŠãã ããã
EnumSetããã³EnumMap
HashSetããã³HashMapã§åæå€ãããŒãšããŠäœ¿çšãããã³ãŒãã¯ãŸã ãããŸãã ããã¯æ©èœããŸãããäžåœã«ç¡é§ã§ãã æ¢åã®ç¹å¥ãªã¯ã©ã¹EnumSetãšEnumMapã¯ãçç£æ§ãå€§å¹ ã«åäžããŠããŸãã ãããã£ãŠãenumã®å€ã64å以äžã®å ŽåãEnumSetã¯ãã¹ãŠãããããã¹ã¯ã®longåã®1ã€ã®ãã£ãŒã«ãã«æ ŒçŽããŸãã EnumMapã«ã¯ãenumã®èŠçŽ ãšåãé·ãã®éåžžã®é åã«ãã¹ãŠã®å€ãå«ãŸããŸãããããŒã¯ãŸã£ããæ ŒçŽãããŸããã enumã®åå€ã«ã¯åºæ°ïŒïŒã·ãªã¢ã«çªå·ããããããenumããŒããé åèŠçŽ ã«ç°¡åã«åãæ¿ããããšãã§ããŸãã ãŸããé åã®ãµã€ãºãå€æŽããå¿ èŠã¯ãããŸããã
Set.addïŒEïŒããã³Set.removeïŒEïŒã¯ããŒã«å€ãè¿ããŸã
å€ãã®å Žåãåæ§ã®ã³ãŒãã衚瀺ãããŸãã
if(!set.contains(item)) { set.add(item); // do something } else { // do something else }
Setã«è¿œå ããæäœã¯ãè¿œå ãæåããå ŽåïŒã€ãŸããèŠçŽ ããªãã£ãå ŽåïŒã«trueãè¿ãããã®ãããªèŠçŽ ãæ¢ã«ååšããå Žåã«falseãè¿ãããšãå¿ããªãã§ãã ããã 次ã®ããã«èšè¿°ã§ãããããã³ãŒããè€éã«ããããã·ã¥ããŒãã«ãŸãã¯ãã€ããªããªãŒãä»ããŠèŠçŽ ãããã«ãã³ãããå¿ èŠã¯ãããŸããã
if(set.add(item)) { // do something } else { // do something else }
åæ§ã«åé€ããŸãã ãã§ãŒã³
if(set.contains(item)) { set.remove(item); ... }
if(set.contains(item)) { set.remove(item); ... }
ã¯
if(set.remove(item)) { ... }
眮ãæããã
if(set.remove(item)) { ... }
ã
Map.putïŒKãVïŒãMap.removeïŒKïŒãList.setïŒidxãEïŒãList.removeïŒidxïŒã¯åã®ã¢ã€ãã ãè¿ããŸã
åããªãã©ããç¶æ³ã ã³ã¬ã¯ã·ã§ã³å ã®ã¢ã€ãã ãå€æŽãŸãã¯åé€ããã¡ãœããã¯ã以åã®å€ãè¿ããããããã䜿çšããå¿ èŠããããŸãã ããšãã°ã次ã®ããã«æžããªãã§ãã ããã
Item item = myMap.get(key); myMap.put(key, newItem);
åã«æžããŸã
Item item = myMap.put(key, newItem);
ã Mapã®2ã€ã®ãšã³ããªãããŒkey1ãkey2ãšäº€æãããã§ããïŒ äžæå€æ°ã¯å¿ èŠãããŸããã
myMap.put(key1, myMap.put(key2, myMap.get(key1)));
Map.keySetïŒïŒããã³Map.valuesïŒïŒ
äœããã®çç±ã§ãå€ãã®äººã ã¯ã
Map.keySet()
ããã³
Map.values()
ãå ã®Mapã®ãããã³ã°ãè¿ãããšãå¿ããŠããŸããããã«ãããèŠçŽ ãåé€ã§ããŸãïŒMapãå€æŽå¯èœãªå ŽåïŒã Mapã«ç¹å®ã®å€ïŒããã³ä»»æã®ããŒïŒã®ã¬ã³ãŒãã®ã¿ãæ®ãå¿ èŠããããŸããïŒ ãé¡ãïŒ
myMap.values().retainAll(toRetain);
removeIf
ãåäœããJava-8ã§ã¯
removeIf
ã
removeIf
ãŸãïŒ
// Map<String, List<Employee>> perDepartment = employees.stream().collect(groupingBy(Employee::getDepartmentName, HashMap::new, toList())); // 10 perDepartment.values().removeIf(list -> list.size() < 10);
Arrays.asListãããŒã«ãªããŸã
å€ã®ã¿ãã«ã䜿çšããŠããããŸãã¯ã»ãããäœæããå¿ èŠãçããããšããããŸãã ããšãã°ã
name, type, version
ãã£ãŒã«ããæã€Item PoJoãªããžã§ã¯ãããããŸãã ãããã«ã¯ãã§ã«
equals
ãš
hashCode
èšè¿°ãããŠããã
HashSet
ã«è¿œå ã§ããŸããããã¹ãŠåé¡ãããŸããã ãã ããããŒãžã§ã³ãç¡èŠããŠã
name
ãã£ãŒã«ããš
type
ãã£ãŒã«ãã®ã¿ã§ã³ã¬ã¯ã·ã§ã³ããäžæã®ãªããžã§ã¯ããéžæããå¿ èŠããããŸãã æ¢åã®
equals
ãš
hashCode
ã¯
hashCode
ã§ããŸããã ãã®ãããªç¶æ³ã§ã¯ãå€ãã®å Žåã
name
ãš
type
ãã£ãŒã«ãã®ã¿ãæã€å¥ã®ã¯ã©ã¹ãäœæãããããããŒãšããŠäœ¿çšããŸãã ãã ãã1åéãã®æäœã®å Žåã¯ã
Arrays.asList()
ã䜿çšããæ¹ãç°¡åã§ãã
Map<List<Object>, Item> map = new HashMap<>(); for(Item item : items) { map.put(Arrays.asList(item.name, item.type), item); } Collection<Item> unique = map.values();
Arrays.asList()
ã¯ãå¿ èŠãªèŠçŽ æ°ã®ãªã¹ããäœæããŸããããã«ã¯ã
equals
ãš
hashCode
equals
é©åã«å®è£ ãããŠããã ãã§ãããã€ã©ãŒãã¬ãŒãã¯å¿ èŠãããŸããã ãããã£ãŠãä»»æã®é·ãã®ããŒãäœæã§ããnullå€ãšããªããã£ãã¯æ£ããåŠçãããŸãïŒãã¯ã·ã³ã°ã®ãããã§ãïŒã ããŒã®äžéšãšããŠé åãå¿ èŠãªå Žåã«ã®ã¿æ©èœããŸããã
Collections.min / max
äœããã®åºæºã§äœãã®æ倧èŠçŽ ãŸãã¯æå°èŠçŽ ãèŠã€ããæåã§èšè¿°ãããã³ãŒããèŠã€ããããšãã§ããé »åºŠã¯é©ãã¹ãããšã§ãã ãã®ãããªäºçŽ°ãªäœæ¥ã¯ãã£ãšåã«è§£æ±ºãããã¹ãã ãšæãããŸãã å®éãããã¯ãã§ã«é·ãé解決ãããŠããŸããã¡ãœãã
Collections.min
ãš
Collections.max
ãŸãã ã³ã³ãã¬ãŒã¿ãŒãæžãã®ã¯ããŸã䟿å©ã§ã¯ãããŸããã§ããããJava-8ã§ã¯ãã¹ãŠãç°¡åã«ãªããŸããã
ããšãã°ãæ倧å€ã«å¯Ÿå¿ãããããã®ããŒãèŠã€ããå¿ èŠããããŸãã ãã®ããã«æžãïŒ
maxKey = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey();
Stream APIã䜿çšããããšãã§ããŸããã
Collections.max()
å€å°é«éã§ãã Java-8ã䜿çšã§ããã
Entry.comparingByValue()
ãªã©ã®ã³ã³ãã¬ãŒã¿ãŒã䜿çšã§ããªãå Žåã¯ãç°¡åã«èšè¿°ã§ããŸãã
ã¹ã¿ãã¯ããã¯ãã«ãããã·ã¥ããŒãã«ãLinkedList
ãããã®ã¯ã©ã¹ã¯äœ¿çšããªãã§ãã ããã ãããã®å©ç¹ã¯ãããŸããã Stackã®ä»£ããã«ArrayDequeãVectorã®ä»£ããã«ArrayListãHashtableã®ä»£ããã«HashMapã䜿çšããŸãã ã¹ã¬ããã»ãŒããå¿ èŠãªå Žåããšã«ããå©ãã«ã¯ãªããŸããã ããããã9ã€ã¯ãŸã @DeprecatedãšããŒã¯ããŸãïŒ JEP 277ãåç §ïŒã
LinkedListã¯ç¹å¥ãªã±ãŒã¹ã§ãã ãªã³ã¯ããããªã¹ãã«ãã䌌ããã®ã¯ãªããå®éã«åœ¹ç«ã€ãšããäŒèª¬ãããããã§ãã å®éã«ã¯ãLinkedListãArrayListãããåªããŠããç¶æ³ã¯ãå®éã«ã¯ã»ãšãã©ãããŸããã Java-8ããåã®ããŒãžã§ã³ã§ã¯ãäœããã®æ¡ä»¶ã§ãé çªã«äžŠãã§ããªãã¢ã€ãã ãé »ç¹ã«åé€ããå Žåã«ãLinkedListã䟿å©ãªå ŽåããããŸããã Java-8ã§ã¯ããããã®ç®çã®ããã«
List.removeIf
ãç»å ŽããŸããããããã¯ãã¡ããArrayListã§ã¯ããæé©ã«å®è£ ãããŠããŸãïŒèŠçŽ ã¯1åãã移åããŸããïŒã ããŸããŸãªå Žæã§å€ãã®æ¿å ¥ãè¡ãå¿ èŠãããå ŽåïŒã¿ã¹ã¯èªäœã¯ãšããŸããã¯ã§ãïŒãæ¢åã®LinkedListã«æ¿å ¥ãããããæ°ããArrayListãäœæããæ¹ãé«éã§ããå¯èœæ§ãé«ãã§ãã åèŠçŽ ã¯ããŒãå ã®åå¥ã®ãªããžã§ã¯ãã§ããã次ã®èŠçŽ ãšåã®èŠçŽ ãžã®ãªã³ã¯ããããããLinkedListã¯æ°åã®ã¡ã¢ãªãæ¶è²»ããããšã«æ³šæããŠãã ããã LinkedListã¯ãã±ãŒã¹ã¹ã¿ãã£ãšããŠã®ã¿äœ¿çšã§ããŸãã
ä»æ¥ã¯ä»¥äžã§ãã åãã§ããã°ã©ã ïŒ