Skip to content

Instantly share code, notes, and snippets.

@inan-mahmud
Last active December 22, 2019 16:16
Show Gist options
  • Save inan-mahmud/d43de2d6a23df1299ea0909c019f3581 to your computer and use it in GitHub Desktop.
Save inan-mahmud/d43de2d6a23df1299ea0909c019f3581 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);
}
}
}
package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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("Inan", 12));
mStudentList.add(new Student("Sayeed", 66));
mStudentList.add(new Student("Ruj", 9));
mStudentList.add(new Student("Robin", 13));
mStudentList.add(new Student("Arif", 60));
// Collections.reverse(mStudentList);
Collections.sort(mStudentList, new NameComparator());
System.out.println(mStudentList);
// studentArrayList.addAll(mStudentList);
for (Student mStudent : studentArrayList) {
// System.out.println(mStudent);
}
try {
Student student1 = mStudentList.get(1);
System.out.println(student1);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
System.out.println("Finally executed");
}
int n = 100 / 2;
if (n == 50) {
throw new ArithmeticException("not valid");
}
}
}
package com.company;
import java.util.Comparator;
public class NameComparator implements Comparator<Student> {
@Override
public int compare(Student student, Student t1) {
/* if(student.getAge()==t1.getAge())
return 0;
else if(student.getAge()>t1.getAge())
return 1;
else
return -1;*/
return student.getName().compareTo(t1.getName());
}
}
package com.company;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student(String name, int 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);
}
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;
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
/*@Override
public int compareTo(Object o) {
if (o instanceof Student) {
if (((Student) o).age == this.age)
return 0;
else if (((Student) o).age > this.age)
return -1;
else if (((Student) o).age < this.age)
return 1;
}
return 0;
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment