kotlin和java和hashmap转换
生活随笔
收集整理的這篇文章主要介紹了
kotlin和java和hashmap转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
碰到一個需求,掃描wifi,然后把掃到的wifi顯示在list中,但是掃出來的wifi總是重復,為了實現去重效果,把list遍歷,存儲成map,然后利用map自身的特性,達到去重的目的,然后拿到不重復的map wifi列表,然后再次遍歷map,把符合條件的wifi信息,存儲到新的list中,然后展示這個list,可以實現wifi列表實時掃描顯示
step1:
step2:
package com.example.demoanalytic;public class StudentBean {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} }step3:測試 java代碼
package com.example.demoanalytic;import android.net.wifi.ScanResult; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log;import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List;public class MapThreeActivity extends AppCompatActivity {private CacheMap<String, StudentBean> cacheMap = new CacheMap<>();private CacheMap<String, String> map = new CacheMap<>();private HashMap<String, StudentBean> map2 = new HashMap<>();private List<StudentBean> list;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);list = new ArrayList<>();StudentBean mStudentBean = new StudentBean();mStudentBean.setName("張飛");mStudentBean.setAge(18);StudentBean mStudentBean2 = new StudentBean();mStudentBean2.setName("關羽");mStudentBean2.setAge(21);map2.put("1", mStudentBean);map2.put("2", mStudentBean2);if (!list.isEmpty()) {list.clear();}Iterator<String> iter = map2.keySet().iterator();while (iter.hasNext()) {String key = iter.next();Log.e("TAG", "key:" + key);if (TextUtils.equals(key, "1")) {list.add(map2.get(key));}}Log.e("TAG", "list:" + list);} }總結
以上是生活随笔為你收集整理的kotlin和java和hashmap转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 红军vs蓝军
- 下一篇: 一键即可实现图片翻译成中文,多国语言任意