最近找到了自己之前写的JAVA基础知识合集,整理了一下,分享给大家。事情很简单。ConvertlisttoarrayObject[]objArray=list.toArray();//转换为指定类型数组MyClass[]a1=list.toArray(newMyClass[0]);MyClass[]a2=newMyClass[3];MyClass[]a3=list.toArray(a2);数组转换为listCollectionlistCol=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写法Iteratoriterator=collection.iterator();while(iterator.hasNext()){Productnext=迭代器.next();System.out.println(next);}ArrayList集合,可以随机选择指定索引的任意集合,但是随机插入不好,比如在200到201之间插入一个LinkList集合,由双向链表和队列组成,在两个成员之间插入很方便,但是随机访问很慢,所以跳到第150个元素,第300个元素效率很低。HashSet,哈希表的集合,使用每个对象实例的哈希值命令。SortedSetinterfaceEfirst();Elast();SortedSettailSet(EfromElement);//从fromElement返回,包括fromElementSortedSetheadSet(EtoElement);//返回之前的toElement,不包括toElementSortedSetsubSet(EfromElement,EtoElement);//返回toElement之前的fromElement,包括fromElement,不包括toElementNavigableSetinterfaceElower(Ee);//返回这个set中小于给定元素ElementEhigher(Ee);//返回集合中大于给定元素的最小元素Efloor(Ee);//返回集合中小于或等于给定元素ceiling(E)的最大元素Ee);//返回此集合中大于或等于给定元素E的最小元素poolFirst();//检索并删除第一个最小的元素。EpoolLast();//获取并删除最后一个最高元素Queueinterfacebooleanoffer(Ee);//容量已满,返回异常,只返回truebooleanadd(Ee);//容量已满andadd失败则返回false,成功则返回trueEremove();//移除并删除队列的元素,如果队列为空,则返回异常,如果有值,returnvalueEpoll();//如果队列为空,则返回null,如果有则returnvalueEelement();//取出最后一个元素,但不从队列中删除,如果没有值,返回异常Epeek();//取出最后一个元素,但不从队列中删除,没有值然后返回nullDequebooleanofferFirst(Ee);booleanofferLast(Ee);booleanaddFirst(Ee);booleanaddLast(Ee);EremoveFirst();EremoveLast();EpollFirst();EpollLast();voidpush(Ee);//后进先出添加方法Epop();//后进先出获取方法PriorytiQueuepublicclassPriorityQueueTest{privatestaticfinalQueuequeue=newPriorityQueue<>(Comparator.comparing(User::getId));publicstaticvoidmain(String[]args){queue.add(newUser(3L));queue.add(新用户(1L));queue.add(新用户(2L));用户用户;while((user=queue.poll())!=null){System.out.println(user);}}}//输出User{id=1}User{id=2}User{id=3}Map包括,HashMap、TreeMap、EnumMap集合排序的两种实现1.实体类通过实现Comparable接口排序publicclassMyClassimplementsComparable{Stringlabel,value;publicMyClass(Stringlabel,Stringvalue){this.label=label;this.value=值;}@Overridepublicbooleanequals(Objecto){MyClassother=(MyClass)o;返回值.equalsIgnoreCase(other.value);}@OverridepublicStringtoString(){返回标签+“|”+值;}@OverridepublicintcompareTo(MyClassother){returnvalue.compareToIgnoreCase(other.value);}}TreeSettree=newTreeSet<>();tree.add(newMyClass("2222","ghi"));tree.add(newMyClass("3333","abc"));tree.add(newMyClass("1111","def"));tree.forEach(e->System.out.println(e));3333|abc1111|def2222|ghi2.通过创建比较器实现比较器接口排序public类MyComparator实现Comparator{@Overridepublicintcompare(MyClassx,MyClassy){returnx.label.compareToIgnoreCase(y.label);}}TreeSettree2=newTreeSet<>(newMyComparator());tree2.add(newMyClass("2222","ghi"));tree2.add(newMyClass("3333","abc"));tree2.add(newMyClass("1111","def"));tree2.forEach(e->System.out.println(e));1111|def2222|ghi3333|abcMap基础Map集合SortedMap接口排序bykeyMap集合NavigableMap接口HashMap通用Map集合TreeMap,自平衡树集合,key排序,可以通过实现Comparable接口或者创建比较器实现Comparator接口进行排序LinkedHashMapSortedMapKfirstKey();Klastkey();SortedMaptailMap(EfromKey);//获取从fromKey开始的较大的SortedMap,包括fromKeySortedMapheadMap(EtoKey);//获取较小的SortedMap以toKey结尾的SortedMap,不包括toKey。,excludingtoKeyNavigableMapMap.EntryfirstEntry();Map.EntrylastEntry();Map.EntrypoolFirstEntry();//获取第一个entry,并从RemoveMap。EntryfromthemappoolLastEntry();//获取最后一个条目,并从map中移除Map.EntrylowerEntry(Kkey);//获取严格小于的最大值thekeyEntryMap.EntryhigherEntry(Kkey);//获取最小的严格大于key的EntryKlowerKey(Kkey);//获取最大的严格小于keyK的keyKhigherKey(Kkey);//获取严格大于最小的keyMap.EntryfloorEntry(Kkey);//获取最大的EntryMap.EntryceilingEntry(Kkey)严格小于等于thekey;//获取严格大于或等于Key的最小EntryKfloorKey(Kkey);//获取最大的key且严格小于或等于keyKceilingKey(Kkey);//获取最小的key严格大于或等于keyNavigableMapdesendingMap();//得到反向的navigableMapNavigableSetdesendingKeySet();//获取倒置KeySetNavigableSetnavigableKeySet();//获取普通keySetNavigableMaptailMap(EfromKey,booleanincl);//获取从fromKey开始的比例到incl值,大SortedMap是否包含fromKeyNavigableMapheadMap(EtoKey,booleanincl);//根据inclu值,以toKey结尾的SortedMap是否包含toKey//获取从fromKey开始的SortedMapwhosetoKey大于它ends小于它。根据这两个incl值判断是否包含fromKey,toKeyNavigableMapsubMap(EfromKey,booleanfromIncl,EtoKey,booleantoIncl);常用方法put,增加key和valueputIfAbsent,如果没有这个key,则增加key和value,如果有,keepget获取key的值,如果没有key,返回nullgetOrDefault获取key的值key,如果没有,返回我们提供的默认值values(),返回所有值的集合。在获取到的集合中删除其中一个value值后,整个map中的key-value值也会被删除,但是不能像value集合那样增加一个值。会报异常keySet(),返回所有的key,在获取到的collection中删除其中一个key-value后,整个map中的key-value值也会被删除,但是不能像keyset那样添加一个value,并且会报exceptionentrySet()并返回mapCollection的key-value,可以循环输出,或者修改forEach的值,lambda表达式循环的每个entryreplaceAll,通过lambda表达式修改关联的值每个键Mapmap=newHashMap<>();map.put("2222","ghi");map.put("3333","abc");map.put("1111","def");Strings1=map.get("3333");System.out.println("s1:"+s1);s1:abcStrings2=map。get("9999");System.out.println("s2:"+s2);s2:nullStrings3=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|DEFpublicstaticvoidmain(String[]args){地图map=newHashMap<>();map.put("key","value");map.put("key1","value1");Set>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返回第一个keylastKey返回最后一个keyheadMap返回另一个map,里面包含了所有小于我传入的指定key的key,但是不包含我传入的keytailMap,返回另一个map,里面包含了所有大于我传入的指定key的key,包括我传入的keysubMap,以及返回包含SortedMapmap1=newTreeMap<>();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|xyzSortedMaphMap=map1.headMap("3333");hMap.forEach((k,v)->System.out.println(k+"|"+v));1111|def2222|ghiSortedMaptMap=map1.tailMap("3333");tMap.forEach((k,v)->System.out.println(k+"|"+v));3333|abc4444|mno5555|pqr6666|xyzSortedMapsubMap=map1.subMap("2222","5555");subMap.forEach((k,v)->System.out.println(k+"|"+v));2222|ghi3333|abc4444|mnoCollection方法,rotate(List>list,intdistance)列表中的最后一个元素并将其移动distance位置,正数向后,负数向前MoveListlist=newArrayList<>();Useruser1=newUser(1L);Useruser2=newUser(2L);Useruser3=newUser(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}]sortsortListlist=newArrayList<>();Useruser1=newUser(1L);Useruser2=newUser(2L);Useruser3=newUser(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));单例模式Setsingleton=Collections.singleton(1);Listone=Collections.singletonList("one");Mapone1=Collections.singletonMap(1,"one");//defaultempty,以下都不能修改List