最近找了之前本人写的JAVA基础知识的汇合局部,整顿了进去,分享给大家,货色很简略。
list转换为数组
Object[] objArray = list.toArray();//转换为指定类型数组MyClass[] a1 = list.toArray(new MyClass[0]);MyClass[] a2 = new MyClass[3];MyClass[] a3 = list.toArray(a2);
数组转换为list
Collection<MyClass> listCol = Arrays.asList(a3);list.forEach(e-> System.out.println("asList:"+e.label));
Collection根本汇合
List列表汇合,保护某些订单概念的汇合(ArrayList,LinkedList)
Queue队列汇合,存在头元素,能够获取下一个。(ArrayDuque,PriorytyQueue)
Set汇合,没有反复的汇合(HashSet,TreeSet,EnumSet)
SortedSed汇合,排好序的Set汇合
Iterator写法
Iterator<Product> iterator = collection.iterator();while(iterator.hasNext()){ Product next = iterator.next(); System.out.println(next);}
ArrayList汇合,能够随机取任何指定index的汇合,然而随机插入不好,例如在200和201个之间插入一个
LinkList汇合,双向链表和队列组成,在两个成员之间插入十分不便,然而随机拜访很慢,所以跳到第150个元素,第300个元素效率很低。
HashSet,哈希表的汇合,应用每个对象实例的哈希值
TreeSet,树汇合,实现均衡二叉树的汇合,每一个元素都是惟一的,批改或者搜寻很慢,然而依照排序程序拜访成员很容易。
SortedSet接口
E first();E last();SortedSet<E> tailSet(E fromElement);//返回从fromElement开始的,包含fromElementSortedSet<E> headSet(E toElement);//返回toElement之前的,不包含toElementSortedSet<E> subSet(E fromElement,E toElement);//返回从fromElement开始的toElement之前的,包含fromElement,不包含toElement
NavigableSet接口
E lower(E e);//返回此set中小于给定元素的最大元素E higher(E e);//返回此set中大于给定元素的最小元素E floor(E e);//返回此汇合中小于或等于给定元素的最大元素E ceiling(E e);//返回此set中大于或等于给定元素的最小元素E poolFirst();//检索并删除第一个最小元素。E poolLast();//检索并删除最初一个最高元素
Queue接口
boolean offer(E e);//容量满了,返回异样,只有增加胜利才返回trueboolean add(E e);//容量满了增加失败时候返回false,增加胜利返回trueE remove();//取出并删除队列的元素,如果队列为空返回异样,有值则返回值E poll();//如果队列为空返回null,有值则返回值E element();//取出最初一个元素,但并不从队列里删除,没有值则返回异样E peek();//取出最初一个元素,但并不从队列里删除,没有值则返回null
Deque
boolean offerFirst(E e);boolean offerLast(E e);boolean addFirst(E e);boolean addLast(E e);E removeFirst();E removeLast();E pollFirst();E pollLast();void push(E e);//LIFO的退出办法E pop();//LIFO的取出办法
PriorytiQueue
public class PriorityQueueTest { private static final Queue<User> queue = new PriorityQueue<>(Comparator.comparing(User::getId)); public static void main(String[] args) { queue.add(new User(3L)); queue.add(new User(1L)); queue.add(new User(2L)); User user; while((user = queue.poll())!=null){ System.out.println(user); } }}//输入User{id=1}User{id=2}User{id=3}
Map包含,HashMap,TreeMap,EnumMap
汇合排序的两种实现
1.实体类通过实现Comparable接口排序
public class MyClass implements Comparable<MyClass> { String label,value; public MyClass(String label,String value){ this.label = label; this.value = value; } @Override public boolean equals(Object o){ MyClass other = (MyClass) o; return value.equalsIgnoreCase(other.value); } @Override public String toString() { return label + " | " + value; } @Override public int compareTo(MyClass other) { return value.compareToIgnoreCase(other.value); }}TreeSet<MyClass> tree = new TreeSet<>();tree.add(new MyClass("2222","ghi"));tree.add(new MyClass("3333","abc"));tree.add(new MyClass("1111","def"));tree.forEach(e-> System.out.println(e));3333 | abc1111 | def2222 | ghi
2.通过创立比拟器实现Comparator接口排序
public class MyComparator implements Comparator<MyClass> { @Override public int compare(MyClass x, MyClass y) { return x.label.compareToIgnoreCase(y.label); }}TreeSet<MyClass> tree2 = new TreeSet<>(new MyComparator());tree2.add(new MyClass("2222","ghi"));tree2.add(new MyClass("3333","abc"));tree2.add(new MyClass("1111","def"));tree2.forEach(e-> System.out.println(e));1111 | def2222 | ghi3333 | abc
Map根底Map汇合
SortedMap接口 依照key排序的Map汇合
NavigableMap接口
HashMap通用Map汇合
TreeMap,自均衡的树汇合,依照key排序,能够依照实现Comparable接口排序或者创立比拟器实现Comparator接口。
LinkedHashMap
SortedMap
K firstKey();K lastkey();SortedMap<K,V> tailMap(E fromKey);//获取从fromKey开始的比其大的SortedMap,包含fromKeySortedMap<K,V> headMap(E toKey);//获取到toKey完结的比其小的SortedMap,不包含toKeySortedMap<K,V> subMap(E fromKey, E toKey);//获取从fromKey开始的比其大的toKey完结的比其小的SortedMap,包含fromKey,不包含toKey
NavigableMap
Map.Entry<K,V> firstEntry();Map.Entry<K,V> lastEntry();Map.Entry<K,V> poolFirstEntry();//获取到第一个entry,并从map中去掉Map.Entry<K,V> poolLastEntry();//获取到最初一个entry,并从map中去掉Map.Entry<K,V> lowerEntry(K key);//获取严格小于key的最大的EntryMap.Entry<K,V> higherEntry(K key);//获取严格大于key的最小的EntryK lowerKey(K key);//获取严格小于key的最大的keyK higherKey(K key);//获取严格大于key的最小的keyMap.Entry<K,V> floorEntry(K key);//获取严格小于等于key的最大的EntryMap.Entry<K,V> ceilingEntry(K key);//获取严格大于等于key的最小的EntryK floorKey(K key);//获取严格小于等于key的最大的keyK ceilingKey(K key);//获取严格大于等于key的最小的keyNavigableMap<K,V> desendingMap();//获取反转的navigableMapNavigableSet<K> desendingKeySet();//获取反转的KeySetNavigableSet<K> navigableKeySet();//获取失常的keySetNavigableMap<K,V> tailMap(E fromKey, boolean incl);//获取从fromKey开始的比其大的SortedMap依据incl值,是否包含fromKeyNavigableMap<K,V> headMap(E toKey, boolean incl);//获取到toKey完结的比其小的SortedMap依据inclu值,是否包含toKey//获取从fromKey开始的比其大的toKey完结的比其小的SortedMap,依据两个incl值,是否包含fromKey,toKeyNavigableMap<K,V> subMap(E fromKey, boolean fromIncl,E toKey, boolean toIncl);
罕用办法
put,减少key和value
putIfAbsent,如果没有该key,则减少key和value,如果有则不放
get获取key的值,如果没有key则返回null
getOrDefault获取key的值,如果没有则返回咱们提供的默认的value
values(),返回所有value的汇合,获取到的汇合删除掉其中的一个value值后,整个map中也会删掉该key-value值,但不能像value汇合中增加值,会报exception
keySet(),返回所有key的汇合,获取到的汇合删除掉其中的一个key值后,整个map中也会删掉该key-value值,但不能像key汇合中增加值,会报exception
entrySet(),返回map的key-value汇合,能够循环输入,也能够批改value值
forEach,lambda表达式的循环每一个条目
replaceAll,通过lambda表达式并批改每个key关联的值
Map<String,String> map = new HashMap<>();map.put("2222","ghi");map.put("3333","abc");map.put("1111","def");String s1 = map.get("3333");System.out.println("s1:"+s1);s1:abcString s2 =map .get("9999");System.out.println("s2:"+s2);s2:nullString s3= map.getOrDefault("9999","xyz");System.out.println("s3:"+s3);s3:xyzmap.forEach((k,v)-> System.out.println(k+" | "+v));2222 | ghi3333 | abc1111 | defmap.replaceAll((k,v)->v.toUpperCase());map.forEach((k,v)-> System.out.println(k+" | "+v));2222 | GHI3333 | ABC1111 | DEFpublic static void main(String[] args) { Map<String,Object> map =new HashMap<>(); map.put("key","value"); map.put("key1","value1"); Set<Map.Entry<String, Object>> entries = map.entrySet(); entries.forEach(entry-> System.out.println(entry.getKey()+"->"+entry.getValue())); entries.forEach(entry-> entry.setValue("tttt")); System.out.println(map.toString());}//输入key1->value1key->value{key1=tttt, key=tttt}
SortedMap办法
firstKey返回第一个key
lastKey返回最初一个key
headMap返回另一个map,其中蕴含所有key小于我传入的指定key,不蕴含我传入的key
tailMap返回另一个map,其中蕴含所有key大于我传入的指定key,蕴含我传入的key
subMap,返回一个map,蕴含所有大与等于开始key,小于完结key的
SortedMap<String,String> map1 = new TreeMap<>();map1.put("2222","ghi");map1.put("3333","abc");map1.put("1111","def");map1.put("6666","xyz");map1.put("4444","mno");map1.put("5555","pqr");map1.forEach((k,v)-> System.out.println(k+" | "+v));1111 | def2222 | ghi3333 | abc4444 | mno5555 | pqr6666 | xyzSortedMap<String,String> hMap = map1.headMap("3333");hMap.forEach((k,v)-> System.out.println(k+" | "+v));1111 | def2222 | ghiSortedMap<String,String> tMap = map1.tailMap("3333");tMap.forEach((k,v)-> System.out.println(k+" | "+v));3333 | abc4444 | mno5555 | pqr6666 | xyzSortedMap<String,String> subMap = map1.subMap("2222","5555");subMap.forEach((k,v)-> System.out.println(k+" | "+v));2222 | ghi3333 | abc4444 | mno
Collection办法,rotate(List<?> list, int distance)列表中最初一个元素并将其挪动distance个地位,负数向后,正数向前挪动
List<User> list = new ArrayList<>();User user1 = new User(1L);User user2 = new User(2L);User user3 = new User(3L);list.add(user1);list.add(user2);list.add(user3);Collections.rotate(list,1);System.out.println(list);//输入[User{id=3}, User{id=1}, User{id=2}]
shuffle随机排列列表元素
Collections.shuffle(list);System.out.println(list);//输入[User{id=2}, User{id=1}, User{id=3}]
sort排序
List<User> list = new ArrayList<>();User user1 = new User(1L);User user2 = new User(2L);User user3 = new User(3L);list.add(user1);list.add(user3);list.add(user2);Collections.sort(list,Comparator.comparing(User::getId));System.out.println(list);//输入[User{id=1}, User{id=2}, User{id=3}]//JDK1.8也能够另外写法,list自带比拟list.sort(Comparator.comparing(User::getId));
单例模式
Set<Integer> singleton = Collections.singleton(1);List<String> one = Collections.singletonList("one");Map<Integer, String> one1 = Collections.singletonMap(1, "one");//默认空的,以下均不可批改List<Object> objects = Collections.emptyList();Map<Object, Object> objectObjectMap = Collections.emptyMap();Set<Object> objects1 = Collections.emptySet();//unmodifiableList操作后的one2的list也不可批改List<User> one2 = Collections.unmodifiableList(list);User min = Collections.min(list,Comparator.comparing(User::getId));System.out.println(min);//输入1