Skip to content

Hacktoberfest contributions for JAVA #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions JAVA/FirstAndLastIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.util.Scanner;
public class FirstAndLastIndex
{
public static void main(String[] args)
{
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter String:");
String str = sc.next();
System.out.println("Enter character or substring to search: ");
String substr = sc.next();
System.out.println("First occurence "+ str.indexOf(substr));
System.out.println("Last occurence "+ str.lastIndexOf(substr));
}
}
}
39 changes: 39 additions & 0 deletions JAVA/HashMaps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.*;
import java.util.Map.Entry;

class HashMaps {

public static void main(String[] args)
{
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
map.put(5, "E");
if (map.containsKey(3)) {
System.out.println("Key 3 is present with value: "+ map.get(3));
}
else {
System.out.println("Key 3 is not present");
}
if (map.containsValue("A")) {
System.out.println("Value A is present");
}
else{
System.out.println("Value A is absent");
}

Set<Integer> keys = map.keySet();

for (Integer key : keys)
System.out.println(key);

Set<Entry<Integer, String>> entries
= map.entrySet();

for (Entry<Integer, String> entry : entries)
System.out.println(entry.getKey() + " : "
+ entry.getValue());
}
}
40 changes: 40 additions & 0 deletions JAVA/HashSets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.*;
class HashSets{
public static void main(String[] args) {
Set <String> set= new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
ArrayList<String> list= new ArrayList<String>();
list.add("x");
list.add("y");
list.add("z");
set.addAll(list);
Iterator<String> itr=set.iterator();
while(itr.hasNext() ){
System.out.println(itr.next());
}
set.clear();
System.out.println("Deleted all elements\nNew elements");

set.add("John");
set.add("David");
set.add("Mike");
set.add("Ricky");
Iterator<String> itr2=set.iterator();
while(itr2.hasNext() ){
System.out.println(itr2.next());
}
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Enter name to search: ");
String name=sc.next();

if(set.contains(name))
System.out.println(name+" found in the list");
else
System.out.println(name+" not found in the list");
}
}
}