Java的map和list学习 (1)

java

Java中List集合去除重复数据的方法

原链接地址

1.通过HashSet除重复元素

public static List removeDuplicate(List list) {   

HashSet h = new HashSet(list);

list.clear();

list.addAll(h);

return list;

}

2.把list里的对象遍历一遍,用list.contain(),如果不存在就放入到另外一个list集合中`

public static List removeDuplicate(List list){  

List listTemp = new ArrayList();

for(int i=0;i<list.size();i++){

if(!listTemp.contains(list.get(i))){

listTemp.add(list.get(i));

}

}

return listTemp;

}

3.删除ArrayList中重复元素,保持顺序

// 删除ArrayList中重复元素,保持顺序     

public static void removeDuplicateWithOrder(List list) {

Set set = new HashSet();

List newList = new ArrayList();

for (Iterator iter = list.iterator(); iter.hasNext();) {

Object element = iter.next();

if (set.add(element))

newList.add(element);

}

list.clear();

list.addAll(newList);

System.out.println( " remove duplicate " + list);

}


Map<\string,string>的遍历的四种方法

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.HashSet;

import java.util.Set;

import java.util.TreeMap;

public class NumCounter {

public static void main(String[] args) throws Exception {

Map<String, String> map = new HashMap<>();

map.put("key1","value1");

map.put("key2", "value2");

map.put("key3", "value3");

//第一种:普遍使用,二次取值

System.out.println("通过Map.keySet遍历key和value:");

for (String key : map.keySet()) {

System.out.println("key= "+ key + " and value= " + map.get(key));

}

//第二种

System.out.println("通过Map.entrySet使用iterator遍历key和value:");

Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();

while (it.hasNext()) {

Map.Entry<String, String> entry = it.next();

System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());

}

//第三种:推荐,尤其是容量大时</span>

System.out.println("通过Map.entrySet遍历key和value");

for (Map.Entry<String, String> entry : map.entrySet()) {

System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());

}

//第四种

System.out.println("通过Map.values()遍历所有的value,但不能遍历key");

for (String v : map.values()) {

System.out.println("value= " + v);

}

}

}

JAVA中Map的使用

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class NumCounter {

public static void main(String[] args) {

Map<String , Student> stu = new HashMap<String, Student>();

//添加元素

stu.put("0001", new Student("sunming1"));

stu.put("0002", new Student("sunming2"));

stu.put("0003", new Student("sunmnig3"));

System.out.println(stu);

System.out.println("-------------------------");

//删除元素

stu.remove("0001");

System.out.println(stu);

System.out.println("-------------------------");

//更新元素

stu.put("0002", new Student("sunmingUpdate"));

System.out.println(stu);

System.out.println("-------------------------");

//查询元素

System.out.println(stu.get("0003"));

System.out.println("-------------------------");

//获取所有的key和value

for(Map.Entry<String, Student> entry : stu.entrySet()){

System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());

}

System.out.println("-------------------------");

//获取所有的key

Set<String> keys = stu.keySet();

for(String key: keys){

System.out.println(key);

}

System.out.println("-------------------------");

//获取所有的value

Collection<Student> values = stu.values();

for(Student value: values){

System.out.println(value);

}

}

}

/**

* Created by SunMing on 2016/9/18.

*/

public class Student {

private String id;

private String name;

public Student(String strName) {

name = strName;

id = "00";

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String toString() {

return id + " " + name + ", ";

}

}

以上是 Java的map和list学习 (1) 的全部内容, 来源链接: utcz.com/z/393512.html

回到顶部