碰到一個需求,掃描wifi,然後把掃到的wifi顯示在list中,但是掃出來的wifi總是重複,為了實作去重效果,把list周遊,存儲成map,然後利用map自身的特性,達到去重的目的,然後拿到不重複的map wifi清單,然後再次周遊map,把符合條件的wifi資訊,存儲到新的list中,然後展示這個list,可以實作wifi清單實時掃描顯示
step1:
package com.example.demoanalytic
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.util.Log
class MapTestActivity : AppCompatActivity() {
// private HashMap<String, StudentBean> map2 = new HashMap<>();
private var map2: HashMap<String, StudentBean> = HashMap<String, StudentBean>()
private var wifiList = mutableListOf<StudentBean>()
private val wifi = "mindor-"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*
map2.put("1", mStudentBean);
map2.put("2", mStudentBean2);
* */
val mStudentBean = StudentBean()
val mStudentBean2 = StudentBean()
val mStudentBean3 = StudentBean()
mStudentBean.age = 18
mStudentBean.name = "郭靖"
mStudentBean2.age = 22
mStudentBean2.name = "楊過"
mStudentBean3.age = 32
mStudentBean3.name = "黃蓉"
map2["mindor-001"] = mStudentBean
map2["mindor-002"] =mStudentBean2
map2["608"] =mStudentBean3
Log.e("TAG", "$map2")
/*
if (!list.isEmpty()) {
list.clear();
}
* */
if (wifiList.isNotEmpty()){
wifiList.clear()
}
val iter: Iterator<String> = map2.keys.iterator()
while (iter.hasNext()) {
val key = iter.next()
Log.e("TAG", "key:$key")
/* if (TextUtils.equals(key, "1")) {
map2?.let {
it[key]?.let { it1 -> wifiList.add(it1) }
}
}
boolean isData = connectedWifiInfo.getSSID().contains(WIFI_NAME);
*/
if (key.contains(wifi)){
map2?.let {
it[key]?.let { it1 -> wifiList.add(it1) }
}
}
}
Log.e("TAG", "key:$wifiList")
}
}
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;
}
}
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;
@Override
protected 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);
}
}