ÔÚjavaÖÐʹÓÃίÍÐʵÏÖmap/filter
ÔÚjavaÖУ¬¼ÙÉèÄãÓÐÒ»¸öuser ¶ÔÏóµÄlist£¬´Ëuser¶ÔÏó·â×°ÁËÓû§µÄid, first name, last name and age. È»ºóÄãÏëµ÷ÓÃÒ»¸öweb service(eg. UserService.deleteUsersByIds(List<Integer> userIds) ȥɾ³ýÊý¾Ý¿âÖÐÖ¸¶¨µÄÕâЩuser¡£ ÌýÆðÀ´Ëƺõ²»Ì«À§ÄÑ£¬²»ÊÇô? ÄãËùÐèÒªÖ»Êǽ« List<User> ת»¯³ÉList<Integer>¡£ ËùÓÐÄãÏÈдÏÂÏÂÃæµÄ´úÂë:
List<Integer> ids = new ArrayList<Integer>(users.size());
for (User user : users) {
ids.append(user.getId());
}
È»ºóÄã¼ÌÐøʹÓÃÄúµÄID list£¬ÕâÒ»Çж¼·Ç³£ºÃ¡£
µ«ÊÇ£¬Á½·ÖÖÓºó£¬Äã»á·¢ÏÖ×Ô¼º²»µÃ²»Îª»ñÈ¡Ò»¸öStringÐ͵ÄÓû§ÃûÁбí¶øÖØж¨ÒåÒ»¸öAPI·½·¨¡£
List<String> names = new ArrayList<String>(users.size());
for (User user : users) {
names.append(new StringBuilder(user.getFirstNam()).append(" ").append("user.getLastName()));
}
»òÐíÄ㻹ÐèÒªÒ»¸ö21ËêÒÔϵÄuser list¡£
List<User> newUsers= new ArrayList<User>(users.size());
for (User user : users) {
if(user.getAge()<21){
newUsers.add(user);
}
}
»òÐíÄ㻹ÐèÒªµÄ¸ü¶à¡£¡£¡£¡£
ºÜ¿ìÄã·¢ÏÖÕâ¼þÊÂÇé±äµÃºÜ·¦Î¶¡£
¸ù¾ÝÄ¿Ç°ÇéÐΣ¬Äã»á·¢ÏÖÕâÀïÓÐ2¸ö·Ç³£ÖØÒªµÄ¹¦ÄÜ map ºÍfilter
1.map(coll, f)£º±éÀú¼¯ºÏcoll£¬ ÔÚ¼¯ºÏÖе÷ÓÃfµÄ·½·¨£¬½«fµÄ·½·¨´¦Àí½á¹û×°ÈëÒ»¸ö¼¯ºÏ·µ»Ø£¨Ò²¾ÍÊǵ÷ÓÃfÖеķ½·¨×é×°ÁíÍâÒ»¸öÐèÒªµÄ¼¯ºÏ£¬È»ºó·µ»Ø£©¡£
2.filter(coll, f)£º±éÀú¼¯ºÏcoll£¬ ÔÚ¼¯ºÏÖе÷ÓÃfµÄ·½·¨£¬µ±f(element) returns true ½«½á¹û×°ÈëÒ»¸ö¼¯ºÏ·µ»Ø¡£
ΪÁË´ïµ½ÉÏÃæµÃµ½Id µÄListÎÒÃÇ¿ÉÄÜÐèÒªÕâÑùÖÐ
<fromType, ToType> List<ToType> map(ArrayList<fromType> list) {
List<ToType> retval = new ArrayList<ToType>(list.size());
for (fromType item : list) {
[...]
}
return retval;
}
ÕâÑù¿´ÆðÀ´Ëƺõ¸úÏÂÃæµÄcodeûÓÐʲôÇø±ð¡£
1 List<Integer> ids = new ArrayList<Integer>(users.size());
2
for (User user : users) {
3
ids.append(user.getId());
4
}
ÓÉÓÚjava²»ÌṩÓïÑÔ¼¶±ðµÄίÍУ¬Òò´ËÎÒÃÇÐèҪΪÕâ¸öίÅÉÌṩһ¸ö½Ó¿Ú
1
interface
Ïà¹ØÎĵµ£º
abstract classºÍinterfaceÊÇJavaÓïÑÔÖжÔÓÚ³éÏóÀඨÒå½øÐÐÖ§³ÖµÄÁ½ÖÖ»úÖÆ£¬ÕýÊÇÓÉÓÚÕâÁ½ÖÖ»úÖƵĴæÔÚ£¬²Å¸³ÓèÁËJavaÇ¿´ó
µÄÃæÏò¶ÔÏóÄÜÁ¦¡£abstract classºÍinterfaceÖ®¼äÔÚ¶ÔÓÚ³éÏóÀඨÒåµÄÖ§³Ö·½Ãæ¾ßÓкܴóµÄÏàËÆÐÔ£¬ÉõÖÁ¿ÉÒÔÏ໥Ìæ»»£¬Òò´ËºÜ¶à¿ª·¢Õß
ÔÚ½øÐгéÏóÀඨÒåʱ¶ÔÓÚabstract classºÍinterface ......
1.Listת»»³ÉΪÊý×é¡££¨ÕâÀïµÄListÊÇʵÌåÊÇArrayList)
¡¡¡¡µ÷ÓÃArrayListµÄtoArray·½·¨¡£
¡¡¡¡toArray
¡¡¡¡public <T> T[] toArray(T[] a)·µ»ØÒ»¸ö°´ÕÕÕýÈ·µÄ˳Ðò°üº¬´ËÁбíÖÐËùÓÐÔªËصÄÊý×飻·µ»ØÊý×éµÄÔËÐÐʱÀàÐ;ÍÊÇÖ¸¶¨Êý×éµÄÔËÐÐʱÀàÐÍ¡£Èç¹ûÁбíÄÜ·ÅÈëÖ¸¶¨µÄÊý×飬Ôò·µ»Ø·ÅÈë´ËÁбíÔªËصÄÊý×é¡£·ñÔò£¬½«¸ù¾ ......
Ï̵߳Ĵ´½¨ºÍÆô¶¯
JavaÓïÑÔÒѾÄÚÖÃÁ˶àÏß³ÌÖ§³Ö£¬ËùÓÐʵÏÖRunnable½Ó¿ÚµÄÀ඼¿É±»Æô¶¯Ò»¸öÐÂỊ̈߳¬ÐÂÏ̻߳áÖ´ÐиÃʵÀýµÄrun()·½·¨£¬µ±run()·½·¨Ö´ÐÐÍê±Ïºó£¬Ï߳̾ͽáÊøÁË¡£Ò»µ©Ò»¸öÏß³ÌÖ´ÐÐÍê±Ï£¬Õâ¸öʵÀý¾Í²»ÄÜÔÙÖØÐÂÆô¶¯£¬Ö»ÄÜÖØÐÂÉú³ÉÒ»¸öÐÂʵÀý£¬ÔÙÆô¶¯Ò»¸öÐÂÏ̡߳£
ThreadÀàÊÇʵÏÖÁËRunnable½Ó¿ÚµÄÒ»¸öʵÀý£¬Ëü´ú±í ......
StringתCharacterÊý×飬ÓÃCharacterµÄisDigitºÍisLetterº¯ÊýÈ¥Åжϡ£
public static boolean isNumeric(String str) {
for (int i = str.length(); i = 0;) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isNumeric(String str) {
Pattern patt ......
1¡¢ÖÐÎÄÎÊÌâµÄÀ´Ô´
ÓÉÓÚJava
Óï
ÑÔÄÚ²¿²ÉÓÃUNICODE±àÂ룬ËùÒÔÔÚJAVA
³ÌÐòÔËÐÐʱ£¬¾Í´æÔÚ×ÅÒ»¸ö´ÓUNICODE±àÂëºÍ¶ÔÓ¦µÄ²Ù×÷ϵͳ¼°ä¯ÀÀÆ÷Ö§³ÖµÄ±àÂë¸ñʽ
ת
»»ÊäÈë¡¢Êä³öµÄÎÊÌ⣬Õâ¸öת»»¹ý³ÌÓÐ×ÅһϵÁеIJ½Ö裬Èç¹ûÆäÖÐÈκÎÒ»²½³ö´í£¬ÔòÏÔʾ³öÀ´µÄºº×־ͻá³öÊÇÂÒÂ룬Õâ¾ÍÊÇÎÒÃdz£¼ûµÄJAVA
ÖÐ
ÎÄÎÊÌâ¡£¼¸ºõËùÓÐµÄ ......