directory
Collectors, Collections and Collectors
2. Classification of apis in Collections
2.1 Sort Operations
2.2 Searching for replacement
2.3 Checkedxxx Checks the collection
2.4 EMPtyxxx Returns an empty collection
2.5 SynchronizedXXX Synchronization set
2.6 unmodifiableXxx Immutable collection
3. Conclusion:
Today I’m going to write about Collections, which is the official Java utility class for collection classes. It’s also a tool for solving a lot of development problems, but there are still a lot of people who don’t know or don’t understand the API.
Collectors, Collections and Collectors
This may be a problem encountered by beginners, and all three are related to collect. The distinction is simple.
1.Collectors are mainly used to process Java Streams, for example, Collectors. ToSet ().
2.Collection is the related interface class of all Collection classes, such as the commonly used Set,List,Map.
3.Collections is the official Java utility class, which I’ll focus on today.
You can remember it by words, Collectors. The Collection Collection. Collections A collection of collection operations.
2. Classification of apis in Collections
2.1 Sort Operations
static <T>boolean addAIl(Collection <? super T>c,T... Elements) adds all specified elements to the specified collection. Static void reverse(List List) Reverses the order of elements in the specified List collection. Static void shuffle(List List) Static void sort(List List) sort the elements of a List by their natural order static void swap(List List,int I, Int j) will swap the element at I and the element at j in the specified List.Copy the code
2.2 Searching for replacement
static int binaryScarch ( List list,Object key)
Use dichotomy to search the index of the specified object in the List. The elements in the List must be ordered
static Object max(Collection col)
Returns the largest element in the given collection
static Object min (Collection col)
Returns the smallest element of the given set
Static Boolean replaccAll (List List,Object oldVal,Object newVal) replaces all old values oldVal in the List collection with a new newVal
2.3 Checkedxxx Checks the collection
Returns a collection of type checks performed when a type is set or added
Usage:
ArrayList list = Lists.newArrayList();
list.add(new Player("Parsley"));
// The returned safeList will be type checked when added
List safeList = Collections.checkedList(list, Player.class);
/ / will examine type at this time, is not a type of Player, an exception is thrown. Java lang. ClassCastException:
safeList.add("xxx");
Copy the code
Source code analysis:
public void add(E e) { i.add(typeCheck(e)); } E typeCheck(Object o) { if (o ! = null && ! type.isInstance(o)) throw new ClassCastException(badElementMsg(o)); return (E) o; }Copy the code
This set of functions can be used in development to avoid exceptions due to carelessness or multi-party cooperation.
2.4 EMPtyxxx Returns an empty collection
This operation returns an empty set that cannot be added or deleted.
Usage:
Public List<Player> getSameUnionFriend(int roleId,int uid){// If the current Player does not have a guild, Null list if (uid == 0){return collections.emptyList (); } // todo .... }Copy the code
The advantage of this is that 1. You don’t have to deal with returning NULL. 2. Do not generate a new empty list to avoid occupying memory, because the returned empty list is used by the entire application. Did you find any good in him?
Source code analysis:
public static final List EMPTY_LIST = new EmptyList<>();
Copy the code
2.5 SynchronizedXXX Synchronization set
Usage:
ArrayList<Player> list = Lists.newArrayList(); // TODO; ----- List<Player> synList = Collections.synchronizedList(list);Copy the code
Synchronize the incoming collection. Resolve thread synchronization issues.
Source:
final Object mutex; // Object on which to synchronize
public void add(int index, E element) {
synchronized (mutex) {list.add(index, element);}
}
Copy the code
This approach is equivalent to repackaging the collection, adding a lock inside.
Note: this locking method can cause problems, if the collection is a single operation is fine, if you want to synchronize the collection state to do some non-meta operations are problematic. Use Synchronize (list) again to be careful.
2.6 unmodifiableXxx Immutable collection
Usage: An incoming collection cannot be changed upon return.
ArrayList<Player> list = Lists.newArrayList(); // TODO; ----- List<Player> synList = Collections.unmodifiableList(list);Copy the code
Source:
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
public E remove(int index) {
throw new UnsupportedOperationException();
}
Copy the code
The collection passed in cannot be changed. Because the wrapper class hides the collection’s methods, it throws an exception.
Note: You cannot modify the contents of the list, but you can modify the contents of the elements in the list. For example:
ArrayList<Player> list = Lists.newArrayList(); List. add(new Player(" 中 国 小 游戏")); List<Player> synList = Collections.unmodifiableList(list); Player player = synList.get(0); Player. Elegantly-named setName (" parsley ");Copy the code
3. Conclusion:
Collections supports generic operations on individual elements in addition to Collections.
If you want to synchronize collections, use synchronizedxxx.
If you do not want to modify the collection, use unmodifiableXxx.
If you want to use empty collections, use emptyXxx.
If you want to keep the collection pure use checkedxxx.
You may hurt yourself at first, but if you don’t learn you’ll never use a sword. There’s nothing hard about technology, it’s the fear of not knowing.
Original is not easy, ask to share, point to see, attention, quality three, thank you for your support.