Skip to content

Instantly share code, notes, and snippets.

@inan-mahmud
Created December 4, 2019 16:21
Show Gist options
  • Save inan-mahmud/80c03d23e08ebdb94d13e5a9b3574ed4 to your computer and use it in GitHub Desktop.
Save inan-mahmud/80c03d23e08ebdb94d13e5a9b3574ed4 to your computer and use it in GitHub Desktop.
package com.company;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
public class HashTablePractice {
public static void main(String[] args) {
Hashtable<Integer, String> mTable = new Hashtable<>();
mTable.put(1, "computer");
mTable.put(2, "keyboard");
mTable.put(3, "Laptop");
mTable.put(4, "Monitor");
mTable.put(5, "Printer");
System.out.println(mTable.get(3));
mTable.put(5, "Mobile");
System.out.println(mTable);
System.out.println(mTable.getOrDefault(6, "Not found"));
Map<Integer, Student> studentHashtable = new Hashtable<>();
/* boolean m =mTable.contains(true);
System.out.println(m);*/
studentHashtable.put(1, new Student("Sayeed", "23"));
studentHashtable.put(2, new Student("Sayeed", "23"));
studentHashtable.put(3, new Student("Robin", "23"));
for (Map.Entry<Integer, Student> mapEntry: studentHashtable.entrySet()) {
System.out.println(mapEntry);
}
HashMap<Integer,String> hashMap = new HashMap<>();
hashMap.put(34,"sdfi");
hashMap.put(null,null);
hashMap.put(12,null);
hashMap.put(12,"isdf");
hashMap.put(14,null);
hashMap.put(166,null);
hashMap.put(16,null);
System.out.println(hashMap);
HashSet<Student> mNumber = new HashSet<>();
mNumber.add(new Student("Sayeed","23"));
mNumber.add(new Student("Sayeed","23"));
mNumber.add(new Student("Sayeed","23"));
mNumber.add(new Student("Sayeed","23"));
for (Student mValue: mNumber) {
System.out.println(mValue);
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// write your code here
/**
* ArrayList = Dynamic Array
*/
ArrayList<Integer> numbers = new ArrayList();
ArrayList<Student> studentArrayList = new ArrayList<>();
ArrayList<String> textList = new ArrayList<>();
ArrayList<Boolean> booleans = new ArrayList<>();
numbers.add(2);
studentArrayList.add(new Student("Sayeed","23"));
textList.add("Isdfs");
booleans.add(true);
numbers.add(4);
studentArrayList.add(new Student("Bappi","23"));
textList.add("Isdfs");
booleans.add(true);
numbers.add(5);
studentArrayList.add(new Student("Masum","23"));
textList.add("Isdfs");
booleans.add(true);
numbers.add(7);
studentArrayList.add(new Student("Rafi","23"));
textList.add("Isdfs");
booleans.add(true);
numbers.add(88);
studentArrayList.add(new Student("Robin","23"));
textList.add("Isdfs");
booleans.add(true);
numbers.add(9);
studentArrayList.add(new Student("Ekanto","23"));
textList.add("Isdfs");
booleans.add(true);
System.out.println(Arrays.toString(studentArrayList.toArray()));
Student student = studentArrayList.get(2);
System.out.println(student);
// studentArrayList.remove(student);
System.out.println(Arrays.toString(studentArrayList.toArray()));
boolean isDelete = studentArrayList.remove(student);
ArrayList<Student> mStudentList = new ArrayList<>();
mStudentList.add(new Student("sfsfs","12"));
mStudentList.add(new Student("sfsfs","12"));
mStudentList.add(new Student("sfsfs","12"));
mStudentList.add(new Student("sfsfs","12"));
mStudentList.add(new Student("sfsfs","12"));
studentArrayList.addAll(mStudentList);
for (Student mStudent: studentArrayList) {
System.out.println(mStudent);
}
}
}
package com.company;
import java.util.Objects;
public class Student {
private String name;
private String age;
public Student(String name, String age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "{"+ this.name+ " "+this.age+"}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(name, student.name) &&
Objects.equals(age, student.age);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment