[JAVA] List, Set, Map

***** LIST ***** Normal Way -Java7

list.java


 import java.util.*;
 import java.lang.*;
 import java.io.*;


public class Main{
	public static void main (String[] args) throws java.lang.Exception
	{
	ArrayList<String> al = new ArrayList<>();
	al.add("A");
	al.add("B");
	al.add("C");
	
	
	
	for(String a : al){
		System.out.println("This is the list: " + a);
	}
	
	Iterator<String> it = al.iterator(); //point at one before the list
	while(it.hasNext()){
		System.out.println("This is the list: " + it.next());
       // it.next() : get the next element
	}
}
}

Lamda -Java8

list.java



class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
	List<String> ls = new ArrayList<>();
	ls.add(1);
	ls.add(2);
	ls.add(3);
	
//to show List 
	ls.stream().forEach(System.out::println);

//to convert List to a double array 
	ls.stream().forEach(i -> System.out.println(i * 2) );
	}
}


***** SET *****

set.java


 import java.util.*;
 import java.lang.*;
 import java.io.*;


public class Main{
	public static void main (String[] args) throws java.lang.Exception
	{
	
	Set<String> alset = new HashSet<>();
	alset.add("A");
	alset.add("B");
	alset.add("C");
	
	for(String a : alset){
		System.out.println("This is the set: " + a);
	}
	
	Iterator itset = alset.iterator();
	while(itset.hasNext()){
		System.out.println("This is the set: " + itset.next());
	}
}
}

***** MAP *****

map.java


 import java.util.*;
 import java.lang.*;
 import java.io.*;


public class Main{
	public static void main (String[] args) throws java.lang.Exception
	{
	
	Map<String, Integer> almap = new HashMap<>();
	almap.put("A", 123);
	almap.put("B", 234);
	almap.put("C", 345);
	
	for(String key : almap.keySet()){
		int value = almap.get(key);
		System.out.println( key + " has the number " + value);
	}
	

	Iterator<Map.Entry<String, Integer>> itmap = almap.entrySet().iterator();
	
	while(itmap.hasNext()){
		Map.Entry<String, Integer> entry = itmap.next();
		System.out.println("Key: " + entry.getKey() + "value: " + entry.getValue());
	}
	
	}
}
}

Lamda -Java8

list.java



class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
	Map<String, Integer> items = new HashMap<>();
	items.put("A", 10);
	items.put("B", 20);
	items.put("C", 30);
	items.put("D", 40);

// To show List 
items.forEach((k, v) -> System.out.println("Item:" + k + " Count:" + v));

// To convert Map to a double Map
	items.forEach((k, v) -> System.out.println("Item:" + k + " Count:" + v * 2));
	
	}
}

Recommended Posts

List, Set, Map
Array / list / map
[Java] Collection-List / Set / Map / Stack / Queue
Make a list map with LazyMap
[Java] Convert 1-to-N List to Map
Java8 list conversion with Stream map
[Java] Get List / Map elements with Iterator
Java (set)
Map method
Thread-safe list
JAVA (Map)
List method
Work list
Link list
[Java] Understand the difference between List and Set
[MyBatis] List <Map <>> Pass nesting as Mapper parameter