Created
October 17, 2019 01:50
-
-
Save dan085/e18b473343272a88da6a1d52d7f13ff5 to your computer and use it in GitHub Desktop.
Compare kotlin two list array.. update delete or update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| //// usuarios del celular registrados | |
| val sync_list_name_mobile = ArrayList<HashMap<String, String>>() | |
| val contactMap_initial = HashMap<String, String>() | |
| contactMap_initial.put("KEY_NAME", "PEDRO") | |
| contactMap_initial.put("mobile", "1111") | |
| sync_list_name_mobile.add(contactMap_initial) | |
| val contactMap_initial22 = HashMap<String, String>() | |
| contactMap_initial22.put("KEY_NAME", "PEDRO") | |
| contactMap_initial22.put("mobile", "4444") | |
| sync_list_name_mobile.add(contactMap_initial22) | |
| //// usuarios de la base datos | |
| val list_friend_inside_db = ArrayList<HashMap<String, String>>() | |
| val contactMap_2 = HashMap<String, String>() | |
| contactMap_2.put("KEY_NAME", "JUAN") | |
| contactMap_2.put("mobile", "2222") | |
| list_friend_inside_db.add(contactMap_2) | |
| val contactMap_23 = HashMap<String, String>() | |
| contactMap_23.put("KEY_NAME", "DANIEL") | |
| contactMap_23.put("mobile", "1111") | |
| list_friend_inside_db.add(contactMap_23) | |
| val contactMap_233 = HashMap<String, String>() | |
| contactMap_233.put("KEY_NAME", "ROSSS") | |
| contactMap_233.put("mobile", "1111") | |
| list_friend_inside_db.add(contactMap_233) | |
| // val aa = U.Chain(list_friend_inside_db).uniq() | |
| // println("unique $aa") | |
| // String[] myStringArray = new String[]{"1111"}; | |
| /// verifica si se elimino un usuario | |
| // U.Chain<String> filterList = U.chain(sync_list_name_mobile).map(b -> b.get("KEY_MOBILE")); | |
| val filterList = sync_list_name_mobile.map { b->b.get("KEY_MOBILE") } | |
| val notIn_delete_user = list_friend_inside_db.filter { arg-> filterList.indexOf(arg.get("KEY_MOBILE"))<0 } | |
| println("notIn_delete_user $notIn_delete_user") | |
| val filterList_ = list_friend_inside_db.map { b->b.get("KEY_MOBILE") } | |
| val notInB_add_user = sync_list_name_mobile.filter { arg-> filterList_.indexOf(arg.get("KEY_MOBILE"))<0} | |
| println("notInB_add_user $notInB_add_user") | |
| val result_ = sync_list_name_mobile.filter { o1-> | |
| list_friend_inside_db.any {o2-> | |
| o1["mobile"] === o2["mobile"] && o1["KEY_NAME"] !== o2["KEY_NAME"] | |
| } | |
| } | |
| println("update user $result_") | |
| // val result_new = | |
| // U.chain(result_).filter({ a -> U.indexOf(arrayOf<Chain>(notInB_add_user), a) < 0 }) | |
| //System.out.println("result_ " + result_); | |
| if (notIn_delete_user.size > 0) { | |
| notIn_delete_user.forEach { arg -> System.out.println("forEach user delete " + arg.get("KEY_MOBILE")) } | |
| } | |
| if (notInB_add_user.size > 0) { | |
| notInB_add_user.forEach { arg -> System.out.println("forEach user add " + arg.get("KEY_MOBILE")) } | |
| } | |
| if (result_.size > 0) { | |
| result_.forEach { arg -> System.out.println("forEach user update" + arg.get("KEY_MOBILE")) } | |
| } | |
| } | |
| override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| menuInflater.inflate(R.menu.menu_main, menu) | |
| return true | |
| } | |
| override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
| // Handle action bar item clicks here. The action bar will | |
| // automatically handle clicks on the Home/Up button, so long | |
| // as you specify a parent activity in AndroidManifest.xml. | |
| return when (item.itemId) { | |
| R.id.action_settings -> true | |
| else -> super.onOptionsItemSelected(item) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment